Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers ending in 3 have at least one multiple having all ones

Hi Here is a Q that was asked in Adobe Interview.

Numbers ending in 3 have at least one multiple having all ones. 
for eg., 3 and 13 have amultiples like 111 and 111111 respectively. Given such 
a no. , find the smallest such multiple of that number. The multiple can 
exceed the range of int, long. You cannot use any complex data structure.

Can you provide me with an efficient solution

like image 446
Euler Avatar asked Jul 02 '13 18:07

Euler


People also ask

How do you find multiples of 3?

The multiples of 3 are the numbers, which are obtained by multiplying 3 with any natural numbers. In other words, the multiples of 3 are the numbers that leave the remainder value of 0, when it is divided by 3. Some of the examples of multiples of 3 are 6, 15, 27, 36, etc.

What is the largest multiple of 3?

The multiples of 3 are: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, … The multiples of 4 are: 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, …

Is 1 a multiple of every number?

Every number is a multiple of 1 and itself.

Is zero a multiple of all numbers?

Zero is a multiple of every number so (among other things) it is an even number. When asked for the “smallest” multiple (for example, the least common multiple), the implication is that only positive multiples are meant.


1 Answers

Here is an attempt to do it more efficiently that trying 1, 11, 111, 111.. Could this pay off. Is there a more elegant answer better than trying numbers one at a time?

Write the numbers 1, 11, 111, ... as (10^k - 1) / 9, where the division is known to be exact. Given a target number in the form 10x+3 we want to find the smallest k solving (10^k - 1) / 9 = Y(10x + 3) where Y is an integer. Look for small solutions of 10^k = 1 mod 9(10x + 3). This is http://en.wikipedia.org/wiki/Discrete_logarithm except that arithmetic mod 9(10x + 3) does not necessarily form a group - however the http://en.wikipedia.org/wiki/Baby-step_giant-step algorithm should still apply and could be used to search steadily increasing ranges of k, instead of searching possible values of k one at a time.

like image 159
mcdowella Avatar answered Sep 19 '22 13:09

mcdowella