# Alternating numbers

Alternating numbers are numbers in which all digits alternate between even and odd. For example: 2703 and 7230 are alternating numbers, but 2730 isn't.

Numbers are very alternating when double the number is an alternating number as well, for example 3816 is very alternating, because 7632 is an alternating number as well.

The question to you is: how many 4 digits very alternating numbers are there? (the number can't start with one or multiple zeros)

Of course this problem can be solved by programming, but it can be solved mathematically as well! Good luck!

NB: I didn't come up with this puzzle myself, it's part of the Dutch "Wiskunde Olympiade": http://www.wiskundeolympiade.nl/files/opgaven/1e-ronde/2015/opgaven_en.pdf

• in this scenario 5000 is the maximum of possible very alternating numbers, right? – Raystafarian Feb 1 '16 at 17:57
• the alternating number itself must be 4 digits, but the double of the alternating number may be more than that. – Xander Feb 1 '16 at 18:01
• Assuming we're sticking to 4-digits, it turns out there's only ONE very very alternating number: 1818, which doubles to 3636, and then again to 7272. There may be more if you allow the double and double-double to be 5 digits... – Darrel Hoffman Feb 1 '16 at 19:48
• There cannot be a 4-digit very-alternating number whose double has 5 digits. Proof: Every 5-digit number that is twice a 4-digit number must start with a 1, which is odd. And all multiples of 2 must end in an even digit. Since no odd-digit number that starts odd and ends even can possibly be alternating, there can be no very-alternating numbers between 5000 and 9999. – Darrel Hoffman Feb 1 '16 at 20:38
• Just to add more neat observations, here's a complete list of very very alternating numbers below 10000: 3, 9, 18, 109, 309, 418, 818, 909, 1818. Of these, only 9, 109, and 909 are very very very alternating. I'm pretty sure it's impossible to find one that's five levels of very alternating. – Darrel Hoffman Feb 1 '16 at 21:10

There number of such numbers is

70

When you double a number, a digit of the result is even if and only if the digit to the right didn't carry.

Therefore, $2x$ is alternating if and only if the digits of $x$ are $LHLH$, where $L \in \{0,1,2,3,4\}$ and $H \in \{5,6,7,8,9\}$.

(As a side note, this implies that if $x$ is very alternating, then $2x$ will still be a four digit number, so that $x < 5000$ necessarily).

We know that if $x$ is alternating when it looks like OEOE or EOEO. For $x$ to be very alternating, it also must be $LHLH$, so let's count the number of ways to fulfill both these constraints.

• OEOE: Each odd number must be low, meaning they are each $1$ or $3$. Each even must be high, so is either $6$ or $8$. There are two choices for each digit for a total of $2\cdot2\cdot2\cdot2=16$ possibilities.

• EOEO: Now the evens are low ($0,2$ or $4$), and the odds are high ($5,7$ or $9$). There are three choices for each digit, except the first digit can't be zero, so there are $2\cdot3\cdot3\cdot3=54$ possibilities.

• I'm kind of lost on your explanation. I get to the LHLH but after that, maybe I'm dense? – Raystafarian Feb 1 '16 at 18:34
• According to the comments on the OP, the double of the alternating number can be more than 4 digits. – GentlePurpleRain Feb 1 '16 at 18:36
• @GentlePurpleRain It doesn't matter. A five-digit number has to start with 1, but its last digit must be even, so it can't alternate. – f'' Feb 1 '16 at 18:44
• @f'' yeah, but also no 5 digit numbers make the cut anyway – Raystafarian Feb 1 '16 at 18:47
• @mikeearnest added a small bit to your answer. Feel free to roll back if you don't like it – Kevin Feb 1 '16 at 18:50

70

I did a manual count. Still trying to work out a mathematical solution.

• I'm sorry, that's not the answer. How did you come to this answer? – Xander Feb 1 '16 at 17:59
• @xander check my latest edit – Kevin Feb 1 '16 at 18:00
• I'm sorry, still not the right answer – Xander Feb 1 '16 at 18:02
• Wait, I see what I did wrong, hold on – Kevin Feb 1 '16 at 18:05
• @xander How about now? – Kevin Feb 1 '16 at 18:24

Damn I started this brute force before the answers and I got

70

VBA

Better logic for this route would have been

if (one mod 2 = three mod 2) and (two mod 2 = four mod 2) then
if one mod 2 <> two mod 2 then
counter increase


Here we go, brute force again!

70

You may check JSFiddle for source code and full list of 4-digit very alternative numbers.

70

#AnubhavBalodhi, puzzling.stackexchange,26036, 1/2/16 2341 hrs
E=['0','2','4','6','8']
O=['1','3','5','7','9']

ans=0
alters=[]

def Alt(num):
N=str(num)
if len(N)<5: #4 digit
if (N in E and N in O and N in E and N in O) or (N in O and N in E and N in O and N in E):
alters.append(num)
else:       #5 digit
if (N in E and N in O and N in E and N in O and N in E ) or (N in O and N in E and N in O and N in E and N in O):
alters.append(num)

for num in range(1000,10**5):
Alt(num)
#print(num)

print(len(alters))

for numb in alters:
if numb<9999 and numb*2 in alters: #if the number in alternating list is of 4 digits, and its double is also in the list.
print(numb)
ans+=1
print("ans is %d" %(ans))