UVa 10127: Ones | MathBlog
Given any integer 0 ≤ n ≤ 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1’s. How many digits are in the smallest such a multiple of n?
Input
A file of integers at one integer per line.
Output
Each output line gives the smallest integer x > 0 such ∑x−1
i=0 1 × 10i = a × b, where a is the corresponding input integer, and b is an integer greater that p = than zero.
Sample Input
3
7
9901
Sample Output
3
6
12
This way we never actually work with the full result, but rather the full result modulo (which is at most ).
Read full article from UVa 10127: Ones | MathBlog
Given any integer 0 ≤ n ≤ 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1’s. How many digits are in the smallest such a multiple of n?
Input
A file of integers at one integer per line.
Output
Each output line gives the smallest integer x > 0 such ∑x−1
i=0 1 × 10i = a × b, where a is the corresponding input integer, and b is an integer greater that p = than zero.
Sample Input
3
7
9901
Sample Output
3
6
12
public
static
int
getDigitCount(
int
n) {
// loop through multiples of n
for
(
int
i = n; ; i += n) {
// create a copy of i
int
tmp = i;
// the number of digits in i
int
digitCount =
0
;
// whether i only consists of ones
boolean
ok =
true
;
// while there are digits left in tmp
while
(tmp >
0
) {
// if the current digit in tmp is not a one
if
(tmp %
10
!=
1
) {
// then i is not an answer
ok =
false
;
break
;
}
// the current digit is a one, so we chop it off
// and increment the digit count
tmp = tmp /
10
;
digitCount++;
}
// if i consists only of ones
if
(ok) {
// then i is a solution, so we return the digit count
return
digitCount;
}
}
}
The correct output for the last test case is . What does that mean? It means that the smallest multiple of that consists only of ones is digits long. Now we realize that we are using a signed 32 bit integer (aka int in Java and similar programming languages), and a signed 32 bit integer cannot contain integers greater than , which is about , but the correct multiple is about . So what can we do? Well, we could use a 64 bit integer (aka long), but you would later find out that it won’t work for this problem.
We have chosen to go the smart way instead, which is to throw out our current solution and try to find a better one.
Let’s try thinking about our previous solution, but in reverse mode. We generated multiples of and checked if they consisted only of ones. But what about doing the opposite? Generate numbers that consist only of ones, and check if they are a multiple of . Sounds like a good plan to me!
But how do we generate numbers that consist only of ones? It’s actually pretty easy… The first number is , the next number is , then , and so on. Or in general, .
public
static
int
getDigitCount(
int
n) {
// the first number to check is 1
int
current =
1
;
// 1 has a digit count of 1
int
digitCount =
1
;
// while n is not divisible by the current number
while
(current % n !=
0
) {
// we add one 1 to the end of current
current = current *
10
+
1
;
// and increment the digit count
digitCount++;
}
return
digitCount;
}
public
static
int
getDigitCount(
int
n) {
// the first number to check is 1
int
current =
1
;
// 1 has a digit count of 1
int
digitCount =
1
;
// while current is not divisible by n
while
(current % n !=
0
) {
// add one 1 to the end of current,
// and do it modulo n
current = (current *
10
+
1
) % n;
// and increment the digit count
digitCount++;
}
return
digitCount;
}