找回密碼
 註冊

[請益-C++] Amicable numbers (親和數)

来源: 新聞 gimball1220 2010-10-2 18:50 只看這個作者 |閱讀模式
5 5277
本文章最後由 site-admin 於 2015-12-27 17:58 編輯 Write a program that reads in a positive integer n and prints all the amicable pairs between 1 and n. Apart from the main function, there must be the following two functions in your program:
int summingFactors( int i ): return the sum of all proper factors of i.
void display( int n, int m ): if n  220, m  284, then prints
220 = 1 + 2 + 4 + 71 + 142
The screen dialog should appear as follows:
Enter a positive integer: 2000
Amicable pairs between 1 and 2000:
220 = 1 + 2 + 4 + 71 + 142
284 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110
1184 = 1 + 2 + 5 + 10 + 11+ 22 + 55 + 110 + 121 + 242 + 605
1210 = 1 + 2 + 4 + 8 + 16 + 32 + 37 + 74 + 148 + 296 + 592
A pair of two positive integers is called an amicable (or friendly) pair if each equals to the sum of the proper divisors of the other (proper divisors means all the divisors excluding the number itself). Symbolically, a pair of two positive integers (m, n) is an amicable pair if s( m )  n and s( n )  m, where s( m ) and s( n ) are sum of proper divisors of m and n respectively. For example proper divisors of number 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110. The proper divisors of number 284 are 1, 2, 4, 71 and 142. Since
s(220)  1  2  4  5  10  11  20  22  44  55  110  284 and
s(284)  1  2  4  71  142  220,
(220, 284) is an amicable pair.
腦袋中想到的是迴圈,從1~2000 (每個數字A都跑因數和 (除了本身) , 可是程式應該會執行的有點久 = =)
要怎麼要把每一個數字的因數和所得到的新數字B再求一次因數和(除了本身)再計算是否等於原先的A數字,這裡不好想...

JSD-Google [/img] 卝卝.卝卝. .. [/url]
收藏
收藏0
原 來 " 平 淡 " 也 可 以 這 麼 美 好 ! !

網友回覆5

跳到指定樓層
本文章最後由 site-admin 於 2015-12-27 17:58 編輯 恩....
我很想幫妳試試看
但是我的英文能力真的很不好166.gif
SO 可以幫我翻譯一下嗎??

JSD-Google [/img] ▂▂.▂▂. .. [/url]
本文章最後由 site-admin 於 2015-12-27 17:58 編輯 應該不會太難..
用迴圈+陣列存取就很方便了
不過我想重點不在於能不能跑的出來..
而是要花多久時間跑完= =

JSD-Google [/img] :.| ..-- [/url]
本文章最後由 site-admin 於 2015-12-27 17:58 編輯 大概上網查了一下翻譯
然後就寫出程式[請益-C++] Amicable numbers (親和數)346
測試一下是不是您要的吧
若需要主程式在PM給我
另外沒使用動態記憶體分配
陣列數設定10000
請自行改成C++......應該不難
//-----------------------------------------------
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
       
        int i;
        int input_valut;
       
        int Factor_sum_buf[10000];
       
       
          printf("[請益-C++] Amicable numbers (親和數)4760lease keyin one number\n");
        scanf("%d",&input_valut);
          printf("You input number is %d\n",input_valut);
         
         
          for(i=1;i<input_valut;i++){                //計算因數相加
                Factor_sum_buf = sun_Factor(i);
        }
         
          for(i=1;i<input_valut;i++){
                if(i == Factor_sum_buf[Factor_sum_buf]){ //如果是親和數相同
                        if(i!=Factor_sum_buf){                                //扣除本身相同
                                printf("Amicable(%d,%d) \n",i,Factor_sum_buf);
                        }       
                }
        }
       
        system("[請益-C++] Amicable numbers (親和數)315AUSE");       
        return 0;
}
int sun_Factor(int ch){  //取因數
        int i;
        int temp_value,sum_value;
        sum_value = 0;
        for(i=1;i<ch;i++){
                temp_value = ch%i;        //取餘數為0
               
                if(!temp_value){
                        //printf("%d\n",i);       
                        sum_value += i;
                }
               
        }
        return sum_value;
}
[ 本文章最後由 小保007 於 2010-10-2 20:53 編輯 ]

JSD-Google [/img] :.| ..牜牜 [/url]
本文章最後由 site-admin 於 2015-12-27 17:58 編輯 搞定了   QQ[請益-C++] Amicable numbers (親和數)6511 [請益-C++] Amicable numbers (親和數)8657

由小老婆網管Jorsindo丩丩.丩丩. .. [/url]
原 來 " 平 淡 " 也 可 以 這 麼 美 好 ! !
本文章最後由 site-admin 於 2015-12-27 17:58 編輯 恭喜 QQ
可以交作業了
[請益-C++] Amicable numbers (親和數)1262

JSD-Google [/img] :.| ..⊥⊥ [/url]