Enigmatic Code

Programming Enigma Puzzles

Enigma 1394: Squares and triangles

From New Scientist #2554, 3rd June 2006

Any number which is the sum of all the perfect squares from 1 to x² is called a square pyramidal number (such as 1, 5, 14, 30, 55). Any number which is the sum of the first x triangular numbers is called a tetrahedral number (such as 1, 4, 10, 20, 35). Within each of the following two statements digits have been consistently replaced by capital letters, different letters being used for different digits. But the two statements are entirely distinct – a letter need not have the same value in one statement as in the other.

Statement 1: ONE and FIVE are both odd square pyramidal numbers.

Statement 2: ONE is an odd tetrahedral number and FOUR is an even tetrahedral number.

Find the numbers represented by: (a) ONE and FIVE in Statement 1, and (b) ONE and FOUR in Statement 2.


This is the 500th Enigma puzzle published to the site. Hooray!

There is now complete coverage from June 2006 to the most recent puzzle (i.e. the last seven years), and also Classic puzzles from the start of Enigma in February 1979 up to August 1981. The most recent puzzle published is Enigma 1765, so this puts us at just over 28% of all Enigma puzzles ever published available on the site.


7 responses to “Enigma 1394: Squares and triangles

  1. Jim Randell 8 September 2013 at 10:08 am

    This Python program runs in 35ms.

    from collections import defaultdict
    from itertools import count
    from enigma import is_duplicate, T, printf
    # generate square pyramidal numbers
    # we accumulate odd SPs with no repeated digits
    (SP, sp) = (defaultdict(list), 0)
    for i in count(0):
      sp += i * i
      if sp > 9999: break
      if sp % 2 == 0: continue
      s = str(sp)
      if is_duplicate(s): continue
    # For part (a) we find a 3-digit SP matching ONE
    for ONE in SP[3]:
      (O, N, E) = ONE
      # and a 4-digit SP matching FIVE
      for FIVE in SP[4]:
        if FIVE[-1] != E or O in FIVE or N in FIVE: continue
        printf("(a) ONE={ONE} FIVE={FIVE}")
    # generate tetrahedral numbers
    # accumulate ones with no repeated digits, and the correct parity
    (tetra, t) = (defaultdict(list), 0)
    for i in count(0):
      t += T(i)
      if t > 9999: break
      s = str(t)
      if is_duplicate(s): continue
      if (t + len(s)) % 2 == 1: continue
    # for part (b) we need a 3-digit tetra matching ONE
    for ONE in tetra[3]:
      (O, N, E) = ONE
      # and a 4-digit tetra matching FOUR
      for FOUR in tetra[4]:
        if FOUR[1] != O or N in FOUR or E in FOUR: continue
        printf("(b) ONE={ONE} FOUR={FOUR}")

    Solution: (a) ONE = 285, FIVE = 3795; (b) ONE = 165, FOUR = 7140.

  2. Naim Uygun 8 September 2013 at 12:10 pm

    I found the answers by observing the output of this program:

    sp=[x*x for x in range(1,32)]
    st=[x*(x+1)//2 for x in range(1,40)]
    print("\nStatement 1: ONE and FIVE are both odd square pyramidal numbers")
    print("Choices for Statement 1")
    #STATEMENT 1 Answer: ONE=285, FIVE=3795
    for i in range(0,len(sp)):
        if 99<n<1000 and n%2==1:
        if 999<n<10000 and n%2==1:
            print("FIVE=", n)
    print("\nStatement 2: ONE is an odd tetrahedral number and")
    print("FOUR is an even tetrahedral number.")
    print("Choices for Statement 2")
    #STATEMENT 2 Answer: ONE=165 FOUR=7140
    for i in range(0,len(st)):
        if 99<n<1000 and n%2 != 0:
        if 999<n<10000 and n%2==0:
  3. ahmet çetinbudaklar 8 September 2013 at 1:55 pm

    Dear Jim:

    This is the most comprehensive longest list of puzzles I have ever seen in the internet so far.
    May I reiterate my sincere appreciation of your delicate work with a lot of passion and diligent attention from your end.
    We have 1765-500=1215 puzzles left!
    Best wishes to you


    • Jim Randell 9 September 2013 at 12:41 pm

      Thanks. So far I’ve been able to track down most of the puzzles I’ve published from online sources, but I estimate there are around 600 that I’ll need to find a copy of the New Scientist in question for. So I’ll be making a trip to Bristol Central Library soon to see if they can help me.

  4. geoffrounce 17 August 2017 at 10:24 pm

    This Enigma proved a bit messy in MiniZinc as I had to comment out code needed for Statement 1 to run Statement 2 as a separate programme, which the code now shows.

    % A solution in MiniZinc
    include "globals.mzn";
    var 0..9:O; var 0..9:N; var 0..9:E; var 0..9:F;
    var 0..9:U; var 0..9:R; var 0..9:I; var 0..9:V;
    constraint O != 0 /\ F !=0 /\ all_different( [O,N,E,F,U,R,I,V]);
    var 100..999: ONE = 100*O + 10*N + E;
    var 1000..9999: FOUR = 1000*F + 100*O + 10*U + R;
    %var 1000..9999: FIVE = 1000*F + 100*I + 10*V + E;
    %set of int : sq_pr3 = { (2*n*n*n + 3*n*n + n) div 6 | n in 7..13 };
    %set of int : sq_pr4 = { (2*n*n*n + 3*n*n + n) div 6 | n in 14..30 };
    set of int: tet3 = { (n*(n+1)*(n+2)) div 6 | n in 8..17};
    set of int: tet4 = { (n*(n+1)*(n+2)) div 6 | n in 18..38};
    % Statement 1
    %constraint ONE in sq_pr3 /\ ONE mod 2 == 1 ;
    %constraint FIVE in sq_pr4 /\ FIVE mod 2 == 1 ;
    % Statement 2
    constraint ONE in tet3 /\ ONE mod 2 == 1;
    constraint FOUR in tet4 /\ FOUR mod 2 == 0;
    solve satisfy;
    %output [ "ONE, FIVE = " ++ show(ONE)  ++ ", " ++ show(FIVE)];  % Statement 1
    % ONE, FIVE = 285, 3795
    output [ "ONE, FOUR = " ++ show(ONE)  ++ ", " ++ show(FOUR)];   % Statement 2
    % ONE, FOUR = 165, 7140
    % Finished in 56msec

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: