### 作者 主題: (不用迴圈)求完全數  (閱讀 9896 次)

0 會員 與 1 訪客 正在閱讀本文。

#### shang1112

• 可愛的小學生
• • 文章數: 5 ##### (不用迴圈)求完全數
« 於: 2010-12-06 23:35 »

(公式是(2的n次方-1)*2的n次方/2    (條件:n為自然數且2的n次方-1為質數);但我也沒用C語言表示過n次方這樣的東西欸)

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b,sum=0,num=0;
for(a=29;a>0;a++)
{
for(b=1;b<a;b++)
{
if(a%b==0) sum+=b;
}
if(sum==a)
{
printf("%d=",a);
num+=1;
for(b=1;b<a;b++)
{
if(a%b==0) printf("%d+",b);
}
printf("0,\n");
}
sum=0;
if(num==3) a=0;
}
system("pause");
return 0;
}

#### TyroneYeh

• 俺是博士！
•     • 文章數: 2396
• 性別:  ##### 回覆： (不用迴圈)求完全數
« 回覆 #1 於: 2010-12-07 08:29 »

--
TyroneYeh

#### Yamaka

• 俺是博士！
•     • 文章數: 4913 ##### 回覆： (不用迴圈)求完全數
« 回覆 #2 於: 2010-12-07 08:56 »

(公式是(2的n次方-1)*2的n次方/2    (條件:n為自然數且2的n次方-1為質數);但我也沒用C語言表示過n次方這樣的東西欸)

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a,b,sum=0,num=0;
for(a=29;a>0;a++)
{
for(b=1;b<a;b++)
{
if(a%b==0) sum+=b;
}
if(sum==a)
{
printf("%d=",a);
num+=1;
for(b=1;b<a;b++)
{
if(a%b==0) printf("%d+",b);
}
printf("0,\n");
}
sum=0;
if(num==3) a=0;
}
system("pause");
return 0;
}

2^12 x (2^13 - 1) = 33550336

#### fillano

• 鑽研的研究生
•     • 文章數: 526 ##### 回覆： (不用迴圈)求完全數
« 回覆 #3 於: 2010-12-07 12:40 »

Sapere aude! Habe Mut, dich deines eigenen Verstandes zu bedienen! ist also der Wahlspruch der Aufklärung.

#### bunko

• 懷疑的國中生
•  • 文章數: 67 ##### 回覆： (不用迴圈)求完全數
« 回覆 #4 於: 2010-12-07 13:47 »
//      perfect_number.c
//  Perfect Numbers under 10000
#include <stdio.h>

int main(int argc, char **argv)
{
int a, i, sum;
printf("This Program will show Perfect Numbers under 10000:\n");

for(a=1; a <= 10000; a++) {
sum = 0;
for(i=1; i <= (a / 2) ; i++) {
if((a % i) == 0) {
sum += i;
if( sum == a) {
printf("perfect number: %4d\n", a);
break;
}
}
}
}
printf("---------------------\n");
printf("Done!\n");
return 0;
}

This Program will show Perfect Numbers under 10000:
perfect number:    6
perfect number:   24
perfect number:   28
perfect number:  496
perfect number: 2016
perfect number: 8128
perfect number: 8190
---------------------
Done!

24 不是2016也不是,8128是.8190也不是.........
===============================

#include <stdio.h>
#include <limits.h>
int main(int argc, char **argv)
{
int running = 1,count,p;
unsigned long long a, i, sum;
printf("This Program can find Perfect Numbers under %llu:\n", ULLONG_MAX );
printf("How many Pefect Numbers you want to find?");
scanf("%i", &p);
a = 0; count=0;
while(running) {
a += 2;

if( a >= ULLONG_MAX) {
running = 0;
}
sum = 0;
for(i=1; i <= (a / 2) ; i++) {
if((a % i) == 0) {
sum += i;
if( sum == a) {
count++;
printf("#%d perfect number: %20llu\n",count, a);

if( count == p) {
running = 0;
}
break;
}
}
}
}
printf("---------------------\n");
printf("Done!\n");
return 0;
}

-------------------------------------

« 上次編輯: 2010-12-07 15:34 由 bunko »

#### TyroneYeh

• 俺是博士！
•     • 文章數: 2396
• 性別:  ##### 回覆： (不用迴圈)求完全數
« 回覆 #5 於: 2010-12-07 13:52 »

--
TyroneYeh

#### bunko

• 懷疑的國中生
•  • 文章數: 67 ##### 回覆： (不用迴圈)求完全數
« 回覆 #6 於: 2010-12-07 17:29 »

//      perfect3.c
//
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
unsigned long long perf;
double i;

for(i=2.0; i <= 13.0; i++) {
perf = (unsigned long long) pow(2.0, (i-1.0)) * (pow(2.0, i) - 1.0);
printf("it maybe is a perfect number:%llu\n", perf);
}
return 0;
}
-------------------------------------------

\$ gcc -Wall -lm  perfect3.c -o perfect3

\$ ./perfect3
it maybe is a perfect number:6
it maybe is a perfect number:28
it maybe is a perfect number:120
it maybe is a perfect number:496
it maybe is a perfect number:2016
it maybe is a perfect number:8128
it maybe is a perfect number:32640
it maybe is a perfect number:130816
it maybe is a perfect number:523776
it maybe is a perfect number:2096128
it maybe is a perfect number:8386560
it maybe is a perfect number:33550336

32640到8386560 都不是, 33550336 才是真正第五個完全數.

« 上次編輯: 2010-12-07 17:31 由 bunko »

#### bunko

• 懷疑的國中生
•  • 文章數: 67 ##### 回覆： (不用迴圈)求完全數
« 回覆 #7 於: 2010-12-07 18:18 »

`//      perfect4.c#include <stdio.h>#include <math.h>int main(int argc, char **argv){ unsigned long long pa; unsigned long long sum,i; double n; int j=0,count=0; for(n=2.0; n <= 13.0; n++) { pa[j++] = (unsigned long long) pow(2.0, (n-1.0)) * (pow(2.0, n) - 1.0); } for(j=0; j <= 11; j++) { printf("it maybe is a perfect number:%llu\n", pa[j]); } printf("=================================================\n"); printf("The real perfect numbers:\n"); count = 0; for(j=0; j <= 11; j++) { sum = 0; for(i=1; i <= (pa[j]/2); i++) { if((pa[j] % i) == 0) { sum += i; } } if( sum == pa[j]) { printf("\n#%d perfect number: %20llu\n",++count, pa[j]); } } return 0;}`
----------------------------

\$gcc -Wall -lm  perfect4.c -o perfect4
\$ ./perfect4
it maybe is a perfect number:6
it maybe is a perfect number:28
it maybe is a perfect number:120
it maybe is a perfect number:496
it maybe is a perfect number:2016
it maybe is a perfect number:8128
it maybe is a perfect number:32640
it maybe is a perfect number:130816
it maybe is a perfect number:523776
it maybe is a perfect number:2096128
it maybe is a perfect number:8386560
it maybe is a perfect number:33550336
=================================================
The real perfect numbers:

#1 perfect number:                    6

#2 perfect number:                   28

#3 perfect number:                  496

#4 perfect number:                 8128

#5 perfect number:             33550336

« 上次編輯: 2010-12-07 18:23 由 bunko »

#### shang1112

• 可愛的小學生
• • 文章數: 5 ##### 回覆： (不用迴圈)求完全數
« 回覆 #8 於: 2010-12-08 18:11 »
for(a=29;a>0;a++)這行應該是正確的，因為程式跑得出前兩個數。

bunko大的第一個回覆我試著改了一下:
for(a=1; a <= 10000; a++) {
sum = 0;
for(i=1; i <= (a / 2) ; i++) {
if((a % i) == 0) {
sum += i;
if( sum == a) {
printf("perfect number: %4d\n", a);
break;
}
}
}
}
for(i=1; i <= (a / 2) ; i++) {

if((a%i)==0)和if(sum==a)改成並列關係，就可以順利的跑出來了。

(我還沒學到函數那邊;不太懂把math包含進來是?)

« 上次編輯: 2010-12-08 18:13 由 shang1112 »

#### bunko

• 懷疑的國中生
•  • 文章數: 67 ##### 回覆： (不用迴圈)求完全數
« 回覆 #9 於: 2010-12-08 20:33 »
\$gcc -Wall -lm  perfect4.c -o perfect4

« 上次編輯: 2010-12-08 20:36 由 bunko »

#### thyme

• 老人組
• 俺是博士！
•     • 文章數: 1281 ##### 回覆： (不用迴圈)求完全數
« 回覆 #10 於: 2010-12-15 13:10 »

#### bunko

• 懷疑的國中生
•  • 文章數: 67 ##### 回覆： (不用迴圈)求完全數
« 回覆 #11 於: 2010-12-15 14:26 »

`// Find Pefect Number and Calculate CPU Time    #include <stdio.h>#include <time.h>unsigned long long mkperf(unsigned int n);int is_perfect(unsigned long long p);char* get_time(void);int main(int argc, char **argv){ unsigned int i, count = 0; unsigned long long perf,last, r; unsigned long long num; unsigned long long pb; int cheat = {0, 1, 2, 4, 8, 11, 13, 22}; clock_t start, end, shstart, shend; double cpu_time_used; last = 1; for(i=1; i <= 33; i++) { perf = mkperf(i); if ( perf >= last) { // printf("i=%2d perf=%20llu\n", i, perf); num[i] = perf; } else { printf("****over in i=%2d perf=%20llu\n", i, perf); break; } last = perf; } for(i=1; i <= 33; i++) { r = num[i] % 10; if ( r == 6 || r == 8) { printf("maybe is a perfect number:%20Lu\n", num[i]); pb[count++] = num[i]; } } printf("Count = %u\n", count); printf("will check these numbers\n"); for(i=0; i<=5; i++) { printf("%20Lu\n", pb[cheat[i]]); } printf("---- Checking ----\n"); printf("Start at %s\n", get_time()); start = clock(); // Checking Loop [START] for(i=0; i <= 5; i++) { shstart = clock(); if( is_perfect(pb[cheat[i]]) ) { shend = clock(); cpu_time_used = ((double) (shend - shstart)) / CLOCKS_PER_SEC; printf("%20Lu is perfect number\n", pb[cheat[i]]); printf("at %s CPU Time Used:%f\n", get_time(), cpu_time_used); } } // Checking Loop [END] end = clock(); printf("End   at %s\n", get_time()); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("CPU Time Used = %f\n", cpu_time_used); return 0;} unsigned long long mkperf(unsigned int n){ // unsigned long long p=1; p <<= n; p--; p <<= (n-1); return p;}int is_perfect(unsigned long long p){ unsigned long long i, sum; sum = 0; for(i=1; i <= (p/2); i++) { if((p % i) == 0) { sum += i; } } if( sum == p) { return 1; } else { return 0; }}char* get_time(void){ time_t timeval; (void)time(&timeval); return ctime(&timeval);}`
--------------------------------------------------

maybe is a perfect number:                   6
maybe is a perfect number:                  28
maybe is a perfect number:                 496
maybe is a perfect number:                2016
maybe is a perfect number:                8128
maybe is a perfect number:              130816
maybe is a perfect number:              523776
maybe is a perfect number:             2096128
maybe is a perfect number:            33550336
maybe is a perfect number:           134209536
maybe is a perfect number:           536854528
maybe is a perfect number:          8589869056
maybe is a perfect number:         34359607296
maybe is a perfect number:        137438691328
maybe is a perfect number:       2199022206976
maybe is a perfect number:       8796090925056
maybe is a perfect number:      35184367894528
maybe is a perfect number:     562949936644096
maybe is a perfect number:    2251799780130816
maybe is a perfect number:    9007199187632128
maybe is a perfect number:  144115187807420416
maybe is a perfect number:  576460751766552576
maybe is a perfect number: 2305843008139952128
Count = 23
will check these numbers
6
28
496
8128
33550336
8589869056
---- Checking ----
Start at Wed Dec 15 14:17:48 2010

6 is perfect number
at Wed Dec 15 14:17:48 2010
CPU Time Used:0.000000
28 is perfect number
at Wed Dec 15 14:17:48 2010
CPU Time Used:0.000000
496 is perfect number
at Wed Dec 15 14:17:48 2010
CPU Time Used:0.000000
8128 is perfect number
at Wed Dec 15 14:17:48 2010
CPU Time Used:0.000000
33550336 is perfect number
at Wed Dec 15 14:17:49 2010
CPU Time Used:0.420000
8589869056 is perfect number
at Wed Dec 15 14:19:52 2010
CPU Time Used:121.840000
End   at Wed Dec 15 14:19:52 2010

CPU Time Used = 122.260000