Header tag

Friday, 21 February 2025

Calculator Games: Pythagorean Triples

 Pythagorean Triples

The Calculator Fun and Games book probably didn’t cover Pythagorean Triples because back in the mid 80s, pocket calculators didn’t have an x2 button.  Also, I didn’t start studying trigonometry and geometry until the late 80s, five or so years after I had the book, so the lack of an x2 button on my calculator didn’t even bother me.  The square root button was exciting enough, although it was a little boring because if you hammered it enough it always led to an answer of 1.

However, the concept still holds and it’s possible to study Pythagorean Triples with a basic calculator and a notepad and pen (although a scientific calculator will be helpful).

Pythagoras once decreed that for a right-angled triangle, a2 + b2 = h2.  The square of the hypotenuse, h, is equal to the sum of the squares of the other two sides.  The hypotenuse is the side of the triangle which is opposite the right angle.  It's not shown on the diagram below, because I couldn't work out how to add it to the picture ;-)  This is a key result in geometry.



Furthermore, we can find an easy solution to a2 + b2 = h2 since 32 + 42 = 52 (9 + 16 = 25).  Integer values of a, b and h (sometimes called c) are called Pythagorean triples.

The challenge is:  can you find any more?

Since Pythagoras did his maths thousands of years ago, many people have studied his work and found plenty more triples.  To list a few:

(3, 4, 5)

32+42=9+16=25=52

(5, 12, 13)

52+122=25+144=169=132

(7, 24, 25)

72+242=49+576=625=252

(8, 15, 17)

82+152 = 64+225 = 289 =172

(9, 12, 15)

92+122=81+144=225=152

Others include (20, 21, 29) and (12, 35, 37). 



GENERATING PYTHAGOREAN TRIPLES

It’s possible to generate a set of triples using Euclid's formulae:

a = m2−n2

b = 2mn

c = m2+n2

where m and n are positive integers.  For example, m = 4 and n = 2 gives:

a = 12
b = 16
c = 20

Which is a scaled-up version of the 3,4,5 triple (each number is 4* larger than the 3,4,5 triple).  This isn’t a unique triple, it’s just one that’s four times larger than an existing one.

Some observations of Pythagorean Triples:

It is not possible to have a Pythagorean triple that contains all prime numbers. 

At least one of the numbers must be even (b = 2mn).  m = 2 and n=1 yields 3,4,5.

There are an infinite number of Pythagorean triples (since m and n can have any values).  Many of these are duplicates of other triples, just scaled up (for example, 12,16,20 is a scaled up version of 3,4,5).  

So I’m going to search for ‘unique’ triples that aren’t just multiples of smaller ones, and for this, I suspect that m and n must not have common factors.  For example, m = 4 and n = 2 can be simplified to m = 2 and n = 1 (divide by two), which is how (12,16,20) is related to (3,4,5).  (Wider reading on Pythagorean triples led me to discover that the accepted term is not ‘unique’ but ‘primitive’ triples.  You get the idea.  I have reworded the rest of my article to call them primitive, in line with the rest of the mathematical world).

m = 5, n = 1 gives (10,24,26) or (5,12,13) – which brings me to another observation: 

Primitive Pythagorean triples generally contain at least one prime number. 

In my list above, I gave (9,12,15) as an example, but that’s just another version of (3,4,5), in this case, scaled up by 3.  By definition,(and I know I haven’t defined them very well) primitive triples must contain three numbers that have no common factors that would enable them to be simplified.  Prime numbers guarantee this, but are not essential – for example, (16, 63, 65) is a triple with no primes.

An easy way to generate primitive triples is to use the m, n method, setting m to an even number and n to 1.  This means that a = m2−n2, and c = m2+n2 will only have a difference of two.

m

n

a

b

c

a2 + b2

c2

2

1

3

4

5

25

25

4

1

15

8

17

289

289

6

1

35

12

37

1369

1369

8

1

63

16

65

4225

4225

10

1

99

20

101

10201

10201

12

1

143

24

145

21025

21025

14

1

195

28

197

38809

38809

16

1

255

32

257

66049

66049

18

1

323

36

325

105625

105625

20

1

399

40

401

160801

160801

22

1

483

44

485

235225

235225

24

1

575

48

577

332929

332929

26

1

675

52

677

458329

458329

28

1

783

56

785

616225

616225

30

1

899

60

901

811801

811801

Prime numbers highlighted in bold.  Rows of green triples contain no prime numbers.

So: to continue our investigation: what happens if we set n = 2?  Do we still get an abundance of primitive triples?  We do, but only when m is odd.  When m is even and n=2, the values of a,b,c are all divisible by 4 (not shown below).  When m is divisible by 4, then a,b,c are all divisible by 8.

Here are the results for n=2 and m is odd: a series of primitive triples, some containing primes.

m

n

a

b

c

a2 + b2

c2

3

2

5

12

13

169

169

5

2

21

20

29

841

841

7

2

45

28

53

2809

2809

9

2

77

36

85

7225

7225

11

2

117

44

125

15625

15625

13

2

165

52

173

29929

29929

15

2

221

60

229

52441

52441

17

2

285

68

293

85849

85849

19

2

357

76

365

133225

133225

21

2

437

84

445

198025

198025

23

2

525

92

533

284089

284089

Prime numbers highlighted in bold.  Rows of green triples contain no prime numbers.

When n=3, we can generate primitive triples when m is an even number that is not divisible by 3 (m = 4, 8, 10, 14, 16, 20…).  When m and n are both odd, then a,b,c are divisible by 2 (unless m is divisible by 3, when a,b,c are all divisible by 9).  This leads to a complicated repeating pattern, which is easier to show than explain: 

m

n

a

b

c

Notes

4

3

7

24

25

Primitive

5

3

16

30

34

m,n both odd, a,b,c divisible by 2

6

3

27

36

45

 multiples of 3, all divisible by 9

7

3

40

42

58

m,n both odd, a,b,c divisible by 2

8

3

55

48

73

Primitive

9

3

72

54

90

m,n, odd and divisible by 3, a,b,c divisible by 18

10

3

91

60

109

Primitive

11

3

112

66

130

m,n both odd, a,b,c divisible by 2

12

3

135

72

153

 multiples of 3, all divisible by 9

13

3

160

78

178

m,n both odd, a,b,c divisible by 2

14

3

187

84

205

Primitive

15

3

216

90

234

m,n, odd and divisible by 3, a,b,c divisible by 18

16

3

247

96

265

Primitive

17

3

280

102

298

m,n both odd, a,b,c divisible by 2

18

3

315

108

333

 multiples of 3, all divisible by 9

19

3

352

114

370

m,n both odd, a,b,c divisible by 2

20

3

391

120

409

Primitive

21

3

432

126

450

m,n, odd and divisible by 3, a,b,c divisible by 18

22

3

475

132

493

Primitive

23

3

520

138

538

m,n both odd, a,b,c divisible by 2

24

3

567

144

585

 multiples of 3, all divisible by 9

25

3

616

150

634

m,n both odd, a,b,c divisible by 2

26

3

667

156

685

Primitive


The investigation (and the pattern-finding) continues, as we try n=4.  Here’s the result – it’s possible to find primitive Pythagorean triples for every case where m = odd again. 

m

n

a

b

c

Notes

5

4

9

40

41

Primitive

6

4

20

48

52

m,n both even: a,b,c divisible by 4

7

4

33

56

65

Primitive

8

4

48

64

80

m,n both divisible by 4, a,b,c divisible by 16

9

4

65

72

97

Primitive

10

4

84

80

116

m,n both even: a,b,c divisible by 4

11

4

105

88

137

Primitive

12

4

128

96

160

m,n both divisible by 4, a,b,c divisible by 32

13

4

153

104

185

Primitive

14

4

180

112

212

m,n both even: a,b,c divisible by 4

15

4

209

120

241

Primitive

16

4

240

128

272

m,n both divisible by 4, a,b,c divisible by 16

17

4

273

136

305

Primitive

18

4

308

144

340

m,n both even: a,b,c divisible by 4

19

4

345

152

377

Primitive

20

4

384

160

416

m,n both divisible by 4, a,b,c divisible by 32


We find that m and n must have no common factors in order to generate primitive triples.  This means m and n must be ‘relatively prime’ (an expression I learned during my wider reading into this project).  

This is starting to go beyond the scope of a calculator, and into spreadsheet territory, but it's still fascinating, and can still be done with a simple pocket calculator (or an app on your phone, naturally), even without an x2 button.

Summary of Pythagorean Triples:

It is not possible to have a Pythagorean triple that contains all prime numbers

It is possible to have a primitive Pythagorean triple that contains no prime numbers; a, b and c will be relatively prime, and these are generally generated by m and n being relatively prime too.

There are an infinite number of triples, and an infinite number of primitive triples

Monday, 17 February 2025

Calculator Games: Reverse Digits and Subtract

REVERSING DIGITS: IS THERE A PATTERN?

This one comes from CoPilot, but is again in the style of Calculator Fun and Games, and is treated slightly differently, as "One Thousand and Eighty Nine" in the book.


Copilot:
Pick a number and try to form a specific pattern using the digits, for example, “Change 123 to 321 using only addition and subtraction.”

Book:
Take a three digit number, and reverse the digits.  Subtract the smaller from the larger.  Then, take the result of this subtraction, and add it to its digits reversed; you will always get 1089.

The Copilot idea is not a perfect suggestion for a game (why subtract when you could just add a smaller number?) but it has potential for an investigation.

Let’s take the example as written:  change 123 into 321.

321-123 = 198

This means that we can change 123 into 321 just by adding 198.  And 1
98, for those who’ve been reading my series on calculator fun and games, immediately stands out as a multiple of 9.  The digits sum to 18, which is a multiple of 9 (and its digits sum to 9 as well).


Let’s try another number and see if this is a fluke:  Let’s try 456 and 654.
654-456 = 198

And another, with non-consecutive digits:

781 – 187 = 594, also a multiple of 9 (5 + 9 +4 =18)

In fact, let’s try a whole range of random numbers, with reversed digits, making sure to subtract the smaller of the pair from the larger.

872 – 278 = 594 (reversed = 495, and 495 + 594 = 1089)
512 – 215 = 297 (reversed = 792, and 792 + 297 = 1089)
503 – 305 = 198 
(reversed = 198, and 198 + 891 = 1089)
902 – 209 = 693 (reversed = 396, and 396 + 693 = 1089)

There's a full solution to the 1089 phenomenon online (there are many; and here's another that's simpler to follow but less detailed); I won't add anything further to it here.

And four-digit numbers:
8112 – 2118 = 5994 
7205 – 5027 = 2178
(if you reverse and sum these results, you get 10989 and 10890; there isn't a unique answer).

And five-digit numbers:
98765 – 56789 = 41976  (41976 / 9 = 4664, in case you were wondering, as I was)
85127 – 72158 = 12969  (also divisible by nine; 12969 / 9  = 1441)

Why do we always get 1089?

Well, let's take a four-digit number ABCD, where A, BC,and are its digits. The value of the number can be expressed as 

1000A+100B+10C+D.

When you reverse the digits, the number becomes dcba, which can be expressed as:
.

Now, when we subtract the smaller number from the larger, we get (assuming ABCD is the larger; it works either way)

  • The difference is: (1000A+100B+10C+D)(1000D+100C+10B+A)
  • Simplifying this, we get: 999A+90B90C999D
  • Factoring out common terms, we get: 999(AD)+90(BC)
  • Notice that both 999 and 90 are multiples of 9.
  • Therefore, the entire expression 999(AD)+90(BC) is a multiple of 9.

And since the result is a multiple of 9, it shows why the result of the subtraction is always a multiple of nine (for four-digit numbers).  The same logic can be applied to three-digit numbers (where it's easier), or to numbers with more digits too. 

In the case of three digits, the result will be simpler.  For a number abc and its reverse, cba, the b will cancel and the result is always be 9 * (11(a-c)) or 99(a-c).

And it stands as a useful little trick to show on a calculator!

Previous Calculator Fun and Games articles:
Snakes and Ladders (Collatz Conjecture)
Crafty Calculator Calculations (numerical anagrams, five digits)
More Multiplications (numerical anagrams, four digits)
Over and Out (reduce large numbers to zero in as few steps as possible)

Thursday, 13 February 2025

Calculator Fun and Games 4: Sums of Digits and Multiples of 9

This post isn't taken directly from Calculator Fun and Games, but it is definitely a puzzle of the sort that Ben Hamilton could have included in his book.  It's very straightforward, and can be demonstrated easily with some examples.



One (or more) players each write down a six- (or seven-, or eight-) digit number on a piece of paper.  Using the calculator, they sum the digits and write down the total.  They then sum those digits (using the calculator), and repeat until they have a single-digit answer.  Largest number wins.

Let's try an example: I'll start with 987654 (regular readers will recognise this from my previous blog post on Calculator Fun and Games, called Over and Out).

987654

9 + 8 + 7 + 6 + 5 + 4 = 39
3 + 9 = 12
1 + 2 = 3

My opponent tries a different approach:

123456

1 + 2 + 3 + 4 + 5 + 6 = 21
2 + 1 = 3

So it's a tie.

Time for a tie-breaker:  I go completely random and punch six digits into my calculator:

946305

9 + 4 + 6 + 3 + 0 + 5 = 27
2 + 7 = 9

My opponent needs to find something special just to draw this round:

813524

8 + 1 + 3 + 5 + 2 + 4 = 23
2 + 3 = 5 

So I win, because 9 is greater than 5.

Analysis

The result for any random number is largely random.  You can't identify the final sum-of-digits just by looking at a number, especially for numbers where the first sum of digits is going to be more than 9.

However, there is a definite pattern in the sums of digits of sequences of numbers which is probably not surprising if you think about it (or use a spreadsheet, as I did to analyse some long sequences of numbers).


The second sum - i.e. the final iteration of the summing - follows a very simple pattern: 1-9, then 1-9 over and over again.  This extends across hundreds (from 899 to 900) and by the same process, extends across thousands (1999 - 2000) and so on.  This means that each digit from 1 to 9 is represented equally as the final sum.  The probability of ending up with a 1 or a 7 or an 8 is equal - each digit has a probability of 1/9 of being the final sum.

And providing you don't memorise a number (or a collection of numbers) which sum to a final answer of 9, this makes it an interesting and fun game for young calculator users.

Not that there are many of them around these days! :-)

Note on the spreadsheet:

To break down a three-digit number to its individual digits (which can then be summed), I used these formula.  Place the three-digit number in cell A1.

Hundreds:  
B1 = INT((A1 - MOD(A1, 100)) / 100)
Takes the remainder of dividing A1 by 100 and subtracts this from A1, then divides this by 100 to provide a digit.

Tens:
C1 = INT((A1 - MOD(A1, 10)) / 10) - 10 * INT(A1 / 100)
Divides A1 by 10 to give a two-digit integer, then subtracts 10 times the hundred digit.  The trickiest of the calculations.

Units:
D1 = MOD(A1, 10)
yields the remainder when A1 is divided by 10 - i.e. the units

Further extension:  asking an AI bot for a series of maths puzzles that can be solved with a calculator, it replied with this:

Magic Number Game:

  • Choose a number, multiply it by 9, and then add the digits together until you get a single digit. This digit will always be 9. Try it with different numbers and see if it works.

So:  is it true that all multiples of 9 eventually sum to 9?

I have known for a long time that the first sum of multiples of 9 is always 9, and can explain why intuitively, but not comprehensively.

1 * 9 = 9
2 * 9 = 9 + (10 - 1) which means that the tens column is incremented by 1, and the units column is reduce by 1.  Therefore the sum of the tens and units will remain unchanged at 9.

9
18
27
36

and so on.

However, this breaks down after reaching 90
9 * 11 = 99
--> 9 + 9 = 18
--> 1 + 8 = 9

...and further iterations of the sums of digits are needed.  I've known this as a useful way of determining if a number is a multiple of 9, but I can't prove why it's always the case except by demonstrating it.  And my A-level mathematics teacher told me that demonstrating for some cases is hardly a proof for all. 

More mathematically advanced brains than mine have already shown why the sums of digits of multiples of nine are always nine - I won't pretend to understand it, but it has been done. 

Further reading


Other Calculator Fun and Games articles:

Snakes and Ladders (Collatz Conjecture)
Crafty Calculator Calculations (numerical anagrams, five digits)
More Multiplications (numerical anagrams, four digits)
Over and Out (reduce large numbers to zero in as few steps as possible)


Sunday, 9 February 2025

Calculator Fun and Games 3: Over and Out

Calculator Fun and Games 3:  Over and Out

This visit to Calculator Fun and Games is one of my favourites, and features a game that I’ve played more than any of the others in the book (even the ‘spell words with your calculator’ game).  It’s called Over and Out, and has a loose cricket theme.  

The rules are simple:  you start with a six-digit number (no zeroes allowed, no duplicated digits), and the aim is to get it down to zero in as few steps as possible, and the target is six (the number of balls in an over in a game of cricket).  The steps are addition, subtraction, multiplication and division, and you can add, subtract, multiply or divide by any one-digit or two-digit numbers.


The example in the book goes like this:

583621

1)       583621 + 19 = 583640

2)       583640 / 40 = 14591

3)       14591 – 11 = 14580

4)       14580 / 45 = 324

5)       324 / 12 = 27

6)       27-27 = 0

Mission accomplished, game over!  583621 is reduced to zero in six steps.

The hints in the book include generally working on subtracting and dividing, but adding or multiplying where necessary to give you a change to divide by a larger number – as we saw in the example here, the first step of adding 19 enables us to divide by 40 in the second step.

The largest six-digit number that has no zeroes or duplicates is 987654 and it presents a challenge!  Here’s how I would have approached it as a young child – the ‘adult’ mathematician will appear later.

987654

1)       It’s even, so I can divide by 2.  And why not?
987654 / 2 = 493827 which is odd, so I can’t divide by 2 again.

2)       493827 – 27 = 493800 which I can divide by 50, or even 80.  I’ll go with 50 for now (because I’m thinking as a child, and I'm not sure how old I would have been when I realized I could divide by 80).

3)       493800 / 50 = 9876  Now we’re talking!  Getting our number down to four digits so quickly puts us in a really good place.  I can see that we can divide this number by four, so that’s my next step.

4)       493800 / 4 = 2469  isn’t divisible by 2, 3 or 5, so I’m struggling to find an easy way down.  I’ll subtract the 69 and it should be easier from there.

5)       2469 – 69 = 2400.  Still quite high, but divisible by 80.

6)       2400 / 80 = 30

7)       30-30 = 0

So it took me seven steps, although I was starting from the highest six-digit number allowed in the game.

Over the years, I’ve played this game quite often when I have a spare minute and a calculator.  In fact, I got so advanced with it, that I started using a spreadsheet to keep track of my results (although I’ve since lost it).  Then, just for good measure, I wrote a macro that would work out the optimal route down from any digit to zero.  It works on one simple principle:  the fastest way down is to divide.  Find the largest factor for any number, and divide by it.  If it’s prime (or its lowest factor is higher than 99) then calculate the highest multiple of 99 below it, and subtract down to it.


Let’s take 987654 as our example, and go through how the macro worked:

1)       987654 – highest factor below 99 is 97.  Divide by 97.
(to determine this, the macro divides 987654 by every number between 2 and 99 and checks if there is a remainder after the division.  If there’s no remainder, then store the number in a variable.  The largest value for that variable after checking all the numbers is the one to use.  In other words, check if (987654/x) = INT( 987654/x).
987654 / 97 = 10182

2)       10182 – highest factor is 6.  Divide by 6.
10182 / 6 = 1697

3)       1697 is prime.  Highest multiple of 99 below 1697 is 17 * 99 = 1683, so we will subtract 14.
(to calculate this: round down the value of (1697/99) to the nearest integer).
1697 – 14 = 1683

4)       1683 is divisible by 99, we set it up that way.
1683 / 99 = 17

5)       17 – 17 = 0

The macro is brutally strong, and took the highest possible value for the game (987654) to zero in five steps.

So let’s change the rules of the game, and look at seven-digit numbers.  Can the macro crunch a seven-digit number down to zero in six steps?  I’ll go first, as an adult mathematician, using every trick I can think of (without using a spreadsheet!).

9876543

1)       9876543 + 57 = 9876600
I prefer this instead of subtracting 43, because I know I can divide the answer by 60, whereas dividing 987500 will only divide by 50.  I’ll do a quick check on my calculator to see if I can divide by 80 (I can’t, fractions aren’t allowed), and hence divide by 60 for my next step.

2)       9876600 / 60 = 164610
So I have a six-digit number, but at least it’s low and it’s divisible by 10.  Easy next step (for the human, anyway)

3)       164610 / 10 = 16461
Now what? I can add 39 and then divide by 50, or subtract the 61 and divide by 80.  I’ll take the second option.

4)       16461 – 61 = 16400

5)       16400 / 80 = 205

6)       205 is visibly divisible by 5, so I’ll do that, get down to less than 100 and subtract what’s left.
205 / 5 = 41

7)       41 – 41 = 0

Seven steps for a seven-digit number seems good to me!  Let’s see what the macro would do:

9876543

1)       Largest factor below 100 is 3.
9876543 / 3 = 3292181 which is prime.

2)       Since 3292181 is prime, find the multiple of 99 which is below it:  33254 * 99 = 3292146.  Second step is to subtract down to this number:
3292181 – 35 = 3292146

3)       This is now divisible by 99:
3292146 / 99 = 33254 (at this stage, the macro is at around twice the value I was at; I had 16461 after my third step).

4)       33254 is divisible by 26:
33254 / 26 = 1279

5)       1279 is prime, so find a multiple of 99 nearby. 12 * 99 = 1188, so I need to subtract down to 1188.
1279 – 91 = 1188

6)       1188 / 99 = 12

7)       12 – 12 = 0

So even my macro needs to take seven steps with this seven-digit number.

Looking further, and with some assistance, I can see that I could improve the human answer.  I reached 164610 after two steps, and some mathematical assistance shows that this is actually divisible by 90 and by 93.  Dividing by 90 gives me 1829, while dividing by 93 gives me 1770.  Let’s follow this path – I’ll recap first:

1)       9876543 + 57 = 9876600

2)       9876600 / 60 = 164610

3)       164610 / 93 = 1770

4)       I can see by eye that 1770 is divisible by 10, but it’s also divisible by 59. 
1770 / 59 = 30

5)       30 – 30 = 0

So my brute-force macro (which I programmed, I’ll take the blame/credit for it) took seven steps by trying to divide by 99 each time, whereas another path can reach the end in five steps.  The macro has a short-sighted strategy – what’s the best ‘next step’ - compared to trying to find a number like 9876600 which can be divided by 60, 93 and 59 to get down to less than 100 in three further steps (in fact 9876600 has 192 factors and 30 of them are below 100, including 1). I will hold my hands up and confess that this was a fluke, I didn’t plan to find such a ‘good’ number with so many factors. 

A follow-up to the macro coding (which I have not pursued) is to take the initial number and work out how to operate on it to get the most factors below 100.  9876600 has 192 factors; 9876500 has only 24, the best next step would be to divide by 50:

1)       9876543 – 43 = 9876500

2)       9876500 / 50 = 197530

3)       197530 / 10 = 19753 which is prime.  Need to find another number within +- 99 of 19753 which has the best sequence of factors (and I need a new algorithm to do that).  For now, let’s use the ‘divide by 99’ even though we know it’s sub-optimal.
19753 / 99 = 199.5 so let’s find 200 * 99 = 19800.  Let’s add up to 19800

4)       197530 + 79 = 19800

5)       19800 / 99 = 200

6)       200 / 50 = 4

7)       4 – 4 = 0

Still stuck on seven steps!

And so you can see why I have whiled away many hours on this particular problem.  It just takes one person and a calculator (and I was playing this when calculators were a feature on watches, not portable phones) and like all good maths puzzles, can be expanded indefinitely.  I could get a spreadsheet and a macro to throw nine-digit or ten-digit numbers around, getting down to zero in minimal steps; or I could use my calculator and take the human approach, and then compare the difference!

Follow-ups are to work out the best way to treat prime numbers.  Anything else can be divided down to something smaller, but the main challenge is what to do when there’s no more dividing available.  At the moment, it follows the “divide by 99 next time”, but it might be worthwhile finding larger factors so that a series of divisions is possible instead of just 99.  That’s a challenge for another day – I’m still happy playing with my calculator and my macro!

Further Reading

An algorithm which determines odd or even to reduce a number to zero, although it only divides by two... instead of looking for the largest factor

Other Calculator Fun and Games articles:

Snakes and Ladders (Collatz Conjecture)
Crafty Calculator Calculations (numerical anagrams, five digits)
More Multiplications (numerical anagrams, four digits)