[Project Euler]加入歐拉 Problem 12

來源:互聯網
上載者:User
文章目錄
  • 整數分解

The sequence of triangle numbers is generated by adding the natural numbers. So the 7^(th) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

     1: 1
     3: 1,3
     6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

第一個擁有超過500個因子的三角數?

 

這個問題如果暴力求解的話,估計時間要很長的。沒有幾分鐘應該是出不來的。

對於前面的三角數,這個應該很熟悉了,它就是 n * (n + 1) / 2;

現在關鍵問題就是如何將這個數分解,暴力的方法就是從1遍曆到這個數,輪番除,計數。

 

數學裡面有關於整數分解的理論:

整數分解

維基百科,自由的百科全書

跳轉到: 導航, 搜尋

數學中,整數分解(質因子分解)問題是指:給出一個正整數,將其寫成幾個素數的乘積。例如,給出45這個數,它可以分解成32 ×5。根據算術基本定理,這樣的分解結果應該是獨一無二的。這個問題在代數學、密碼學、計算複雜性理論和量子電腦等領域中有重要意義。

 

因子分解

完整的因子列表可以根據素數分解推匯出,將冪從零不斷增加直到等於這個數。例如,因為45= 32×51,45可以被30 ×50,30×51,31×50,31×51,32×50,和32×51,或者 1,5,3,9,15,和 45整除。相對應的,素數分解只包括素數因子。參見素數分解演算法。

 

有了上面的知識,這個題的解決方嚮應該就有了。

 

對n進行不斷自加,找到一個數,裡面的所有因子的指數加一 相乘大於500.比如上面的45,其實就是(2 + 1) * (1 + 1) = 6.

 

下面就是寫代碼了:

整個代碼已耗用時間在3秒左右。還是可以接受. 裡面也包含了暴力的方法,不過暴力求解,我的機器是沒有運行出來。估計要很長時間吧。

 

寫代碼注意幾點,

特別注意,在處理數組的時候,要注意數組的下標從0開始的,所以檢測元素的時候,沒有的話,要返回負數,這樣比較好處理。前面由於沒有注意到這個問題,調試了很久o(╯□╰)o

歐拉項目第十二題#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>int isContain(int arr[], int n, int num); //檢測一個數組裡面是否包含數num, 返回位置,如果不包含返回 -1 double bruteDiv(int n);  // 暴力求解法 參數 最少多少個因子, 返回滿足的數 double mathDiv(int n); // 數論理論求解 參數 最少多少個因子, 返回滿足的數 int main(int argc, char *argv[]){    clock_t start_time=clock();     double num1 = mathDiv(500);       clock_t end_time=clock();     printf("%lf\n", num1);      printf("Running time is: %lf ms\n", (double)(end_time-start_time)/CLOCKS_PER_SEC*1000);        system("PAUSE");  return 0;}double bruteDiv(int n){    double sum = 1.0;    int tmpn = 1;    int nd = 0;        while(nd <= n)    {        sum = tmpn * (tmpn + 1) / 2;                nd = 0;                double i;                for(i = 1; i <= sum; i++)        {            if(fmod(sum, i) == 0)            {                nd++;            }        }                        tmpn++;            }        return sum; }double mathDiv(int n){    double sum = 1.0, rsum;    int tmpn = 1, nd = 1;    int arr1[100], arr2[100]; // 估算可以分解為100個質數, arr1儲存質數,arr2儲存對應質數個數             while(nd <= n)    {        int i;                for(i = 0; i < 100; i++) //初始化數組         {            arr1[i] = 0;            arr2[i] = 1;        }                sum = tmpn * (tmpn + 1) / 2;        rsum = sum;        nd = 1;                       int k = 2;                int j = 0;        while(k <= sum) // 將數進行因素分解         {            if(fmod(sum, k) == 0)            {                int index = isContain(arr1, 100, k);                if(index == -1)  // 檢測質數數組裡是否有該因子                {                    arr1[j] = k;                    arr2[j]++;                    j++;                                    }                else                {                    arr2[index]++;                }                                sum = sum / k;            }            else            k++;        }                int m = 0;        while(arr2[m] != 1)        {            nd *= arr2[m];            m++;            }                tmpn++;            }        return rsum;}int isContain(int arr[], int n, int num){    int i;    for(i = 0; i < n; i++)    {        if(arr[i] == num)        return i;    }        return -1;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.