Hi,
I'm looking for some helpful code that will be able to determine the number of perfect numbers betweeen 1 and 10000.
If you have any suggestions... thanks a lot.
Chris
Hi,
I'm looking for some helpful code that will be able to determine the number of perfect numbers betweeen 1 and 10000.
If you have any suggestions... thanks a lot.
Chris
code snippet (might have syntax error because I am very tired):
int numberof=0;
int loop;
float tester:
for(loop=1;loop<1000;loop++)
{
tester=sqrt(loop)
if tester%1==0
{
numberof++;
}
}
well, tester % 1 is always tester but you get the idea of how to use a loop and a counter to achieve the objective. Now all you have to do is work on the algorhythm to determine if the number being evaluated is a perfect number, whatever a perfect number is.
1 = 1
1 + 3 = 4
1 + 3 + 5 = 9
1 + 3 + 5 + 7 = 16
1 + 3 + 5 + 7 + 9 = 25
1 + 3 + 5 + 7 + 9 + 11 = 36
.
.
.
you get the idea
so a perfect number is the sum of a sequence of odd numbers which is the same value as a perfect square?
1 = 1 ............................................first perfect number = 1^2
1 + 3 = 4 ......................................second perfect number = 2^2
1 + 3 + 5 = 9.................................third perfect number = 3^2
1 + 3 + 5 + 7 = 16.........................fourth perfect number = 4^2
1 + 3 + 5 + 7 + 9 = 25...................fifth perfect number = 5^2
1 + 3 + 5 + 7 + 9 + 11 = 36...........sixth perfect number = 6^2
if a perfect number is a perfect square then you can check if there is a number less than the test number which, when multiplied by itself, equals the test number. You should be able to shorten the search sequence somewhat by a little trial and error. For example; you only need to check values which are less than half the test value if the test value is larger than 4, and you may be able to do better than that. You can use the pow() function to assist you if you wish or just if (i * i == testNumber), then testNumber is a perfect number.