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

Username:   Password: 
 RegisterRegister   
 help - perfect numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
whoareyou




PostPosted: Thu Mar 03, 2011 3:45 pm   Post subject: help - perfect numbers

What is it you are trying to achieve?
I am trying to create a program that will calculate the first 3 perfect numbers


What is the problem you are having?
When I try to run the program, nothing shows up.


Describe what you have tried to solve this problem
moving variables and counters in and out of the loop.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


var sumOfFactors : int:= 0
var num : int:=0
 
loop
    num +=1
   
    for i : 1 .. num - 1

       
        if (num mod i = 0) then

         
            sumOfFactors += i
        end if
    end for


    if (num = sumOfFactors) then
        put num, " is perfect!"
   
        end if

end loop




Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Mar 03, 2011 3:58 pm   Post subject: RE:help - perfect numbers

code:


    num +=1
   
    for i : 1 .. num - 1


What's this doing?
whoareyou




PostPosted: Thu Mar 03, 2011 4:00 pm   Post subject: RE:help - perfect numbers

num is initally set to zero,
so then that adds 1 to the num value

and for i: 1 .. num-1 is for the range of the divisors of the number. since a perfect number cant have a factor equal to itself, the range ends at num - 1.
RandomLetters




PostPosted: Thu Mar 03, 2011 4:06 pm   Post subject: RE:help - perfect numbers

what about this?

sumOfFactors

also, it's an infinite loop. you should stop the program after it finds 3 numbers
Insectoid




PostPosted: Thu Mar 03, 2011 4:19 pm   Post subject: RE:help - perfect numbers

That's not what it does at all.

i = 1. You're looping from 1 to i-1. What's i-1?
whoareyou




PostPosted: Thu Mar 03, 2011 4:48 pm   Post subject: RE:help - perfect numbers

RandomLetters: sumOfFactors stores the sum of the numbers that go into the number perfectly, and then if the total of that variable equals the number, then its a perfect number.

Insectoid: umm i dont think i understnad you. it's i..num-1 not num-i
Insectoid




PostPosted: Thu Mar 03, 2011 4:51 pm   Post subject: RE:help - perfect numbers

Sorry; It's still the same issue.

Num = 1. You're looping from 1 to num-1. Num-1 is zero. You're looping from 1 to zero. That is a problem.
mirhagk




PostPosted: Thu Mar 03, 2011 4:55 pm   Post subject: RE:help - perfect numbers

initially it is 0, then it becomes 1, then 2. The problem isn't there insectoid.

The main problem is that you don't reset sumOfFactors.

So basically if it's checking 7, it not only checks it against the sum of 7's factors, but 7's factors + 6's factors + 4's factors etc..
Sponsor
Sponsor
Sponsor
sponsor
whoareyou




PostPosted: Thu Mar 03, 2011 4:56 pm   Post subject: Re: help - perfect numbers

i see i see.

ok i changed the initial value of num to 5, and now im getting an output screen. but now, it outputs :

"6 is perfect!
7 is perfect!"
?
whoareyou




PostPosted: Thu Mar 03, 2011 4:59 pm   Post subject: Re: RE:help - perfect numbers

mirhagk @ Thu Mar 03, 2011 4:55 pm wrote:

The main problem is that you don't reset sumOfFactors.

So basically if it's checking 7, it not only checks it against the sum of 7's factors, but 7's factors + 6's factors + 4's factors etc..


so then, after it finds a perfect number, i should put

sumOfFactors := 0 ?
whoareyou




PostPosted: Thu Mar 03, 2011 5:12 pm   Post subject: Re: help - perfect numbers

THE ALMOST FINISHED PRODUCT!

Turing:


var sumOfFactors : int:= 0
var num : int:=0
 
loop
    num +=1
   
    for i : 1 .. num - 1

       
        if (num mod i = 0) then

         
            sumOfFactors += i
        end if
    end for


    if (num = sumOfFactors) then
        put num, " is perfect!"
   sumOfFactors :=0
   
   else
   sumOfFactors :=0
    end if

end loop



thanks to mirhagk --> the problem was with the sumOfFactors! thank you!

now i need to find a way to stop the loop after it finds 3 perfect numbers. maybe another variable ?[/u]
RandomLetters




PostPosted: Thu Mar 03, 2011 5:39 pm   Post subject: RE:help - perfect numbers

Insectoid's logic is correct
num - 1 = 0 on the first iteration.

I guess turing's counted loop is equivalent to

while i < k

not

exit when i = k
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  [ 12 Posts ]
Jump to:   


Style:  
Search: