Powers of 2 with all even digits
107 comments
·March 20, 2025nneonneo
FartyMcFarter
I think you can calculate "2^X mod (10^N)" where N is the number of digits using a modular exponentiation algorithm.
This would avoid using a lot of memory, and it would also be faster.
nneonneo
I am already doing that (thanks to `pow(x, y, z)` in Python). The numbers I'm working with would have trillions of digits were it not for this trick - way more than 1GB. 1GB is what I use to store all of the candidates, in an inefficient JSON format.
shrx
Is your algorithm published somewhere?
nneonneo
I literally just invented it, so it's not published yet, although I'll probably throw it up on GitHub at some point. It matches all published results up to an exponent of 3 billion or so, so I'm quite confident it's correct.
The short explanation is that 2^x mod 10^k will repeat with a cycle length of 5^(k-1)*4. This is easily obtained from Euler's phi formula on 2^x mod 5^k, plus the fact that 2^x === 0 mod 2^k for all x >= k. So, after the k'th term, the rest will repeat with a particular cycle period. We'll manually test all of the 2^x for x < k (there aren't many) and then rely on the cycles to test all of the larger powers.
The algorithm itself is a kind of sieving + lifting procedure: it inductively identifies all of the candidate exponents mod 5^(k-1)*4 which yield numbers with k trailing even digits (i.e. all even digits mod 10^k). Each such exponent will yield 5 possible exponents mod 10^(k+1) via a lifting procedure, which we can test; on average, half of these will have a top digit that is even and is therefore a candidate for the next power of 10 (10^(k+1)). Therefore, on average, we grow our candidate list by a factor of 2.5 per added digit - thus, for each 10^k, we test O(2.5^k) candidates (approximately 1.6*2.5^k, experimentally). This isn't too bad - at mod 10^19, we test only 55097940 candidates, representing every exponent below 5^18*4 = 15258789062500.
My rather hasty prototype is actually implemented in Python - not a language you want to do tons of arithmetic in - but it's still fast enough to chew through all those candidates in ~20 minutes on a single core. Obviously, there's ample room to make this faster; I figure a good, parallelized native (C/C++/Rust etc.) implementation could easily be 100x faster.
bluewin
I worked on this once after an argument with my boyfriend.
The original argument was "the ones digit has permanent pattern in 2^n {2,4,8,6,2...}.
We made a system to generate digits for powers of two, although eventually we just made one that can take arbitrary bases, and found that you can decompose digit frequency and find a variety of NMR like resonances that vary based on where you terminate data collection.
It was really fun and this makes me want to get back into this so I could check the properties of those resonances across bases and stopping points for data collection.
zoky
> The original argument was "the ones digit has permanent pattern in 2^n {2,4,8,6,2...}.
Isn’t that obviously the case (for n >= 1 anyway)? If each successive power of two is just the previous number times two, then it would always have to follow that pattern.
Any integer >= 10 can be expressed as the sum of a multiple of 10 plus a single digit number, for example 32 = 30 + 2. So 32 * 2 can be written as 2 * (30 + 2). And since any integer ending in zero multiplied by any integer must also end in zero, you only need to look at the single digit part of the number to see that a pattern must immediately emerge for powers of two, or of any number for that matter.
pinkmuffinere
> I worked on this once after an argument with my boyfriend.
Wow I love this relationship dynamic! you sound like very cool people
Nifty3929
Followed by "... We made a system to generate digits for powers of two" ('we' not 'I')
That's awesome!
swyx
is this kind of argument normal for you two?
what.. what other arguments have you had?
i request highlight reel
millipede
Unbelievable, they actually missed one: 2^(log(22)/log(2)) has all even digits!
Mae_soph
I might have a proof that this list is complete (I am very tired though and should be sleeping instead of doing this, so my apologies if I'm wrong): Because we can only get one extra by carrying, each digit of 2^(k - 1) is at most 4 (otherwise the next digit in 2^k will be odd).
Assume this list is complete up to 10^n. We find the biggest l such that 2^(5^(l - 1)*4) < 10^n. Let us consider the 10^(n+1) > 2^k > 10^n such that 2^k has all even digits. By cyclicity of powers of 2 mod 10^l (that's why we chose this l), this means that 2^(k - 1) = a*10^l + b, where a is some integer and b is 1,2,4,32 or 1024 (because those are the only options with digits less than 5 mod 10^l). If l > 10,that means that we can divide by b to get 2^(k-1)/b = c*10^d + 1 where c and d are nonzero integers. But this is a contradiction.
Now we only need to show up to 2^(5^10 * 4) to allow l > 10, which has already been done by other comments.
IIAOPSW
I might have a proof too but it is too large for the margin of this text box.
waffletower
Definitely not finite in radix-16 (hexadecimal): [2 4 8 10 20 40 80 100 200 400 800 1000 2000 4000 8000 10000 20000 40000 80000 100000 200000 400000 800000 1000000 ...]
or radix-8 (octal): [2 4 10 20 40 100 200 400 1000 2000 4000 10000 20000 40000 100000 ...]
Interesting puzzle due to radix representation and sequence interactions.
parsimo2010
I'm not a number theorist, but I note that 16 is 2^4 and 8 is 2^3 (both powers of 2). Maybe there is a provable statement about whether these lists are finite in bases that are not 2^k, and maybe there is a bound on the length of the list by the value of log_2(base).
I'm not going to write it out, there is certainly a proof that the list is infinite in base 2^k (for integer k >= 2). I'm more wondering about how hard it is to prove that the list is finite in a different base.
ethanwillis
when dealing with only even and odd they are not finite in base 2^k.
if we marked sequences of integers with 3 options. even, odd, other. then these lists are not finite in bases of 3^k.
for four options. even, odd, other, another. then these lists are not finite in bases of 4^k.
there is an intersection in the infinite lists where the base is equivalent to the power of an earlier base.
so infinite lists for 2^k would overlap a subset of the infinite lists for 2^2^k=4^k
all prime bases, p, p^k would admit infinite lists that cover all the infinite lists for some composite base, c, c^k.
ethanwillis
there is another similar problem about the largest number where all digits are prime numbers. which afaik has only been proven in base 10.
similarly there the largest number with all prime digits actually differs if you ask the question in different bases.
and there is also a pattern that exists to predict what the number will be in a given base.
WithinReason
No additional terms up to 2^(10^10). - Michael S. Branicky, Apr 16 2023
How did he do this?
sltkr
To prove that there is no value of k between 12 and 10^10 such that 2^k has all even digits, you only have to prove that there is an odd digit among the lowest X decimal digits for all 12 ≤ k ≤ 10^10.
The value of X necessary to prove this grows rather slowly compared to k. For example, the smallest power of 2 that doesn't have an odd digit in its last 16 digits is 2^12106. The smallest power of 2 that doesn't have an odd digit in its last 32 digits is 2^3789535319. So it makes sense to try increasingly large values of X until you are able to rule out all values of 2^k for k up to 10^10.
Here's a C++ program you can run to replicate this proof. It takes around 20 minutes to run, and can probably be optimized further, but it shows the principle: https://pastebin.com/DVK2JKdq
gridspy
This optimization is important because you can then discard most of the number in question, bounding the integer size required for computation.
For instance you could store the number in question in a 128 bit integer, shift left (double), check for odd digits (a series of modulo & divide operations) and then truncate using a modulo and subtract. You can repeat this process as long as you like. If you find an all evens number than you can do a more expensive indepth check.
lifthrasiir
As noted in 3) in the Shepherd's comment, 2^k has no odd digits when 2^k mod 10^n for all integer n have no odd digits as well. So many k would be filtered by checking whether 2^k mod 100 has an odd digit, then another portion of the remainder will get filtered with 2^k mod 1000, 2^k mod 10000 and so on. (EDITED: Thanks to andrewla!) All of them would be periodic, so first few steps can be made into a lookup table to filter almost every k.
andrewla
> whether 2^k mod 10 is odd
2^k mod 10 is never odd; it's the cycle (2, 4, 8, 6).
Related here is the length of the cycles mod 2^k, https://oeis.org/A005054. Interestingly, the number of all-even-digit elements in those cycles does not appear to be in the oeis, I get 4, 10, 25, 60, 150 as the first five terms.
This does appear to get more efficient as k gets higher; for k=11 I get a cycle length of 39,062,500 with an even subset of 36,105, meaning only .09% of the cycle is all-even.
This is all brute force; there's probably a more elegant way of computing this.
andrewla
I see my error here -- you can in fact eliminate half even for 2^k mod 10, because both 6 and 8 force a carry; so ending in a 2 or a 6 means that the next higher digit must be odd.
Retr0id
So I suppose if you ever find a cycle where the full cycle has no all-even members, you can prove that there are no more all-even numbers to find.
lifthrasiir
Oh, yeah, I should have said 2^k mod 100 has no odd digits.
madcaptenor
10^10 * 36105/39062500 = 9242880, so you're already down to under 10^7 cases to check, which is starting to seem more tractable.
AnotherGoodName
There likely is a trick but the above is also technically feasible as-is.
You have to do this for 10^10 (ten billion) powers. Each operation needs to check ~4.3billion decimal digits at worst (half that on average). It's highly parallelizable since each power is an easy to compute binary digit and you can do a binary->decimal conversion without relying on previous results which is a log(n) operation, ie one operation per decimal digit.
All up 10^10 powers * ((10^4.3)/2) decimal digits to calculate and check for each of those powers. Around 200 trillion operations all up in human terms. It's still hard enough you'd want a lot of compute. Getting each operation down to a nanosecond still means you're waiting 2.3days for a result. But it's also fair to say it's feasible.
thaumasiotes
> and you can do a binary->decimal conversion without relying on previous results which is a log(n) operation, ie one operation per decimal digit
Aren't those operations divisions? One division would usually be considered more than one operation.
fdej
Just check for the existence of at least one odd digit mod 10^B for some well chosen B.
Here is a C program that does the verification up to 2^(10^10) in 30 seconds: https://gist.github.com/fredrik-johansson/8924e10e5d74e39109...
Edit: made it multithreaded, goes up to 2^(10^12) in nine minutes on 8 cores.
LeftHandPath
Hah, I had Michael Branicky as a professor (for his AI course) at the time (Jan - May 2023). Didn't expect to see his name here. He's a brilliant guy.
theamk
This is plausible with brute-force, perhaps with some basic optimization.
You only need to test 10^10 values, and that is just less than 2^34 cases. Not hard to brute force at all, and trivial to parallelize too.
sltkr
You forget that the number of decimal digits grows linearly with the exponent. To generate the first n numbers of the form 2^n numbers you need O(n^2) time.
For example, 2^(10^10) is 10^10 bits and about 3 billion decimals digits.
So for n up to 10^10, you need to do about (10^10)/2 = 5×10^19 elemental operations. At one operation per nanosecond that takes 1584 years of CPU time. Not at all easy to brute force!
theamk
No, I did not forget.
First of all, 1584 years of CPU time is not that bad.. if your university has a lab of 200 computers, each with 64 cores, that's already 45 days. If there is SETI-like system which lets researchers run their code on idle PCs, the calculation like this might get finished in a few months. Don't underestimate amount of idle compute sitting around in large organizations.
Second, while you can use naive algorithm (generate number, use something like GMP to convert to decimal, find odd digit), there are some pretty trivial optimizations. The OEOIS comments mention most numbers have odd values in last few digits, so in most cases, all you need to do is to calculate (2^n mod 100000000) and check that there is an odd digit there. Only if if there is not (which should be pretty rare) then you pull out that GMP and start do full check.
But wait, there is more! 2^(10^10) is a single binary 1 followed 9999999999 binary zeros, so it seems stupid to waste gigabytes of memory bandwidth storing all that zeros, and you don't need a result either. Implementing your own custom division algorithm specialized for those numbers will let you have tight loop with almost no memory accesses - something that modern CPUs do very fast. I would not be surprised if you can even get GPU to do it for you.
There could be more opportunities for improvement.. For example, I suspect the internal state of that division algorithm might end up being periodic, in which case you'd be able to quickly come up with an answer without having through go to every digit. But even if that's not possible, the optimization will make this problem pretty tractable.
gavinsyancey
To prove a number is on the list, you need to calculate all its digits. But to prove it's not on the list, you only need to calculate its digits up to the first odd one. It looks like the number of digits until the first odd grows very slowly; per the comments there up to n=50000 it has a maxiumum of 18.
38
yeah that's weird - its kind of a pointless comment without an included algorithm or something
madcaptenor
There’s probably a smart way to rule out a lot of cases so you only have to check a relatively small number of candidates. It would be good to know what it is.
taneq
I guess the margin was too small to contain it.
vhcr
Here's a really dumb algorithm:
for i in range(1, 10**10):
for k in range(1, 5):
s = str(pow(2, i, 10**(10**k)))
if '1' in s or '3' in s or '5' in s or '7' in s or '9' in s:
break
else:
print(2**i)
It's really easily to parallelize, I was able to run it up to 10**8 in about 15min, so you would be able to run it up to 10**10 in a few hours with parallelization.toxik
It's not 10^10 ≈ 2^33 though, it's 2^(10^10) = 2^10000000000, or about 9 999 999 967 orders of magnitude more.
null
Aardwolf
In base 2 there are 0 of those since all are of the form 1000...
hrldcpr
The base 2 list is even shorter.
IsTom
It might be finite, but it also has a "fast growing sequence" kind of smell too.
vessenes
I thought that at first as well. Then I read the notes which made me reframe it as ‘odds your digit sequence won’t include a six ever’ and note that checking up to 2^50000 has only two candidates with the first 15 digits even, and I came down on ‘shrinking so quickly it’s super unlikely’. No proof here due to HNs comment limits of course..
francoi8
I wonder if we can get a sense of how fast it would grow if we hypothesize it is an infinite sequence.
And if it is a finite sequence, one could define f(p, n) as the sequence of successive exponents of 2 such that the ratio of even digits over its total number of digits is greater than p. This could be an interesting way of describing a set of fast growing functions from exponential growth (p=0) to arbitrarily fast growth as p grows closer to 1 (or P where P is the smallest number such that f(P, n) is a finite sequence).
andrewla
This is remarkable! I always find it fascinating that simple to express properties lack a proof. This is a very simple thing to evaluate and seems like it should be straightforward to establish that 2048 is the highest such power.
sweezyjeezy
Base‑10 is just our chosen way of writing numbers, it doesn’t need to have any deep relationship with the arithmetic properties of sequences like the powers of 2. For most series (Fibonacci numbers, factorials etc), the digits for large members will be essentially random, their digits don't obey any pattern - it's just two unconnected things. It seems extremely likely that 2048 is the highest, but there might not be a good reason that could lead to a proof - it's just that larger and larger random numbers have less and less chance of satisfying the condition (with a tiny probability that they do, meaning we can't prove it).
Interestingly, there are results in the other kind of direction. Fields medalist James Maynard had an amazing result that there are infinitely many primes that have no 7s (or any other digit) in their decimal expansion. This actually _exploits_ the fact that there is no strong interaction between digits and primes - to show that they must exist with some density. That kind of approach can't work for finiteness though.
kens
Yes, I find math problems that depend on base 10 to be unsatisfying because they rely on arbitrary cultural factors of how we represent numbers. "Real" mathematics should be universal, rather than just solving a puzzle.
Of course, such a problem could yield deep insight into number theory blah blah blah, but it's unlikely.
codeflo
Everything about this seems so arbitrary. You look at the powers of an arbitrary number (here, 2), you pick an arbitrary base (here, 10) in which to express those powers, and ask for a random property of its digits (whether they belong to the set {0,2,4,6,8}).
Nothing about this question feels natural. I've noticed that random facts often don't have simple proofs.
LegionMammal978
In this case, it doesn't even help to downsize the problem. Erdős once asked the same question, but with powers of 2, base 3, and the set {0,1}. (If you want to, you can disguise that version as something more natural-looking like "Which powers of 2 can be expressed as the sum of distinct powers of 3?") But we're still nowhere close to solving it.
intalentive
You can generalize it if you want. Given powers of p in base b, what is the largest n=p^i such that each digit is divisible by k. Here we have: if p=2, b=10, k=2, then n=2048 and i=11. Why? Maybe there is a deeper reason that applies to all values of p, b, k.
null
Sharlin
Proofs of non-existence aren't usually straightforward.
andrewla
I mean, clearly it isn't in this case. But given that the digits of 2^n are cyclical at each decimal position, it does feel like this should fall out of some sort of chinese remainder theorem manipulation.
Sharlin
True. It might also just be that the question hasn't attracted the attention of number theorists, and finding a proof wouldn't be unreasonably difficult to an expert in the field.
guy234
why should it be straightforward to establish that?
openasocket
For those curious, one relevant field of mathematics that could be used to prove properties of this sequence would be Sieve theory: https://en.m.wikipedia.org/wiki/Sieve_theory
jmount
Simple permutations of digits: https://rworks.dev/posts/digital-difficulties/
Fun fact: 2^133477987019 is the smallest power of two that ends with 40 even digits. In fact, it ends with 46 even digits - which is surprising, given that it is significantly smaller than 2^(2^46). The last 50 digits of this number are ...32070644226208822284248862288402404246620406284288. This number has over 40 billion digits, though, so it seems kind of unlikely that we will ever find another number where all the digits are even. The relevant OEIS sequence is here: https://oeis.org/A096549
Context: I wrote a search program that is substantially faster - it takes just a few minutes to get up to 2^(10^13), although my laptop's limited memory is starting to be a problem (my intermediate result file is already nearly 1GB in size). Unfortunately, it seems there are no results up to 2^15258789062500, which is a 4.5-trillion digit number.