## [QUIZ] Happy Numbers (#93)

Ron Teitelbaum Ron at USMedRec.com
Fri Sep 1 18:50:09 UTC 2006

```Hey Markus,

I can't resist a good puzzle.  Please not too many, I won't get any work
done!

the programming.  I did that pretty fast it was the optimization that is
really interesting.

What I noticed was that digit order and zeros have no effect.  I figured I
could hold onto the sortedDigits results and significantly reduce the amount
of data that I needed to hold.  It worked fine but it turned out to be much
slower!  It's amazing to me that holding onto hundreds of thousands of
associations turned out to be faster then 296.  I didn't spend much time to
figure out why, but I guess it's adding the sorting that took so long, and
the speed of the dictionary lookup was just fine with hundreds of thousands
of objects!

Who knew!

The class can process 1 million numbers for happiness in 5 mins.

Ron Teitelbaum

> From: Markus Gaelli
> Sent: Friday, September 01, 2006 9:35 AM
>
> >
> > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> > =-=-=-=-=-=-=
> >
> > by Shane Emmons
> >
> > Write a program that tells whether a given integer is happy. A
> > happy number is
> > found using the following process: Take the sum of the squares of
> > its digits,
> > and continue iterating this process until it yields 1, or produces
> > an infinite
> > loop.
> >
> > For example the number 7:
> >
> > 	7^2 = 49
> > 	4^2 + 9^2 = 97
> > 	9^2 + 7^2 = 130
> > 	1^2 + 3^2 + 0^2 = 10
> > 	1^2 + 0^2 = 1
> >
> > If a number is not happy than it is obviously unhappy. Now that you
> > have this
> > program, what is the largest happy number you can find? What is the
> > happiest
> > number between 1 and 1,000,000. I define the happiest number as the
> > smallest
> > number that finds the most other happy numbers with it, i.e. 7
> > found four other
> > numbers (49, 97, 130, and 10) making it a rank 4 in happiness.
> >
> > If you find all these examples trivial, write you program so that
> > it will find
> > happy numbers in other bases such as base 2 or 16. From there you
> > can extend the
> > program so that it finds happy bases (other than 2 and 4). A happy
> > bases is a
> > base where all numbers are happy. Good luck.
> >
>
> from http://www.rubyquiz.com/
>
> Nice.
> Any takers for this one?
>
> I really like the idea of quizzes. I think we should have sth.
> similar for Squeak and Squeakland also.
> Or just collect our answers ...
>
> Cheers,
>
> Markus
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: HappyNumber.st
Type: application/octet-stream
Size: 8095 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20060901/d768ea43/HappyNumber.obj
```