Investigating Happy Numbers
Happy numbers are found by finding the sum of the squares of the digits of the initial test number.
A number is happy if after iteration the sequence terminates on 1
13→1+9=10→1+0=1
So 13 is a happy number because it reaches 1.
2→4→16→37→58→89→145→42→20→4
So 2 is an unhappy number because it ends in a loop.
logolists Help with LOGO list commands.
Logo Investigation | |||
Sum of the squares of the digitsThis procedure calculates the sum of the squares of the digits of a number.
to digitsqsum :n
-count :n counts the number of digits in :n and uses it to control a repeat loop. This is called and tested using the command.
show digitsqsum 13 |
Sum ChainThis procedure generates the chain of sums from a given start number.
to digitsqchain :n
-:n is entered as the first link in the chain This is called and tested using the command.
show digitsqchain 13 |
||
Searching for Happy Numbers.This procedure searches up to a given limit.
to digitsqsearch :limit
-masterlist contains all the numbers (happy or sad) which have appeared in any chain. This is called and tested using the command.
show digitsqsearch 10 |
Further InvestigationsHow many unhappy loops are there? Write a procedure to find all the numbers which belong to the happy or sad chains. What happens for cubes or other powers higher than 2? |
Last modified 29th May 2010