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

Username:   Password: 
 RegisterRegister   
 Program to Determine Perfect Numbers
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CITC




PostPosted: Fri Jan 09, 2004 9:28 pm   Post subject: Program to Determine Perfect Numbers

Unfortunately this program takes really long to determine the perfect numbers after 1700 something... I don't think theres any better way to do it though, or it hasn't been discovered yet.. this is brute force!! Wink

for any who don't know: Perfect Numbers are numbers that their factors (excluding the number itself) add up to the number... wow explained that bad, here's an example.
6 is a perfect number.. factors of 6 = 1,2,3,6.. exclude 6... 1+2+3 = 6

code:
var num : int := 0
var key : string (1)
loop
   cls
   num += 1
   var keep : array 1 .. num of int
   var total : int := 0
   for k : 1 .. num
       keep (k) := 0
   end for
   for i : 1 .. num
       if num rem i = 0 then
           keep (i) := i
       end if
   end for
   for p : 1 .. num
       if keep (p) not= 0 then
           total += keep (p)
       end if
   end for
   if (total div num = 2) and (total rem num = 0) then
       put num
       for o : 1 .. num
           if keep (o) not= 0 then
               put keep (o), ", " ..
           end if
       end for
       getch (key)
   end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
McKenzie




PostPosted: Fri Jan 09, 2004 10:49 pm   Post subject: (No subject)

A bit faster:
code:
function isPerfect (n : int) : boolean
    var sum := 0
    for i : 1 .. n div 2
        if n mod i = 0 then
            sum := sum + i
        end if
    end for
    if sum = n then
        result true
    else
        result false
    end if
end isPerfect

var now:int
for i:1..10000
    if isPerfect(i) then
        put i
       clock(now)
       put "time ",now/1000 :0:2, " Seconds"
       end if
end for

The timer stuff is there because I was comparing with yours to make sure mine was worth posting.
AsianSensation




PostPosted: Fri Jan 09, 2004 10:50 pm   Post subject: Re: Program to Determine Perfect Numbers

CITC wrote:
I don't think theres any better way to do it though, or it hasn't been discovered yet..


yeah, actually there is a better way, there is a formula that let you derive any perfect number, google it up, I sure it's on the web somewhere.

or you can prove it yourself?
Andy




PostPosted: Sat Jan 10, 2004 10:41 am   Post subject: (No subject)

LOL.... formulas... the reason why we use compsci is cuz we dont like formulas.... its actually the same speed for a computer to use a for loop to add one to a number than to multiply the number
CITC




PostPosted: Sat Jan 10, 2004 11:40 am   Post subject: (No subject)

Your right there is a formula for it. I'll post the program in a day or two probably.
santabruzer




PostPosted: Sat Jan 10, 2004 4:18 pm   Post subject: (No subject)

i've always wondered if there was a formula for prime numbers.. so that you wouldn't have to devide by every number halfway up to that number... and for perfect ones.. iyea.. it's cool.. there's a formula...
AsianSensation




PostPosted: Sat Jan 10, 2004 5:47 pm   Post subject: (No subject)

there is no formula for prime numbers, that's why really really large prime numbers are used to encrypt many many things.
Andy




PostPosted: Sat Jan 10, 2004 8:04 pm   Post subject: (No subject)

but u can get a program that generates prime numbers... me and shaun are making one, one of these days
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Sat Jan 10, 2004 9:55 pm   Post subject: (No subject)

sure, there are many many ways of generating a prime number, but starting with the Mersenne (did I spell that right?) prime is what they do now to find a really large prime number. Mersenne primes are number in the form of 2^n - 1. The other day, we were told that the largest prime known was updated again, and they found that number by passing parts of the prime finding program to alot of computers, so if you want to be part of the finding team, all you have to do was download their program, and it runs in the background. Then they take all the info generated at different computer and come up with the number.
CITC




PostPosted: Sat Jan 10, 2004 11:37 pm   Post subject: (No subject)

I heard about 1 year and a half ago (i think) that an Indian mathemetician who was amazing with numbers and patterns had discovered a formula to get prime numbers... read it in some magazine, dunno which. I'm not too sure though, I could be wrong.

btw, that bit about the mersenne prime formula being 2^n - 1 is half of the perfect number formula.

its 2^(n - 1) * (2^n - 1)

I'm overwhelmed with trying to get the slime volleyball bouncing thing working though, if no one else makes this program and posts it, I'll make it when I can.

Cheers
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: