Computer Science Canada Help-Perfect and Happy number fail |
Author: | SilverMan [ Sat Nov 03, 2007 10:17 am ] |
Post subject: | Help-Perfect and Happy number fail |
i need to write a function for get Perfect and get Happy number. I came up with the program but it will Int overflow. I need to find 10,000 Perfect number and 500 get Happy number. I have search the forum but can't find those 2 example for Perfect and Happy number function getPerfect( n : int ) : real var a : real for i : 1 .. 10 by 1 a := 2 ** (i - 1) * (2 ** i - 1) % i check this equation on Wikipedia put a : i .. end for result a end getPerfect %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function checkPerfect (n : int) : boolean if n = getPerfect (n) then result true else result false end if end checkPerfect %~~~~~~~~~~~~~~~~~~~~~~~~~~~ function getHappy (n : int) : real var a, b, c, d : real := 0 var count : int := 0 for i : 2 .. 500 by 1 c := i loop a := c mod 10 b := c div 10 d := a ** 2 + b ** 2 exit when d = 4 or d = 16 or d = 37 or d = 58 or d = 42 or d = 145 c := d if d = 1 then put d : 3 .. count := count + 1 end if end loop end for result d end getHappy %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ function checkHappy (n : int) : boolean var x, y : int := 0 for increasing : n .. 15 by 1 if getHappy (n) < 15 then result true else result false end if end for end checkHappy |
Author: | CodeMonkey2000 [ Sat Nov 03, 2007 11:32 am ] | ||
Post subject: | Re: Help-Perfect and Happy number fail | ||
This sounds like a string manipulation problem. Remember strings are just arrays of characters. That means you can check a string character by character (using the array notation ie If we have MyString="Hello World" then MyString(7) will be "W"). Some useful functions: length() returns the length of a string, intsrt() and strint() converts an integer to string and string to an integer. Here is a solution for happy numbers I did a while back.
|
Author: | SilverMan [ Sat Nov 03, 2007 11:52 pm ] |
Post subject: | RE:Help-Perfect and Happy number fail |
Thx CodeMonkey200 for the code. It work perfectly to find the perfect number ^_^ Can some one help me with the Perfect Number. I want to find it ups to 10,000 number. It only work under 15 number because it will overflow when i put the large number in it |
Author: | CodeMonkey2000 [ Sun Nov 04, 2007 1:18 pm ] |
Post subject: | RE:Help-Perfect and Happy number fail |
Do you know what the formula you are using does (or how it works)? Read the article carefully. And getting 10 000 perfect numbers sounds ridiculous. The numbers get really big really quickly. |
Author: | Brightguy [ Sun Nov 04, 2007 3:02 pm ] |
Post subject: | Re: RE:Help-Perfect and Happy number fail |
CodeMonkey2000 @ Sun Nov 04, 2007 1:18 pm wrote: And getting 10 000 perfect numbers sounds ridiculous.
Heh, yeah, especially considering only 44 are currently known. Though, it is conjectured there are an infinite number of them. Small ones are quite easy to find: just form the Mersenne number M_p=2^p-1, if it is prime, then 2^(p-1)*M_p is perfect. |
Author: | SilverMan [ Sun Nov 04, 2007 9:34 pm ] |
Post subject: | RE:Help-Perfect and Happy number fail |
The formula i check it on the wikipedia when i try to check what is perfect number . I don't know what the teacher think either since he wants us to find 10 000 of perfect number T_T |