28
2
Challenge description
For every positive integer n
there exists a number having the form of 111...10...000
that is divisible by n
i.e. a decimal number that starts with all 1
's and ends with all 0
's. This is very easy to prove: if we take a set of n+1
different numbers in the form of 111...111
(all 1
's), then at least two of them will give the same remainder after division by n
(as per pigeonhole principle). The difference of these two numbers will be divisible by n
and will have the desired form. Your aim is to write a program that finds this number.
Input description
A positive integer.
Output description
A number p
in the form of 111...10...000
, such that p ≡ 0 (mod n)
. If you find more than one - display any of them (doesn't need to be the smallest one).
Notes
Your program has to give the answer in a reasonable amount of time. Which means brute-forcing is not permited:
p = 0
while (p != 11..10.00 and p % n != 0)
p++
Neither is this:
do
p = random_int()
while (p != 11..10.00 and p % n != 0)
Iterating through the numbers in the form of 11..10..00
is allowed.
Your program doesn't need to handle an arbitrarily large input - the upper bound is whatever your language's upper bound is.
Sample outputs
2: 10
3: 1110
12: 11100
49: 1111111111111111111111111111111111111111110
102: 1111111111111111111111111111111111111111111111110
Can we have a reasonable upper bound to the possible output? (Something about less than 2.4 billion (approx. the max value of a signed integer) should be fine, as arrays or lists might be required for some implementations) – Tamoghna Chowdhury – 2016-02-28T15:25:38.573
@MartinBüttner I think that the first satisfying output should be enough (reasonable timeframe constraint) – Tamoghna Chowdhury – 2016-02-28T15:26:46.883
The last 0 is not necessary in the 49 test case. – CalculatorFeline – 2016-02-28T15:36:28.527
@CatsAreFluffy I think all numbers need to contain at least
1
and at least one0
, otherwise0
is a solution for any input. (Would be good to clarify this though.) – Martin Ender – 2016-02-28T15:38:38.920Just requiring one
1
should work. – CalculatorFeline – 2016-02-28T15:41:29.393Challenge description updated. @MartinBüttner: It needs to start with
1
, I think it's stated rather clearly in the description. – shooqie – 2016-02-28T15:48:24.480@CatsAreFluffy: It is, otherwise it wouldn't end with
0
. – shooqie – 2016-02-28T15:50:58.697Surprisingly, I think this is actually a computationally hard problem for which there is no polynomial algorithm in the size (number of bits) of
n
. For a given numbern
(with no factors of 2,3,5) we need to find somek
so10^k-1
is a multiple ofn
. So,k
is some multiple of the order of10
in the multiplicative group modulon
. Generically, ifn
is a product of two primes, this order isphi(n)
, and finding this is equivalent to factoringn
. I think that being asked for a multiple of this order isn't easier. – xnor – 2016-02-28T15:51:18.427Uhh...why does 3 matter? – CalculatorFeline – 2016-02-28T15:55:39.917
@CatsAreFluffy A number like
1111
is(10^4-1)/9
, and this division takes out factors of 3. – xnor – 2016-02-28T15:57:04.823Do we have to provide the smallest possible output or just one that works? – CalculatorFeline – 2016-02-28T16:17:33.177
This could be a duplicate because I remember making this exact pigeonhole argument in a comment on another question... – feersum – 2016-02-28T19:43:12.977
About the computationally hard problem: Doesn't the 1^n0^n trick imply a quadratic-time algorithm? (x=searching, y=modulo) – CalculatorFeline – 2016-02-29T01:07:57.990
Related. @feersum, is this the one you were thinking of? – Peter Taylor – 2016-02-29T13:52:54.070
@PeterTaylor Yep, that's it. I see it's a bit different in that the zeroes and ones may come in any order. – feersum – 2016-02-29T15:03:19.427