
-----------------------------------
whoareyou
Thu Mar 03, 2011 3:45 pm

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)



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

-----------------------------------
Insectoid
Thu Mar 03, 2011 3:58 pm

RE:help - perfect numbers
-----------------------------------
[code]

    num +=1 
    
    for i : 1 .. num - 1 
[/code]

What's this doing?

-----------------------------------
whoareyou
Thu Mar 03, 2011 4:00 pm

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
Thu Mar 03, 2011 4:06 pm

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
Thu Mar 03, 2011 4:19 pm

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
Thu Mar 03, 2011 4:48 pm

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
Thu Mar 03, 2011 4:51 pm

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
Thu Mar 03, 2011 4:55 pm

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..

-----------------------------------
whoareyou
Thu Mar 03, 2011 4:56 pm

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
Thu Mar 03, 2011 4:59 pm

Re: RE:help - perfect numbers
-----------------------------------

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
Thu Mar 03, 2011 5:12 pm

Re: help - perfect numbers
-----------------------------------
THE ALMOST FINISHED PRODUCT!



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
Thu Mar 03, 2011 5:39 pm

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
