what is the fastest way to check if a number is perfect?
i am not asking for code.
Name:
Anonymous2010-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
>>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:
Anonymous2010-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;
}