I could just use division and modulus in a loop, but this is slow for really large integers. The number is stored in base two, and may be as large as 2^8192. I only need to know if it is a power of ten, so I figure there may be a shortcut (other than using a lookup table).
Asked
Active
Viewed 208 times
6
-
1Idea: Count the trailing zeros, then compute 5 to the power of whatever that was and compare it to your number shifted right. – user2357112 Dec 28 '13 at 08:43
-
1What language are you working in, and what bignum implementation are you using? That'll affect what operations are cheap and what bitwise operations are available. – user2357112 Dec 28 '13 at 08:50
-
10=2*5. Why didn't I think of that? Also, I am using Java and the number is stored as a simple list of integers. – warren Dec 28 '13 at 08:59
-
Why is using a lookup table undesirable? Aside from that, maybe try successive powers of ten and see if they equal your number? Don't know if that'd be faster or slower. (To speed this idea up, you can do a binary search on the space of valid powers of ten less than 2^8192 instead of a linear search) – Pandu Dec 28 '13 at 08:59
-
I know a lookup table would work. But I don't want to store one in memory. – warren Dec 28 '13 at 09:03
-
List of integers? Like base-10 digits or base-FFFFFFFF digits or what? – Pandu Dec 28 '13 at 09:10
-
to be very specific, 0-(2^32-1) digits stored in a custom linked list data structure. – warren Dec 28 '13 at 09:25
2 Answers
8
If your number x is a power of ten then
x = 10^y
for some integer y, which means that
x = (2^y)(5^y)
So, shift the integer right until there are no more trailing zeroes (should be a very low cost operation) and count the number of digits shifted (call this k). Now check if the remaining number is 5^k. If it is, then your original number is a power of 10. Otherwise, it's not. Since 2 and 5 are both prime this will always work.

mimicocotopus
- 5,280
- 4
- 22
- 24
-
1
-
2Another fail fast test: `add all the bytes of the number, and if the sum is divisible by 5, the number is divisible by 5`. See http://mathforum.org/library/drmath/view/55908.html – Jakub Kotowski Dec 28 '13 at 09:35
-
1another fail fast test is powers of five end in binary 01 and the rest of the digits follow a repeating pattern (k mod 2^number_of_digits) that can allow for a condensed table. – warren Dec 28 '13 at 11:22
0
Let's say that X
is your input value, and we start with the assumption.
X = 10 ^ Something
Where Something
is an Integer
.
So we say the following:
log10(X) = Something.
So if X
is a power of 10
, then Something
will be an Integer
.
Example
int x = 10000;
double test = Math.log10(x);
if(test == ((int)test))
System.out.println("Is a power of 10");

christopher
- 26,815
- 5
- 55
- 89
-
1This depends on a fast, ultra-high-precision implementation of the base 10 logarithm for arbitrary-precision integers. I don't think most logarithm implementations have enough precision to detect that 10^100+1 isn't a power of 10. Even if you had such a library, I doubt it would be faster than the divide/modulus loop. – user2357112 Dec 28 '13 at 08:49
-
Hmm.. It's a good point. But perhaps if we implement this using the `BigDecimal` class, we can take advantage of the infinite precision? – christopher Dec 28 '13 at 08:50
-
Also, you have a mistake in your first equation, it should be X = 10 ^ Something – Jakub Kotowski Dec 28 '13 at 08:53