# Happy Numbers (#93)

**URL:** <https://www.ruby-forum.com/t/happy-numbers-93/67234>\
**Category:** Ruby\
**Created:** [September 1, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234 "2006-09-01T15:04:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/1 "2006-09-01T15:04:18Z")

</div>

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this quiz  
until  
48 hours have passed from the time on this message.

2. Support Ruby Q. by submitting ideas as often as you can:

[http://www.rubyquiz.com/](http://www.rubyquiz.com/)

1. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps  
everyone  
on Ruby T. follow the discussion. Please reply to the original quiz  
message,  
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Shane E.

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.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 3:11pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/2 "2006-09-01T15:11:57Z")

</div>

James G. wrote:

> 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.

-Boggles-

Okay, I’m feeling stupid here. What MAKES that number be happy? The  
nearest I can find is that if it’s an infinite loop, it isn’t happy. If  
it’s not a loop, and resolves to 1 eventually. (It has to resolve to 1,  
or it’s be a loop.) It’s happy.

Is that right?

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/3 "2006-09-01T15:32:21Z")

</div>

On Sep 1, 2006, at 8:12 AM, William C. wrote:

> Okay, I’m feeling stupid here. What MAKES that number be happy? The  
> nearest I can find is that if it’s an infinite loop, it isn’t  
> happy. If  
> it’s not a loop, and resolves to 1 eventually. (It has to resolve  
> to 1,  
> or it’s be a loop.) It’s happy.
> 
> Is that right?

Sounds right on to me.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/4 "2006-09-01T15:56:25Z")

</div>

Peter H. wrote:

> Doesn’t this mean that all numbers are happy?

Do it by hand for 3…

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/5 "2006-09-01T15:46:23Z")

</div>

Doesn’t this mean that all numbers are happy?

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 4:12pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/6 "2006-09-01T16:12:28Z")

</div>

3^2=9  
9^2=81  
8^2+1^2=65  
6^2+5^2=61  
6^2+1^2=37  
3^2+7^2=58  
5^2+8^2=89  
8^2+9^2=145  
1^2+4^2+5^2=42  
4^2+2^2=20  
2^2+0^2=4  
4^2=16  
1^2+6^2=37  
We just started a loop here since 37 has already been found.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 4:41pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/7 "2006-09-01T16:41:36Z")

</div>

James G. wrote:

> On Sep 1, 2006, at 8:41 AM, Peter H. wrote:
> 
> > Doesn’t this mean that all numbers are happy?
> 
> Here’s some further reading for those still confused:
> 
> [http://mathworld.wolfram.com/HappyNumber.html](http://mathworld.wolfram.com/HappyNumber.html)
> 
> James Edward G. II

Wow. Lots of strategy on that page. Almost like cheating 😉

On a side note… Did anyone NOT calculate 3 by hand? Heh.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/8 "2006-09-01T16:00:26Z")

</div>

On Sep 1, 2006, at 8:41 AM, Peter H. wrote:

> Doesn’t this mean that all numbers are happy?

Here’s some further reading for those still confused:

> **[Happy Number -- from Wolfram MathWorld](https://mathworld.wolfram.com/HappyNumber.html)**
>
> Let the sum of the squares of the digits of a positive integer s\_0 be represented by s\_1. In a similar way, let the sum of the squares of the digits of s\_1 be represented by s\_2, and so on. Iterating this sum-of-squared-digits map always eventually...

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/9 "2006-09-01T17:12:44Z")

</div>

On 9/1/06, William C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On a side note… Did anyone NOT calculate 3 by hand? Heh.

/me sheepishly raises his hand

I didn’t use my hands, just my head. ☹

Jacob F.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/10 "2006-09-01T17:28:46Z")

</div>

Sorry, I should have clarified that a Happy number results in 1, and an  
Unhappy number results in an infinite loop.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 5:32pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/11 "2006-09-01T17:32:47Z")

</div>

One note of advice - the operator to use is actually \*\* not ^ as might  
be expected:

irb\> 7 ^ 2 # Gives 5  
irb\> 7 \*\* 49 # Gives 49

Cheers,  
Roland

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 4:42pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/12 "2006-09-01T16:42:33Z")

</div>

Problem is that from the quiz it states that you either get a 1 or an  
infinite loop and that an unhappy number is “obvious”. Which is a sign  
that something has not been explained clearly. Given that the Wolfram  
page was clearer than the quiz at to what constitutes happy don’t be  
surprised if some people go off down the wrong path.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/13 "2006-09-01T18:10:52Z")

</div>

knaveofdiamonds wrote:

> One note of advice - the operator to use is actually \*\* not ^ as might  
> be expected:
> 
> irb\> 7 ^ 2 # Gives 5  
> irb\> 7 \*\* 49 # Gives 49

I think you meant 7 \*\* 2 = 49, whereas  
7 \*\* 49 = 256923577521058878088611477224235621321607

But, since these are integers, we are better off computing n \* n,  
not n \*\* 2. There’s really no point (in time efficiency) to explicitly  
raising n to a power p unless n is not an integer or p is relatively  
large.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/14 "2006-09-01T18:26:53Z")

</div>

> Now that you have this program, what is the largest happy number you can find?

(10 \*\* N) where N is anything. You name N, I’ll say N+1.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/15 "2006-09-01T18:38:56Z")

</div>

On Sep 1, 2006, at 7:58 AM, Shane E. wrote:

> Sorry, I should have clarified that a Happy number results in 1,  
> and an  
> Unhappy number results in an infinite loop.

That’s not quite what the cited article says. it says that happy  
numbers result in an infinite loop on 1 and unhappy numbers result in  
an infinite loop on the series: 4, 16, 37, 58, 89, 145, 42, 20,  
4, … and that there are no other possibilities.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/16 "2006-09-01T18:42:57Z")

</div>

Peter H. wrote:

> Problem is that from the quiz it states that you either get a 1 or an  
> infinite loop and that an unhappy number is “obvious”. Which is a sign  
> that something has not been explained clearly. Given that the Wolfram  
> page was clearer than the quiz at to what constitutes happy don’t be  
> surprised if some people go off down the wrong path.

As I understand the Wolfram article, an unhappy number never hits 1 as a  
result, whereas a happy number eventually hits 1:

“Unhappy numbers have eventually periodic sequences of s(sub)i which  
never  
reach 1.”

The quiz item emphasizes a criterion that is only mentioned in passing  
on  
the Wolfram page, that is, there are degrees of happiness, and a number  
that has many happy predecessors is, umm, more happy. So a relatively  
large  
number that eventually results in 1, but with a lot of steps along the  
way  
(therefore more happy ancestors), is happier.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/17 "2006-09-01T18:28:54Z")

</div>

On Sep 1, 2006, at 9:01 AM, Ruby Q. wrote:

> 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?

Clearly, there is no largest happy number. Any number of the form  
10\*\*n in base b is b-relative happy (with rank 1). I feel this makes  
the stated question an ill-conditioned one.

> 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.

This seems a better question to pursue than the first. Other  
questions one might explore: how does rank vary as the numbers  
increase? Does maximum rank grow as happy numbers get bigger? Is  
there a pattern? Are there interesting statistics?

Regards, Morton

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/18 "2006-09-01T18:59:05Z")

</div>

On Sep 1, 2006, at 9:01 AM, Ruby Q. wrote:

> 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?

Clearly, there is no largest happy number. Any number of the form  
10\*\*n in base b is b-relative happy (with rank 1). I feel this makes  
the stated question an ill-conditioned one.

> 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.

This seems a better question to pursue than the first. Other  
questions one might explore: how does rank vary as the numbers  
increase? Does maximum rank grow as happy numbers get bigger? Is  
there a pattern? Are there interesting statistics?

Regards, Morton

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/19 "2006-09-01T18:44:57Z")

</div>

On 1-Sep-06, at 11:00 AM, knaveofdiamonds wrote:

> One note of advice - the operator to use is actually \*\* not ^ as might  
> be expected:
> 
> irb\> 7 ^ 2 # Gives 5  
> irb\> 7 \*\* 49 # Gives 49

I hope that’s not cut & pasted! 7 \*\* 49 is a little bigger:

ratdog:~ mike$ irb --prompt simple

> > 7 ^ 2  
> > =\> 5  
> > 7 \*\* 2  
> > =\> 49  
> > 7 \*\* 49  
> > =\> 256923577521058878088611477224235621321607

Mike

–

Mike S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)

The “`Stok’ disclaimers” apply.

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [September 1, 2006, 8:03pm UTC](https://www.ruby-forum.com/t/happy-numbers-93/67234/20 "2006-09-01T20:03:14Z")

</div>

On Sep 1, 2006, at 10:40 AM, Paul L. wrote:

> 7 \*\* 49 = 256923577521058878088611477224235621321607
> 
> But, since these are integers, we are better off computing n \* n,  
> not n \*\* 2. There’s really no point (in time efficiency) to explicitly  
> raising n to a power p unless n is not an integer or p is  
> relatively large.

#!/usr/bin/env ruby -w

require “benchmark”

TESTS = 1\_000\_000  
Benchmark.bmbm(10) do |results|  
results.report(“Exponent:”) { TESTS.times { |n| n \*\* 2 } }  
results.report(“Multiply:”) { TESTS.times { |n| n \* n } }  
end

# \>\> Rehearsal ---------------------------------------------

# \>\> Exponent: 1.370000 0.000000 1.370000 ( 1.372358)

# \>\> Multiply: 1.730000 0.010000 1.740000 ( 1.747647)

# \>\> ------------------------------------ total: 3.110000sec

# \>\>

# \>\> user system total real

# \>\> Exponent: 1.440000 0.000000 1.440000 ( 1.452441)

# \>\> Multiply: 1.760000 0.010000 1.770000 ( 1.775988)

James Edward G. II

[Next page](https://www.ruby-forum.com/t/happy-numbers-93/67234.md?page=2)
