Computer Science Canada

Program to Determine Perfect Numbers

Author:  CITC [ 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

var num : int := 0
var key : string (1)
   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

Author:  McKenzie [ Fri Jan 09, 2004 10:49 pm ]
Post subject: 

A bit faster:
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
        result false
    end if
end isPerfect

var now:int
for i:1..10000
    if isPerfect(i) then
        put i
       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.

Author:  AsianSensation [ 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?

Author:  Andy [ Sat Jan 10, 2004 10:41 am ]
Post 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

Author:  CITC [ Sat Jan 10, 2004 11:40 am ]
Post subject: 

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

Author:  santabruzer [ Sat Jan 10, 2004 4:18 pm ]
Post 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...

Author:  AsianSensation [ Sat Jan 10, 2004 5:47 pm ]
Post subject: 

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

Author:  Andy [ Sat Jan 10, 2004 8:04 pm ]
Post subject: 

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

Author:  AsianSensation [ Sat Jan 10, 2004 9:55 pm ]
Post 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.

Author:  CITC [ Sat Jan 10, 2004 11:37 pm ]
Post 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.