Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [QUIZ] Happy Numbers (#93)

Reply
Thread Tools

[QUIZ] Happy Numbers (#93)

 
 
Ruby Quiz
Guest
Posts: n/a
 
      09-01-2006
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
on Ruby Talk follow the discussion. Please reply to the original quiz message,
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.

 
Reply With Quote
 
 
 
 
William Crawford
Guest
Posts: n/a
 
      09-01-2006
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/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      09-01-2006
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

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      09-01-2006
Doesn't this mean that all numbers are happy?


 
Reply With Quote
 
ChrisH
Guest
Posts: n/a
 
      09-01-2006

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


Do it by hand for 3...

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      09-01-2006
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

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      09-01-2006
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.


 
Reply With Quote
 
William Crawford
Guest
Posts: n/a
 
      09-01-2006
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/.

 
Reply With Quote
 
Jacob Fugal
Guest
Posts: n/a
 
      09-01-2006
On 9/1/06, William Crawford <(E-Mail Removed)> 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

 
Reply With Quote
 
knaveofdiamonds
Guest
Posts: n/a
 
      09-01-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT: Happy Happy Joy Joy! Mike T. MCSE 79 11-19-2006 08:22 PM
[QUIZ SOLUTION] Happy Numbers (#93) - happy bases Daniel Martin Ruby 1 09-06-2006 07:06 PM
HEXUS.opinions :: Have a happy happy gaming holiday Silverstrand Front Page News 0 12-23-2005 04:12 PM
Happy Happy Joy Joy, I recovered my photos Tama Mativa Digital Photography 9 05-24-2004 04:58 PM
happy happy christmas showgun MCSE 26 12-17-2003 07:11 PM



Advertisments