
-----------------------------------
ILIKEMILK
Sun Apr 24, 2011 5:39 pm

What's wrong with my code?
-----------------------------------
What is it you are trying to achieve?
I need to write a program in which I find out if a number is a perfect number or not. A perfect number means that if you add up all the factors of the number, their sum will be the number itself. For example 6 is a perfect number because the sum of its factors (1+2+3) = 6.
What is the problem you are having?
I'm not sure how I can find the factors of a number. What would the code for that be?
Describe what you have tried to solve this problem
This is my code at the moment.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
var userNum : int
var numoriginal : int := 0
var factoredNum : int := 1
var factoredSum : int := 0
put "Please enter a positive integer. " ..
get userNum
for i : 1 .. userNum
    userNum / i 
    put i
end for






Please specify what version of Turing you are using


-----------------------------------
apython1992
Sun Apr 24, 2011 8:56 pm

RE:What\'s wrong with my code?
-----------------------------------
Have you checked out Turing's mod operator?

-----------------------------------
Raknarg
Sun Apr 24, 2011 9:14 pm

Re: What's wrong with my code?
-----------------------------------
I would use a function actually. I'll explain it in a short sense. 
So a function is like a set of steps used to get a value. If you've worked with procedures before, they're very similar. You call the name, give it a parameter to do stuff with, and you tell it what kind of result you're looking for. You use it by calling the name and having the value in brackets next to it.
For instance:

function square (x : int) : int
    result x * x
end square

put square (5)


In this case you would get an output of 25, or x times x.
In your case, it would be a bit different. You see, your code seems to be tying to find an answer with an integer; what you need is a boolean, a.k.a. true or false. Therefore, you would use a fuction with the result of a boolean.

function perfect (x : int) : boolean
    var total : int := 0
    for i : 1 .. x - 1
        if x / i = x div i then
            total := total + i
        end if
    end for
    if total = x then
        result true
    else
        result false
    end if
end perfect

if x / i = x div  checks to see if they both divide to produce am integer (means its a factor). If it is, it adds it to a total.
Try using that.

-----------------------------------
apython1992
Sun Apr 24, 2011 9:19 pm

RE:What\'s wrong with my code?
-----------------------------------
The mod operator does that in a cleaner, more efficient, and more readable way. It produces the remainder of the division. For example, 3 mod 2 is 1, because when you divide 3 by 2, you get 1 remainder 1. If the remainder is 0, you have yourself a factor.

-----------------------------------
Raknarg
Sun Apr 24, 2011 9:28 pm

RE:What\'s wrong with my code?
-----------------------------------
Damn, I forgot about that -.-'
Would've read the original comment, but it took me 30 mins to make a reply. So distracted :P

-----------------------------------
Raknarg
Sun Apr 24, 2011 9:29 pm

RE:What\'s wrong with my code?
-----------------------------------
In that case... 

function perfect (x : int) : boolean 
    var total : int := 0 
    for i : 1 .. x - 1 
        if x mod i = 0 then 
            total := total + i 
        end if 
    end for 
    if total = x then 
        result true 
    else 
        result false 
    end if 
end perfect 


-----------------------------------
apython1992
Sun Apr 24, 2011 9:36 pm

RE:What\'s wrong with my code?
-----------------------------------
Much better ;) We should try to let the OP figure out as much as he can without giving too much code, but +1 karma for helping. :)

-----------------------------------
Raknarg
Sun Apr 24, 2011 9:39 pm

RE:What\'s wrong with my code?
-----------------------------------
Lol. I don't like to give out code, but it's habit from grade 10 with too many noobs :P
Plus, I find that I learn much more when I have an expert to show me the best way to do something.

-----------------------------------
Zren
Mon Apr 25, 2011 3:26 am

RE:What\'s wrong with my code?
-----------------------------------
A factor is never more than half itself (other than itself of course).
Eg: The factors of 12 are 1, 2, 3, 4, 6, and 12.

1 x n = n
The next possible factor is 2.
2 x n/2 = ?

Thus you can limit the range of numbers you have to check.

So you could do:
Add 1 and n as factors. Since 1 times itself will always be itself.
Check the range of 2 -> n/2 for other factors.

Efficiency might not seem so bad when n is a low number like 12, but when your checking number bigger than that, the loop tends to chug.

-----------------------------------
Raknarg
Mon Apr 25, 2011 10:48 am

RE:What\'s wrong with my code?
-----------------------------------
Yeah, I know, I just didn't feel like putting it in -.-' If you have a small code like that, it wont really make a speed difference anyways.

function perfect (x : int) : boolean 
    var total : int := 0 
    for i : 1 .. x div 2 
        if x mod i = 0 then 
            total := total + i 
        end if 
    end for 
    if total = x then 
        result true 
    else 
        result false 
    end if 
end perfect 


-----------------------------------
ILIKEMILK
Mon Apr 25, 2011 9:00 pm

RE:What\'s wrong with my code?
-----------------------------------
Here is my code now, but it still doesnt work.


    var userNum, factoredNum : int
    var factoredSum : int := 0

    put "Please enter a positive integer. " ..
    get userNum
    for decreasing i : userNum .. 1
        factoredNum := userNum mod i
        if
                userNum mod i = 0 then
            factoredSum := factoredSum + i
        end if
    end for
    if factoredSum = userNum then
        put userNum, " is a perfect number."
    else
        put
            userNum, " is not a perfect number."
    end if

-----------------------------------
apython1992
Mon Apr 25, 2011 10:01 pm

RE:What\'s wrong with my code?
-----------------------------------
In figuring out if a number is perfect or not, you only care about its proper divisors. The proper divisors of say, 6, are 1, 2, and 3.  Your program should then test all numbers from 1 to the inputted number, but not including that number. Think you can figure out how to change that?

-----------------------------------
apython1992
Mon Apr 25, 2011 10:02 pm

RE:What\'s wrong with my code?
-----------------------------------
Oh and as Zren has mentioned, you really only need to test up to half of the inputted number.

-----------------------------------
whoareyou
Tue Apr 26, 2011 3:20 pm

Re: What's wrong with my code?
-----------------------------------
I did a program like this a while back. You can take a look at the help the other users had given me when I had troubles with it.

two variables, one to get the number from the user and one to store the sum of the factors.

Then you'd use a FOR loop to calculate any factors from the 1 to 1 less than the number (since the number itself cannot be a factor); IF you've found a factor, it should be added to the "sum of factors variable". (you could also implement the range limit as mentioned my the previous posters when searching for factors.)

Lastly, you need to check IF the sum of factors variables equals the number that was entered. IF it is, then it is a perfect number. ELSE, the number that the user entered is not a perfect number.

You could also LOOP the program so that the program can check more than one number.

-----------------------------------
ILIKEMILK
Wed Apr 27, 2011 12:07 am

RE:What\'s wrong with my code?
-----------------------------------
^ Thanks, that helped out a lot. I got it now.
