Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help-Perfect and Happy number fail
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
SilverMan




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: 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.
Turing:
function Happy (n : string) : int
    var num : string := n
    var tmp : int := 0
    loop
        for x : 1 .. length (num)
            tmp += strint (num (x)) ** 2
        end for
        num := intstr (tmp)
        tmp := 0
        exit when length (num) = 1
    end loop
    result strint (num)
end Happy

function isHappy(n : int) : boolean
    result Happy (intstr (n)) = 1
end isHappy

for x : 1 .. 500
    if isHappy(x) then
        put x, " is happy!"
    end if
end for
SilverMan




PostPosted: 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
CodeMonkey2000




PostPosted: 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.
Brightguy




PostPosted: 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.
SilverMan




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: