【ProjectEuler】ProjectEuler_021

來源:互聯網
上載者:User
// Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).// If d(a) = b and d(b) = a, where a  b, then a and b are an amicable pair and each of a and b are called amicable numbers.//// For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.//// Evaluate the sum of all the amicable numbers under 10000.#include <iostream>#include <windows.h>using namespace std;const int MAX_NUM = 10000;int number[MAX_NUM + 1];//擷取某數親和數int GetAmicableNum(int num){    //尋找此數的親和數    int amicableNum = 0;    for(int i = num / 2; i >= 1; i--)    {        if(num % i == 0)        {            amicableNum += i;        }    }    //記錄此數的親和數    number[num] = amicableNum;    //返回親和數    return amicableNum;}void F1(){    cout << "void F1()" << endl;    LARGE_INTEGER timeStart, timeEnd, freq;    QueryPerformanceFrequency(&freq);    QueryPerformanceCounter(&timeStart);    int result = 0;//親和數的數目    int tempAmicableNum = 0;    for(int i = 1; i <= MAX_NUM; i++)    {        tempAmicableNum = GetAmicableNum(i);        //是親和數        if(tempAmicableNum <= MAX_NUM && number[tempAmicableNum] == i && tempAmicableNum != i)        {            cout << number[i] << " " << i << endl;            result += i + tempAmicableNum;        }    }    cout << MAX_NUM << "以內的親和數之和為" << result << endl;    QueryPerformanceCounter(&timeEnd);    cout << "Total Milliseconds is " << (double)((double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / (double)freq.QuadPart) << endl;}//主函數int main(){    F1();    return 0;}/*void F1()220 2841184 12102620 29245020 55646232 636810000以內的親和數之和為31626Total Milliseconds is 216.438By GodMoon*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.