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

Username:   Password: 
 RegisterRegister   
 Checking for all divisible factor of a number
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
aznrex




PostPosted: Fri Nov 18, 2005 3:42 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Fri Nov 18, 2005 5:23 pm   Post subject: (No subject)

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




PostPosted: Fri Nov 18, 2005 5:47 pm   Post subject: (No subject)

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




PostPosted: Fri Nov 18, 2005 6:02 pm   Post subject: (No subject)

Try this code for the first couple of perfect numbers.

code:


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




PostPosted: Fri Nov 18, 2005 8:36 pm   Post subject: (No subject)

hmm, can you please explain to me what is happening in teh code??
ie what is fcn?
aznrex




PostPosted: Fri Nov 18, 2005 8:42 pm   Post subject: (No subject)

actually,
what does n repersent and what does i repersent??
i get your logic of the code
Cervantes




PostPosted: Fri Nov 18, 2005 8:49 pm   Post subject: (No subject)

aznrex wrote:
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 [Turing Tutorials]. There's a link to it in the Turing Walkthrough, as well.

In terms of the isPrime function, n represents the number in question. Is n a prime number? That's what the function determines. i is used to iterate through various numbers, checking whether they are a factor of n.

GlobeTrotter: Why don't you use the following?
code:
for i : 2 .. n div 2
GlobeTrotter




PostPosted: Sat Nov 19, 2005 3:55 pm   Post subject: (No subject)

Cervantes wrote:

GlobeTrotter: Why don't you use the following?
code:
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.
Sponsor
Sponsor
Sponsor
sponsor
aznrex




PostPosted: Sat Nov 19, 2005 5:26 pm   Post subject: (No subject)

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




PostPosted: Sun Nov 20, 2005 7:56 pm   Post subject: (No subject)

code:

for x : 1 .. a - 1
    if a mod x = 0 then
        b += x
    end if
end for
MysticVegeta




PostPosted: Sun Nov 20, 2005 8:14 pm   Post subject: (No subject)

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
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  [ 11 Posts ]
Jump to:   


Style:  
Search: