# [QUIZ] Happy Numbers (#93)

Discussion in 'Ruby' started by Ruby Quiz, Sep 1, 2006.

1. ### Ruby QuizGuest

The three rules of Ruby Quiz:

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 Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
if you can.

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

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.

Ruby Quiz, Sep 1, 2006

2. ### William CrawfordGuest

Re: Happy Numbers (#93)

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

--
Posted via http://www.ruby-forum.com/.

William Crawford, Sep 1, 2006

3. ### James Edward Gray IIGuest

Re: Happy Numbers (#93)

On Sep 1, 2006, at 8:12 AM, William Crawford 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 Gray II

James Edward Gray II, Sep 1, 2006
4. ### Peter HickmanGuest

Re: Happy Numbers (#93)

Doesn't this mean that all numbers are happy?

Peter Hickman, Sep 1, 2006
5. ### ChrisHGuest

Re: Happy Numbers (#93)

Peter Hickman wrote:
> Doesn't this mean that all numbers are happy?

Do it by hand for 3...

ChrisH, Sep 1, 2006
6. ### James Edward Gray IIGuest

Re: Happy Numbers (#93)

On Sep 1, 2006, at 8:41 AM, Peter Hickman 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

James Edward Gray II

James Edward Gray II, Sep 1, 2006
7. ### Peter HickmanGuest

Re: Happy Numbers (#93)

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.

Peter Hickman, Sep 1, 2006
8. ### William CrawfordGuest

Re: Happy Numbers (#93)

James Gray wrote:
> On Sep 1, 2006, at 8:41 AM, Peter Hickman 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
>
> James Edward Gray II

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

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

--
Posted via http://www.ruby-forum.com/.

William Crawford, Sep 1, 2006
9. ### Jacob FugalGuest

Re: Happy Numbers (#93)

On 9/1/06, William Crawford <> 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 Fugal

Jacob Fugal, Sep 1, 2006
10. ### knaveofdiamondsGuest

Re: Happy Numbers (#93)

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

knaveofdiamonds, Sep 1, 2006
11. ### Matthew MossGuest

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

Matthew Moss, Sep 1, 2006
12. ### Morton GoldbergGuest

On Sep 1, 2006, at 9:01 AM, Ruby Quiz 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

Morton Goldberg, Sep 1, 2006
13. ### Chris GehlkerGuest

Re: Happy Numbers (#93)

On Sep 1, 2006, at 7:58 AM, Shane Emmons 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.
>
> On 9/1/06, William Crawford <> wrote:
>>
>> James Gray wrote:
>> > On Sep 1, 2006, at 8:41 AM, Peter Hickman 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
>> >
>> > James Edward Gray II

>>
>> Wow. Lots of strategy on that page. Almost like cheating
>>
>> On a side note... Did anyone NOT calculate 3 by hand? Heh.
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>>

>
>
> --
> --- Shane Emmons <>

--
Vegetarians eat Vegetables, Humanitarians frighten me

Chris Gehlker, Sep 1, 2006
14. ### Mike StokGuest

Re: Happy Numbers (#93)

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 Stok <>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Mike Stok, Sep 1, 2006
15. ### Morton GoldbergGuest

On Sep 1, 2006, at 9:01 AM, Ruby Quiz 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

Morton Goldberg, Sep 1, 2006
16. ### Roland SwinglerGuest

Re: Happy Numbers (#93)

Sorry, should have been 7 ** 2

Out of interest, any mathmaticians care to enlighten me as to whether
happy numbers are in any way "useful"?

Cheers,
Roland

Roland Swingler, Sep 1, 2006
17. ### James Edward Gray IIGuest

Re: Happy Numbers (#93)

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

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

#!/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 Gray II

James Edward Gray II, Sep 1, 2006
18. ### PhrogzGuest

Re: Happy Numbers (#93)

Interestingly, there doesn't seem to be a pattern as to the number of
happy numbers in a given range as the base changes:

(My results...might be wrong.
How many Happy numbers between 1..10000?
Base 2: 10000 happy numbers.
Base 3: 1988 happy numbers.
Base 4: 10000 happy numbers.
Base 5: 2571 happy numbers.
Base 6: 645 happy numbers.
Base 7: 162 happy numbers.
Base 8: 549 happy numbers.
Base 9: 627 happy numbers.
Base 10: 1442 happy numbers.
Base 11: 196 happy numbers.
Base 12: 24 happy numbers.
Base 13: 582 happy numbers.
Base 14: 93 happy numbers.
Base 15: 164 happy numbers.
Base 16: 2585 happy numbers.
Base 17: 253 happy numbers.
Base 18: 4154 happy numbers.
Base 19: 3647 happy numbers.
Base 20: 1616 happy numbers.
Base 21: 45 happy numbers.
Base 22: 17 happy numbers.
Base 23: 19 happy numbers.
Base 24: 9 happy numbers.
Base 25: 519 happy numbers.
Base 26: 377 happy numbers.
Base 27: 279 happy numbers.
Base 28: 6 happy numbers.
Base 29: 1730 happy numbers.
Base 30: 5266 happy numbers.
Base 31: 11 happy numbers.
Base 32: 11 happy numbers.
Base 33: 84 happy numbers.
Base 34: 192 happy numbers.
Base 35: 77 happy numbers.
Base 36: 50 happy numbers.

Phrogz, Sep 1, 2006
19. ### PhrogzGuest

Re: Happy Numbers (#93)

Although others are sure to have better results, I thought I'd post
mine (no spoilers). I did it a simple way first, and then another more
sophisticated way second. Nice speed improvement.

The below tests 1000 numbers for happiness in 33 different bases.

range = 1..1000
require 'Benchmark'
Benchmark.bm(20){ |r|
r.report( "Technique 1" ){
3.upto(36){ |base|
range.select{ |i|
# find out if 'i' is a happy number in the supplied base
}
}
}

r.report( "Technique 2" ){
3.upto(36){ |base|
range.select{ |i|
# find out if 'i' is a happy number in the supplied base
}
}
}
}

user system total real
Technique 1 19.282000 0.031000 19.313000 ( 19.359000)
Technique 2 2.140000 0.000000 2.140000 ( 2.156000)

Phrogz, Sep 1, 2006
20. ### Matthew MossGuest

Re: Happy Numbers (#93)

> Out of interest, any mathmaticians care to enlighten me as to whether
> happy numbers are in any way "useful"?

As has been said, "real mathematicians" shudder that their work might be useful.

In actuality, sometimes things like this aren't, and sometimes they
are years later. Prime numbers never had much use until recent years,
where they have become very important for encryption.

Maybe happy numbers have a use... just no one knows it yet!

Matthew Moss, Sep 1, 2006