# The loneliest number...

I was reading the io9 article Why four is the nemesis of happy numbers and it got me thinking. Are there actually any cycles other than the one stated in the article? Why or why not?

As a bit of background, the article is talking about “happy” numbers. Essentially, if you repeatedly sum the square of the digits in a number and eventually reach 1, you’re a happy number. The example given in the article is 82:

828, 264 + 4 = 68
686, 836 + 64 = 100
1001, 0, 01 + 0 + 0 = 1

So 82 is in fact happy. Any number that isn’t happy is considered unhappy. Theoretically, the numbers have to either terminate at 1 or enter into a cycle, although at the moment, I don’t have a proof for that. But it’s actually even stranger than that. It turns out that (theoretically) all unhappy numbers eventually enter this cycle:

4, 16, 37, 58, 89, 145, 42, 20, 4

Why? Perhaps because eventually all numbers will be reduced to a single digit and 4 is the only one that loops.

But I was curious and I like writing scripts to test things like this. So I wrote up a bit of Scheme code (it runs just fine on Chez Scheme and should work with only minimal modifications if any on other Schemes):

; convert a number into a list of digits
(define (digits n)
(if (zero? n)
'()
(cons (mod n 10) (digits (div n 10)))))

; square a number
(define (sqr n)
(* n n))

; return either the terminal number (should be 1)
; or the point at which a cycle is detected
(define (happy n)
(let ^ ([n n] [ns '()])
(if (member n ns)
n
(^ (apply + (map sqr (digits n))) (cons n ns)))))

; go through all of the numbers, check if we have the cycle or 1
; print and exit if we find a counter example
(define (test)
(let ([cycle '(1 4 16 37 58 89 145 42 20)])
(let ^ ([i 1])
(let ([res (happy i)])
(when (not (member res cycle))
(printf "~s\t~s\n" i res)
(exit))