Enigmatic Code

Programming Enigma Puzzles

Enigma 529: Statistical sickness

From New Scientist #1681, 9th September 1989 [link]

Only eight of our statistics students have handed in sick-leave certificates this year — which, if averaged over all our complement, would amount to precisely one day’s illness per student.

All absences were of different lengths, of between 1 and 365 days duration, with none having two consecutive digits the same, and none ending in a zero. An odd coincidence was that these eight absences were such that they split into four pairs, each member of a pair being the same as its companion with the digits reversed. But what is really strange is that these four pairs shared a further interesting property. The square of each absence was also the digit-reversal of the square of its companion.

How many students did we have this year?

[enigma529]

2 responses to “Enigma 529: Statistical sickness

  1. Jim Randell 9 December 2019 at 8:48 am

    This Python program runs in 87ms.

    Run: [ @repl.it ]

    from enigma import irange, nsplit, tuples, nconcat, nreverse, subsets, printf
    
    # collect pairs (r, n) pairs
    pairs = list()
    
    # consider values for n
    for n in irange(1, 365):
      # split it into digits
      ds = nsplit(n)
      # cannot end in 0
      if ds[-1] == 0: continue
      # no two consecutive digits are the same
      if any(x == y for (x, y) in tuples(ds, 2)): continue
      # reverse the digits
      rs = ds[::-1]
      r = nconcat(rs)
      # reverse must be less
      if not(r < n): continue
      # n^2 is the reverse of r^2
      if n * n != nreverse(r * r): continue
    
      printf("[{n} -> {r}]")
      pairs.append((r, n))
    
    # choose 4 pairs
    for ps in subsets(pairs, size=4):
      # find the total number of sick days
      t = sum(n + r for (n, r) in pairs)
      # output solution
      printf("total = {t} [pairs = {ps}]")
    

    Solution: There were 784 students.

    The four pairs (and their squares) are:

    (12, 21) → (144, 441)
    (13, 31) → (169, 961)
    (102, 201) → (10404, 40401)
    (103, 301) → (10609, 90601)

  2. GeoffR 9 December 2019 at 10:29 pm
    from enigma import nreverse
    
    nlist = []
    students = 0
    
    for n in range(12,366):
      nr = nreverse(n)
    
      if n < 100: 
        if nr == n: continue
        
      if n >= 100:
        # check all 3 digits are different
        a, b, c = n // 100, n // 10 % 10, n % 10
        if a == b or a == c or b == c: continue
    
      # Form square of number and square of its reverse  
      n_sq = n * n
      nr_sq = nr * nr
    
      # Check square of number and square of its reverse are equal
      if str(nr_sq) == str(n_sq)[::-1]:
        s = set((n, nr))
        if s not in nlist:
          students += n + nr
          nlist.append((s))
    
    # Check there are four pairs of numbers
    if len(nlist) == 4:
      print('Number of students = ', students)
      print('Four pairs are ', nlist)
    
    # Number of students = 784
    # Four pairs are [{12, 21}, {13, 31}, {201, 102}, {301, 103}]
    
    
    

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: