Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Perfect numbers

Name: Anonymous 2010-12-16 12:54

what is the fastest way to check if a number is perfect?
i am not asking for code.

Name: Anonymous 2010-12-16 13:09

I don't know the fastest way, but this is what I would do:

1. create an array to hold factors
2. use the modulus operator and an iteration to find factors. if the iterator is a factor, put it inside the array. if not, then increase the iterator and continue attempting to factor the number until either condition is met:

a. the sum of the numbers inside the array is greater than the number itself
b. you are done iterating

Name: Anonymous 2010-12-16 13:11

inline _Bool perfect(int n)__attribute__((pure)){return n==3;}

Name: Anonymous 2010-12-16 13:24

6
28
496
8128
33550336
8589869056
137438691328
2305843008139952128
2658455991569831744654692615953842176
191561942608236107294793378084303638130997321548169216
13164036458569648337239753460458722910223472318386943117783728128
14474011154664524427946373126085988481573677491474835889066354349131199152128
23562723457267347065789548996709904988477547858392600710143027597506337283178622239730365539602600561360255566462503270175052892578043215543382498428777152427010394496918664028644534128033831439790236838624033171435922356643219703101720713163527487298747400647801939587165936401087419375649057918549492160555646976
141053783706712069063207958086063189881486743514715667838838675999954867742652380114104193329037690251561950568709829327164087724366370087116731268159313652487450652439805877296207297446723295166658228846926807786652870188920867879451478364569313922060370695064736073572378695176473055266826253284886383715072974324463835300053138429460296575143368065570759537328128

Do you really need any more?

Name: Anonymous 2010-12-16 13:51

I'm sure there is some super easy way to tackle this cause I once did it for project Euler....in Java.

Anyway perhaps not the fastest approach but should be pretty fast.

Get a list of all perfect numbers than run a binary search on it to check if a number is a perfect number.

If you want to check for multiple numbers you don't have to start search from the beginning for each number.

Name: Anonymous 2010-12-16 13:54

>>4
That is what i am doing now. I have a list of perfect numbers to check if n is in that list. But i want to know if there is a fast way to calculate if n is perfect.

Name: Anonymous 2010-12-16 14:59

have a loop with the length of the number being tested add all the numbers that the modulo operation confirms as a proper positive divisor and compare that number to the tested number
In case my explication makes no sense
here is an example in javascript

function perfect(n){
    var test=0;
    for(i=0;i<n;i++){
        if(n % i==0)test+=i;
    }
    return (test==n)?true:false;
}

Don't change these.
Name: Email:
Entire Thread Thread List