Every positive integer has a multiple that, when expressed in base ten, is comprised
entirely of zeros and ones. Finds the smallest multiple of 98961 that has this property.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Nice solution! That's how I did it too.
9 8 9 6 1 × i = n
If n is the integer composed of only 1 's and 0 's, then n must equal 1 or 0 mod 1 0 . The values of i that allow this are themselves 1 or 0 mod 1 0 . So the only values of i we would have to check are 1 0 k or 1 0 k + 1 . This cuts our search space by 1 0 . An implementation using this idea in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
It takes time :/
Log in to reply
It sure does.. ~5s was what was on top of my head. Nice solution though also nice status ..
I thought you would always post your answer only in Python?
Log in to reply
Incidentally I am learning java in this course I am taking, so I thought your prob would be great practice
In your face xp (look at my solution)
Log in to reply
Haha..Good for you.. upvoted
i used a simple heuristic to speed up iterations.
first of all, let us assume that we have to multiply 'c' to 98961 to get the solution. now, the unit digit of 'c' has got to be 1 because if it's 0, we can trim it down to get a smaller multiplier. since unit digit of 'c' is 1, tens digit of 'c' must be 4 or 5 to keep the tens digit of product 0 or 1 correspondingly.
hence iterate by using a multiplier c which increments by 10 if it's tens digit is 4 and by 90 it it's tens digit is 5. thus the last two digits of c remain 41 and 51. we can continue with this reasoning but even this much gives a fast enough solution (within 0.047 seconds on my PC).
Here is the c++ code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
edited. you can click the "edit" button to view what I have changed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Fastest algorithm
it is quite fast but i sincerely doubt it is the fastest as it is ultimately a brute force iteration without any regard to the nature of the given number and the restrictions imposed on the number which has to be multiplied to 98961 to get a product as required.
it looks like you brute forced all values of 'i' till 'i' was divisible by number. looking at the length of solution, your algorithm would have finished within around 8000 (~2^13) iteration.
for your solution, one way to make it twice as fast would be to restrict the last digit of i to 1 (for smallest such product, unit digit has to be 1 since last digit of 98961 is 1) and the count of iteration is cut to half.
I also used the fact that 9 8 6 9 1 has to multiplied by 1 0 i and 1 0 i + 1 to have the last digit of the multiple be 0 and 1 respectively. I used the following Python code.
i = 1
while True:
s = str(989610*i)
result = filter(lambda x: x=='0' or x=='1', s)
if result == s: break
s = str(98961*(10*i+1))
result = filter(lambda x: x=='0' or x=='1', s)
if result == s: break
i += 1
print s
What is the number?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
I solved this by generating all integers in base 10 containing 0s and 1s, and using BFS algorithm to get the smallest multiple. C++ code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Boom, 1110010010001
No clever mathematics, easy to code.
Simple brute-force in Python:
def f(x):
while x:
if x % 10 != 0 and x % 10 != 1:
return False
x = x / 10
return True
n = 98961
while not f(n):
n = n + 98961
print n
I have solved this problem with C++ using Dynamic Programming(DP). Here's my solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
1 2 |
|
Problem Loading...
Note Loading...
Set Loading...
I check through all the numbers containing only zeros and ones by converting all positive integers into binary.
Java code:
Output: