Home Studies Number Theory Perfect Numbers, How to Calculate?
|
Perfect Numbers, How to Calculate? |
|
|
|
Let's take a perfect number 10. We write numbers into an array from 1 to 10 in the way that odd numbers from left to write till the middle and even numbers from end to middle. Then we start to put each number into the place where belongs. If every number goes its correct place, we say that 10 is a perfect number.

The Algorithm
C Source Code #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<time.h> int Dizi_muk(long n) { unsigned int i, j, k, t, m, a[20000]; for(i=5;i<n;i++) { k = 1; for(j=0;j<i;j++) { if( (k & 1) == 1 ) // sayy tek a[(j+1)/2] = k; else // sayy çift a[i-(j+1)/2] = k; k++; } k = 2; t = i; j = 1; while(t != 2) { m = 0; while((a[m] != t) && (m<i)) m++; a[t-1] = a[m]; t = m+1; j++; } a[t-1] = 2; j++; if(j == i) printf("%d is a perfect number\n", i); } return 0; } void main(void) { long n; double time1, time2; clock_t start, end; clrscr(); printf("Perfect numbers till(max number): ");scanf("%ld",&n); start = clock(); Dizi_muk(n); end = clock(); time1 = (double)(end - start) / CLK_TCK; printf("Time elapsed: %5.4f seconds\n", time1); getch(); }
Perfect Numbers from 5 to 999 6 | 159 | 355 | 586 | 792 | 7 | 174 | 360 | 594 | 804 | 10 | 175 | 372 | 607 | 810 | 12 | 180 | 376 | 612 | 811 | 15 | 184 | 379 | 615 | 819 | 19 | 187 | 387 | 616 | 832 | 24 | 190 | 394 | 619 | 834 | 27 | 192 | 399 | 630 | 835 | 30 | 195 | 411 | 639 | 847 | 31 | 210 | 412 | 640 | 867 | 34 | 211 | 414 | 642 | 871 | 36 | 222 | 415 | 646 | 874 | 40 | 231 | 420 | 651 | 880 | 42 | 232 | 427 | 652 | 892 | 51 | 234 | 430 | 654 | 894 | 52 | 240 | 432 | 660 | 912 | 54 | 244 | 439 | 684 | 924 | 66 | 246 | 442 | 687 | 931 | 70 | 252 | 444 | 691 | 934 | 75 | 255 | 454 | 714 | 936 | 82 | 262 | 471 | 720 | 939 | 84 | 271 | 474 | 724 | 940 | 87 | 274 | 484 | 726 | 951 | 90 | 279 | 492 | 727 | 954 | 91 | 282 | 496 | 742 | 966 | 96 | 294 | 510 | 744 | 975 | 99 | 300 | 516 | 747 | 976 | 100 | 304 | 520 | 750 | 987 | 106 | 307 | 531 | 756 | 990 | 114 | 310 | 532 | 762 | 994 | 120 | 324 | 544 | 766 | 999 | 132 | 327 | 546 | 772 | | 135 | 330 | 555 | 775 | | 136 | 331 | 559 | 780 | | 147 | 339 | 562 | 784 | |
There are 175 perfect numbers. Elapsed time to generate perfect numbers till 1000 is 0,934 seconds using Celeron 500 MHz CPU.
|
|
|