
-----------------------------------
aznrex
Fri Nov 18, 2005 3:42 pm

Checking for all divisible factor of a number
-----------------------------------
Hey,
one of my work question is telling me to make a program where the user will input an Int number and check whether it is a perfect number or not
for those who dont know what a perfect number is :
a perfect number, is a number where the sum of all its dividable factors other then it self is equal to the number it self ie:
1 + 2 + 3 = 6
1 2 and 3 are int number that 6 can evenly divid into, and the sum is also 6

1 + 2 + 4 + 7 + 14 = 28
1 2 4 7 14, are int number that 28 can evenly divid into, and the sum will be 28

ok, 
so my problem is whether it is possoble for turing to find out all the dividable factors thats an int, of a number. also how it shows it to the user

thanks

-----------------------------------
MysticVegeta
Fri Nov 18, 2005 5:23 pm


-----------------------------------
Wasnt this a DWITE Contest problem? Anyways, We cant "do" it for you but I can help you with the pseudocode :

Ok think about this, you need a variable that stores the sum of factors. Now, You need to check whether a number is divisible by the main number. So you need a "for" loop for that. Make sure you understand the "mod" function. Press F10, and check that out. In the end, check if the sum > num or < num or =  num.  :wink:

-----------------------------------
aznrex
Fri Nov 18, 2005 5:47 pm


-----------------------------------
hmm,
i dont think you would mod this number
because i need the actualy value when i divid, say "a" by the counter
also, is it possbible for when u divid a/counter (from for counter : 1 .. a)
and only divid whole numbers?? and not any decimals
then somehow store the sum of all those whole number and check whether it   is equal to "a"

-----------------------------------
GlobeTrotter
Fri Nov 18, 2005 6:02 pm


-----------------------------------
Try this code for the first couple of perfect numbers.



fcn isPrime (n : int) : boolean 
    if n = 2 then 
        result true 
    end if 
    for i : 2 .. n - 1 
        if n mod i = 0 then 
            result false 
        end if 
    end for 
    result true 
end isPrime 

for n : 2 .. 13 
    if isPrime ((2 ** n) - 1) then 
        put ((2 ** n) - 1) * (2 ** (n - 1)) 
    end if 
end for 


-----------------------------------
aznrex
Fri Nov 18, 2005 8:36 pm


-----------------------------------
hmm, can you please explain to me what is happening in teh code??
ie what is fcn?

-----------------------------------
aznrex
Fri Nov 18, 2005 8:42 pm


-----------------------------------
actually,
what does n repersent and what does i repersent??
i get your logic of the code

-----------------------------------
Cervantes
Fri Nov 18, 2005 8:49 pm


-----------------------------------
hmm, can you please explain to me what is happening in teh code??
ie what is fcn?
fcn stands for function.  To learn about functions, check out our tutorial on the subject, which can be found in for i : 2 .. n div 2

-----------------------------------
GlobeTrotter
Sat Nov 19, 2005 3:55 pm


-----------------------------------

GlobeTrotter: Why don't you use the following?
for i : 2 .. n div 2

I could have made the isPrime function more efficient.  Actually rather than going up to n div 2, I could have gone up to ceil(sqrt(n)).  Anything above that would be redundant.

-----------------------------------
aznrex
Sat Nov 19, 2005 5:26 pm


-----------------------------------
hmm, but i am trying to write a program to check for a perfect number not generate,

what i was thinking is whether turing has any fucntion that will only display or store numbers that are whole ( like 1, 2 , 3 and not 1.1 or 2.4 etc)

after the number is stored, say var a then i can go like n (user inputed number) and do this : if n = a - n then 
put "it is perfect number"
sumthing like this, but so far i am not able to do that

-----------------------------------
person
Sun Nov 20, 2005 7:56 pm


-----------------------------------

for x : 1 .. a - 1
    if a mod x = 0 then
        b += x
    end if
end for


-----------------------------------
MysticVegeta
Sun Nov 20, 2005 8:14 pm


-----------------------------------
Wrong way to code Xiao, If n is a huge number, execution takes forever, I think GlobeTrotter's method is better, its faster. 

Btw you can "for x : 1..ceil(n/2) " too saving more time
