zoukankan      html  css  js  c++  java
  • 欧拉计划6-10题

    6、Sum square difference

    Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

    Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

    前十个自然数的平方和是:

    12 + 22 + ... + 102 = 385

    前十个自然数的和的平方是:

    (1 + 2 + ... + 10)2 = 552 = 3025

    所以平方和与和的平方的差是3025 − 385 = 2640.

    找出前一百个自然数的平方和与和平方的差。

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
      
    #define N 100
      
    int powplus(int n, int k)
    {
        int s=1;
        while(k--)
        {
           s*=n;
        }
      return s;
    }
      
    int sum1(int n)
    {
       return  powplus((n+1)*n/2,2);
    } 
      
    int sum2(int n)
    {
       return (n*(n+1)*(2*n+1))/6;
    }
      
    void solve()
    {
         printf("%d
    ",sum1(N));
         printf("%d
    ",sum2(N));
         printf("%d
    ",sum1(N)-sum2(N));
    } 
      
    int main()
    {
      solve();
      return 0;
    }
    View Code
    Answer:25164150
    Completed on Tue, 2 Apr 2013, 06:57
     

     7、10001st prime

    By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

    What is the 10 001st prime number?

    前六个质数是2,3,5,7,11和13,其中第6个是13.

    第10001个质数是多少?

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
      
    int prim(int n)
    {
       int i;
       for(i=2; i*i<=n; i++)
       {
          if(n%i==0)
            return 0;
       }
       return 1;
    }
      
    void solve(int n)
    {
      int i=2;
      int count=0;
      while(1)
      {
         if(prim(i))
         { 
           count++;
           if(count==n)
             break;
         }
         i++;
      }
      printf("%d
    ",i);
    }
      
      
    int main()
    {
      int n=10001;
      solve(n);
      return 0;
    }
    View Code
    Answer:104743

    Completed on Thu, 4 Apr 2013, 17:34


     8、Largest product in a series

    Find the greatest product of five consecutive digits in the 1000-digit number.

    73167176531330624919225119674426574742355349194934
    96983520312774506326239578318016984801869478851843
    85861560789112949495459501737958331952853208805511
    12540698747158523863050715693290963295227443043557
    66896648950445244523161731856403098711121722383113
    62229893423380308135336276614282806444486645238749
    30358907296290491560440772390713810515859307960866
    70172427121883998797908792274921901699720888093776
    65727333001053367881220235421809751254540594752243
    52584907711670556013604839586446706324415722155397
    53697817977846174064955149290862569321978468622482
    83972241375657056057490261407972968652414535100474
    82166370484403199890008895243450658541227588666881
    16427171479924442928230863465674813919123162824586
    17866458359124566529476545682848912883142607690042
    24219022671055626321111109370544217506941658960408
    07198403850962455444362981230987879927244284909188
    84580156166097919133875499200524063689912560717606
    05886116467109405077541002256983155200055935729725
    71636269561882670428252483600823257530420752963450

    找出以下这个1000位的整数中连续5个数字的最大乘积。(例如前五个数字的乘积是7*3*1*6*7=882)

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    FILE *fp;
    char *buffer;
    int i=0,j=0;
    
    fp=fopen("E://file.txt","r");
    
    char c;
    
    while((c=fgetc(fp))){
      if(c==EOF)
        break;
      else if(c!='
    ')
        i++;
    }
    buffer=(char *)malloc(i*sizeof(char));
    rewind(fp);
    while((c=fgetc(fp))){
      if(c==EOF)
        break;
      else if(c!='
    ')
        {
            *(buffer+j)=c;
            j++;
        }
    }
    findmax(buffer,i);
    }
    
    
    int findmax(char *buffer,int i)
    {
        int j=0,max=0;
    
        for(j=0;j<i-4;j++)
        {
            if(max<((buffer[j]-'0')*(buffer[j+1]-'0')*(buffer[j+2]-'0')*(buffer[j+3]-'0')*(buffer[j+4]-'0')))
                max=((buffer[j]-'0')*(buffer[j+1]-'0')*(buffer[j+2]-'0')*(buffer[j+3]-'0')*(buffer[j+4]-'0'));
    
        }
    printf("
    %d",max);
    return 0;
    
    }
    View Code
    Answer:40824
    Completed on Sun, 17 Nov 2013, 11:16
     

     9、Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

    a2 + b2 = c2

    For example, 32 + 42 = 9 + 16 = 25 = 52.

    There exists exactly one Pythagorean triplet for which a + b + c = 1000.
    Find the product abc.

    一个毕达哥拉斯三元组是一个包含三个自然数的集合,a<b<c,满足条件:

    a2 + b2 = c2

    例如:32 + 42 = 9 + 16 = 25 = 52.

    已知存在并且只存在一个毕达哥拉斯三元组满足条件a + b + c = 1000。

    找出该三元组中abc的乘积。

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<stdbool.h>
    
    void show()
    {
        int a,b,c;
        for(a=1; a<333; a++)
        {
            for(c=333; c<500; c++)
            {
                b=1000-a-c;
                if(a*a+b*b==c*c)
                {
                    printf("%d
    ",a*b*c);
                    return;
                }
            }
        }
    }
    
    int main()
    {
        show();
        return 0;
    }
    View Code
    Answer:31875000
    Completed on Wed, 24 Jul 2013, 08:53
     

     10、Summation of primes

    The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

    Find the sum of all the primes below two million.

    10以下的质数的和是2 + 3 + 5 + 7 = 17.

    找出两百万以下所有质数的和。

    #include<stdio.h>
    #include<math.h>
    #include<stdbool.h>
    
    #define N 2000000
    
    bool prim(int n)
    {
        int i;
        for(i=2; i*i<=n; i++)
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
    
    int main()
    {
        int i;
        long long sum=2;
        for(i=3; i<=N; i=i+2)
        {
            if(prim(i))
            {
                sum+=i;
            }
        }
        printf("%lld
    ",sum);
    
        return 0;
    }
    View Code
    Answer:142913828922

    Completed on Tue, 23 Jul 2013, 17:02

  • 相关阅读:
    如何保持页脚始终在页面底部
    CSS自适应宽度圆角按钮
    ACM1004
    java输出格式
    北大ACM1001题Exponentiation(求高精度幂)
    深入理解sizeof
    java之类BigDecimal
    ACM1003
    ACM1005
    C的输出格式printf
  • 原文地址:https://www.cnblogs.com/wuyudong/p/projecteuler6-10.html
Copyright © 2011-2022 走看看