# Enigmatic Code

Programming Enigma Puzzles

## Enigma 217: Auntie Greta’s age

From New Scientist #1363, 23rd June 1983 [link]

“You want to know how old I am?” said Auntie Greta to her two nieces. “Well, as you know, your ages (in years) have no common factor higher than 1. My age (in years) is not an exact multiple of the age of either of you, but the square of my age is exactly the average of the cubes of your ages. So I am …”

How many years old?

This puzzle brings the total number of Enigmas on the site to 700. There’s a continuous archive of puzzles from Enigma 1299 on 24th July 2004 until the final puzzle, Enigma 1780 at the end of 2013 (482 puzzles). There is also a continuous archive from the start of Enigma in February 1979, up to Enigma 217 on 23rd June 1983 (217 puzzles), along with a lone puzzle from 2000 this brings us to 700 Enigmas in total, and leaves around 1085 puzzles to source and upload.

In total just over 39% of all Enigma puzzles are now available on the site. I will continue to expand the archive as time and internet connection allow.

Note: I am waiting for a phone line to be connected at my new house, so I only have sporadic access to the internet at the moment.

[enigma217]

### 26 responses to “Enigma 217: Auntie Greta’s age”

1. Jim Randell 26 August 2014 at 1:44 pm

This Python program examines possible ages for the nieces from 1 to 100 years. It runs in 38ms.

Run: [ @repl.it ]

```from enigma import irange, gcd, is_square, printf

# consider the ages of the nieces a < b
for b in irange(1, 100):
for a in irange(1, b):
# nieces ages are co-prime
if gcd(a, b) != 1: continue
# aunts age squared is the average of the cubes
(g2, r) = divmod(a ** 3 + b ** 3, 2)
if r > 0: continue
g = is_square(g2)
if g is None: continue
# and not divisible by either of the nieces ages
if g % a == 0 or g % b == 0: continue

printf("g={g} [a={a} b={b}]")
```

Solution: Auntie Greta is 42 years old.

The nieces are aged 11 and 13 years old.

There are further solutions for higher numbers. The next solution would be when the Aunt is aged 2387 and the nieces are aged 17 and 225, but this seems impractical.

This puzzle will be pleasing to fans of “The Hitchhikers Guide to the Galaxy”.

• geoffrounce 3 January 2016 at 12:04 pm

Talking about Basic in Enigma 323, here is a Visual Basic console solution for Enigma 217. I compiled and ran it under Visual Studio 2015. I think now I have seen programming solutions on your site in Python, C++, SQL, MiniZinc, Fortran and Basic (both BBC Basic and Visual Basic now). Any more candidates?

```
Module Module1
Function gcd(ByVal a As Integer, ByVal b As Integer) As Integer
If b Mod a = 0 Then
Return a
Else
Return gcd(b, a Mod b)
End If
End Function

Sub Main()
Dim niece1, niece2, aunt As Integer
For niece1 = 1 To 100
For niece2 = 1 To 100
If gcd(niece1, niece2) <> 1 Then Continue For
For aunt = 1 To 100
If aunt Mod niece1 = 0 Or aunt Mod niece2 = 0 Then Continue For
If (niece1 ^ 3 + niece2 ^ 3) = 2 * aunt ^ 2 Then
Console.WriteLine("Aunt's age is " & aunt)
Console.WriteLine("Nieces' ages are " & niece1 & " and " & niece2)
End If
Next aunt
Next niece2
Next niece1
End Sub
End Module

' Output
' Aunt's age is 42
' Nieces' ages are 11 and 13
```
• Jim Randell 4 January 2016 at 8:42 am

When I started coding programmed solutions to Enigma puzzles (Enigma 1361) I was using Perl, and I have put up quite a few Perl solutions (for most of Enigmas 15221554), although since I learned Python I have tended to prefer it for it’s compactness and readability (although it is often slightly slower). I have coded the odd Enigma puzzle in C to get a faster program, but I don’t think I’ve put any of those up. Also I did have a brief dabble with Julia for Enigma 190.

There have also been contributions to the site in Haskell (although I couldn’t get the program to run) and Mathematica (which I can run on my Raspberry Pi).

• Hugh Casement 4 January 2016 at 11:18 am

If some of those languages can be likened to an elegant suit, Basic is the comfortable jeans and an old sweater.   Easy to learn; easy to see what a program is doing (in great contrast to many!).   You don’t have to think the program out in advance or “structure” it — though I admit indenting the FOR loops does improve the readability.   You can have lots of lovely GOTOs, and Turbo Basic also has an EXIT FOR instruction to leave a loop prematurely without messing up the stack pointer.  As Geoff has shown, it doesn’t have to be spaghetti code.   Altogether it suits me fine.   I just wish for a bit more speed sometimes, for example with my sledgehammer approach to some of these puzzles involving numerous nested loops.

• Jim Randell 8 January 2016 at 10:12 am

Here are a few other sites I found that have collected programmatic solutions to Enigma puzzles.

[ http://charlespetzold.com/etc/enigma/index.html ] – by Charles Petzold, mostly in C.
[ https://sites.google.com/site/jzapal/fortran_enigma ] – by Jan Zapal, mostly in Fortran 95.
[ http://www.hakank.org/minizinc/ ] – by Hakan Kjellerstrand, Enigma puzzles and more, mostly in MiniZinc.

• geoffrounce 10 January 2016 at 1:37 pm

Here are solutions in the C# and C languages:

```// ****************************C Sharp ************** ******************
using System;

namespace Enigma_217_cs
{
public class GrossCD
{
static int GCD(int a, int b)
{
int Remainder;

while (b != 0)
{
Remainder = a % b;
a = b;
b = Remainder;
}
return a;
}

class Program
{
static void Main(string[] args)
{
int n1, n2, a;
for (n1 = 1; n1 < 100; n1++)
{
for (n2 = 1; n2 < 100; n2++)
{
if (GCD(n1, n2) != 1) continue;
for (a = 1; a < 100; a++)
{
if (a % n1 == 0 || a % n2 == 0) continue;
if (n1 * n1 * n1 + n2 * n2 * n2 == 2 * a * a)
{
Console.WriteLine(" Aunt's age is {0}", a);
Console.WriteLine(" Nieces ages are {0} and {1}", n1, n2);
Console.ReadLine();  // to freeze console screen
}
}
}
}
}
}
}
}

// code gives aunts age of 42 and nieces ages as 11 and 13
```
```// ********************************** C Code ******************************
#include <stdio.h>

int GCD(int a, int b)
{
int Remainder;

while (b != 0)
{
Remainder = a % b;
a = b;
b = Remainder;
}
return a;
}

int main() {

int a, n1, n2, result;

for ( n1 = 1; n1 < 100; n1++ )  {

for ( n2 = 1; n2 < 100; n2++ )  {
{
result = GCD(n1,n2);
if (GCD(n1,n2) != 1) continue;
}

for ( a = 1; a < 100; a++ )   {
if (a % n1 == 0 || a % n2 == 0 ) {
continue;
}

if (n1*n1*n1 + n2*n2*n2 == 2 * a * a ) {
printf("\n Aunt 's age is : %d", a);
printf("\n Nieces' ages are : %d and %d", n1, n2);
}
}   // a loop
}   // n2 loop
}   // n1 loop
return 0;
}

/*
Output
-----
Aunt 's age is : 42
Nieces' ages are : 11 and 13
Aunt 's age is : 42
Nieces' ages are : 13 and 11
--------------------------------
Process exited after 0.02283 seconds with return value 0
*/
```
• Jim Randell 2 February 2016 at 11:07 am

In Enigma 1182 I wrote the coprime_pairs() function to generate coprime pairs. I’ve added a variation of the code to the enigma.py library, so with the latest version you can solve this puzzle with the following Python code:

Run: [ @repl.it ]

```from enigma import coprime_pairs, is_square, printf

# consider the ages of the nieces a < b
for (a, b) in coprime_pairs(order=1):
if a % 2 != b % 2: continue
# aunts age squared is the average of the cubes
g = is_square((a ** 3 + b ** 3) // 2)
if g is None: continue
# and not divisible by either of the nieces ages
if g % a == 0 or g % b == 0: continue

printf("g={g} [a={a} b={b}]")
break # remove this to find more solutions
```
2. Naim Uygun 26 August 2014 at 3:10 pm
```def asal(n):
if n in [2,3,5,7]: return True
lim=int(n**0.5)+2
for i in range(2,n):
if n%i==0: return  False
return True

from itertools import permutations
for y in range(2,140):
for a, b in permutations(range(2,y),2):
if asal(a)== True and asal(b)==True:
if (a**3+b**3)/2 == y**2 :
print("Auntie Gretay's age =",y," Nieces' ages =",a," and ",b)
break
```
• saracogluahmet 29 August 2014 at 12:05 pm

In asal function, what is the purpose of lim variable? it is not used, I guess it is for the end value of the loop.Even if it was meant for the loop, the function would still be inefficient.

• saracogluahmet 30 August 2014 at 7:24 am

How do you know that two numbers are prime? They have no common factor except 1, then gcd(a,b)=1, for example let a=5 and then b=12, so b is not a prime number. This means asal function lacks of checking some numbers as I showed.

• mahmut 30 August 2014 at 12:20 pm

[deleted]

• saracogluahmet 30 August 2014 at 3:50 pm

My intention is to correct the mistake, there is a big mistake in the code,the numbers share a common factor and that is 1. I could not see the code related to it. What about the lim variable? What is the purpose of it? In my opinion, You should talk about the code, not about me. The subject is the code itself, not me! Mahmut

• saracogluahmet 30 August 2014 at 4:00 pm

I like to explain a little more in case you do understand, Mahmut. Do check these numbers for example, 8 and 15. Are they prime numbers? They share only a common factor and it is 1. So, asal function is not sufficient since it does not check these numbers, instead there should be a greatest common divisor function. Shorty, gcd(a,b).

Apart from this, The asal function does not seem efficient.

• Jim Randell 5 February 2015 at 11:13 pm

I am going to edit or remove comments in this thread that don’t add to the discussion of this particular puzzle.

Naim: As Ahmet points out the program you have posted solves a variation on the puzzle where the ages of the nieces are both prime (which does ensure that they have no common factor greater than 1, but is not a necessary condition). It turns out that in the solution the nieces ages happen to both the prime, so your program does find the right answer. It also doesn’t seem to check the condition that the aunt’s age is not an exact multiple of either of the nieces ages.

If you want to post an amended version of your code please feel free to.

• Naim Uygun 30 May 2016 at 12:22 pm

Therefore, here is my amended version of my code:

```def gcd(x, y):
return gcd(y, x % y) if y else x

from itertools import combinations
for y in range(2,140):
for a, b in combinations(range(2,y),2):
if gcd(a,b) == 1 and (a**3+b**3)*0.5 == y**2 and y%a != 0 and y%b != 0:
print("Auntie Greta's age =",y," Nieces' ages =",a," and ",b)
#break
```
3. arthurvause 27 August 2014 at 2:51 pm
```from itertools import combinations
from fractions import gcd

#oldest verified age is 122
squares = {x*x:x for x in xrange(123)}

# Nieces' ages must be odd, different and greater than 1
# If Greta's age as a multiple of a niece's, the nieces ages are not coprime
for m,n in combinations(range(3,123,2),2):
if (m**3+n**3)/2 in squares and gcd(m,n)==1:
print "Aunt Greta's age is",squares[(m**3+n**3)/2], ", nieces ages are",m,n
```
4. Paul 27 August 2014 at 3:44 pm

MMA code

```b=Flatten[Cases[Tuples[Range[2,100],{2}],{a_,b_}/;GCD[a,b]==1&&IntegerQ[c=Sqrt[(a^3+b^3)/2]]&&a<b]];
Print["Aunt Greta's age is ",Sqrt[(b[[1]]^3+b[[2]]^3)/2]," and the Nieces ages are ",b[[1]]," and ",b[[2]]]  //Timing```

Aunt Greta's age is 42 and the Nieces ages are 11 and 13
To fast to record any time.
Paul Cleary.

5. saracogluahmet 1 September 2014 at 11:46 am
```def gcd(a,b):

while b!=0:
c=a%b
a=b
b=c

return a

def Solve():
for a in range(1,100):
for b in range(a+1,100):
if gcd(a,b)==1:#"This is for ................have no common factor higher than 1."
c=((a**3+b**3)/2)**0.5 #This is for ......"square of my age is exactly the average of the cubes of your ages
C=int(c)
if C==c:
logical=(C%a)&(C%b) # This is for .......exact multiple of the age of either of you"
if logical!=0:
print(a,b,c)
Solve()
```
• saracogluahmet 1 September 2014 at 12:21 pm

The same algorithm using C++,

```#include <iostream>
#include <cmath>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int gcd(int A, int B){
int c=0;
while (B!=0){
c=(A)%(B);
A=B;
B=c;

}

return A;
}

int main(int argc, char** argv) {
float d,C,total;
for (int a=1;a<100;a++){
for (int b=1;b<100;b++){

if (gcd(a,b)==1){
total=(pow(a,3)+pow(b,3))/2;
C=pow(total,0.5);

if (int(C)==C){
d=(int(C)%a)&(int(C)%b);
if (d!=0)
std::cout<<a <<" "<< b << " "<< C<< "\r\n";
}

}

}
}

return 0;
}
```
6. geoffrounce 22 September 2016 at 12:12 pm

The MiniZinc solution below did not need the following condition in the puzzle to get the answer:

“Well, as you know, your ages (in years) have no common factor higher than 1.

```% A Solution in MiniZinc
include "globals.mzn";

var 1..100 : n1;
var 1..100 : n2;
var 1..100 : a;

constraint a mod n1 !=0 /\ a mod n2 != 0;

constraint (n1*n1*n1) + (n2*n2*n2) == 2*a*a;

solve satisfy;

output ["Aunt's age is ", show(a) ++ "\n" ++
"Nieces' ages are ",show(n1)," and ", show(n2)];

% Aunt's age is 42
% Nieces' ages are 13 and 11
% Finished in 82msec
```
• Jim Randell 22 September 2016 at 2:46 pm

Without the condition that the nieces ages are co-prime the next possible solution is:

g=336, a=44, b=52

which are reasonable ages for the nieces, but probably not for Auntie Greta.

With the condition the next possible solution is:

g=2387, a=17, b=225