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 Math Olympiad, see this PDF

  • $\begingroup$ in this scenario 5000 is the maximum of possible very alternating numbers, right? $\endgroup$ – Raystafarian Feb 1 '16 at 17:57
  • 1
    $\begingroup$ the alternating number itself must be 4 digits, but the double of the alternating number may be more than that. $\endgroup$ – Xander Feb 1 '16 at 18:01
  • 1
    $\begingroup$ 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... $\endgroup$ – Darrel Hoffman Feb 1 '16 at 19:48
  • 2
    $\begingroup$ 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. $\endgroup$ – Darrel Hoffman Feb 1 '16 at 20:38
  • 2
    $\begingroup$ 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. $\endgroup$ – Darrel Hoffman Feb 1 '16 at 21:10

There number of such numbers is


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.

These two counts add to the desired answer.

  • $\begingroup$ I'm kind of lost on your explanation. I get to the LHLH but after that, maybe I'm dense? $\endgroup$ – Raystafarian Feb 1 '16 at 18:34
  • $\begingroup$ According to the comments on the OP, the double of the alternating number can be more than 4 digits. $\endgroup$ – GentlePurpleRain Feb 1 '16 at 18:36
  • 4
    $\begingroup$ @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. $\endgroup$ – f'' Feb 1 '16 at 18:44
  • $\begingroup$ @f'' yeah, but also no 5 digit numbers make the cut anyway $\endgroup$ – Raystafarian Feb 1 '16 at 18:47
  • $\begingroup$ @mikeearnest added a small bit to your answer. Feel free to roll back if you don't like it $\endgroup$ – Kevin Feb 1 '16 at 18:50

The answer is


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

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

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



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!

The answer is, as pointed out in other answers:


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


The correct answer is:


#AnubhavBalodhi, puzzling.stackexchange,26036, 1/2/16 2341 hrs


def Alt(num):
if len(N)<5: #4 digit
    if (N[0] in E and N[1] in O and N[2] in E and N[3] in O) or (N[0] in O and N[1] in E and N[2] in O and N[3] in E):
else:       #5 digit
    if (N[0] in E and N[1] in O and N[2] in E and N[3] in O and N[4] in E ) or (N[0] in O and N[1] in E and N[2] in O and N[3] in E and N[4] in O):

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


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("ans is %d" %(ans))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.