• 欧拉计划1-5题


    1、Multiples of 3 and 5

    If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

    Find the sum of all the multiples of 3 or 5 below 1000.

    题目大意:

    10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是23.

    找出1000以下的自然数中,属于3和5的倍数的数字之和。

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
       
    void solve()
    {
      int sum,i;
      sum=0;
      for(i=3; i<1000; i++)
      {
         if(i%3==0 || i%5==0)
         {
            sum+=i;
         }
      }
      printf("%d
    ",sum);
         
    }
       
    int main()
    {
      solve();
      return 0;
    }
    View Code
    Answer:233168

    Completed on Sun, 31 Mar 2013, 14:35


     2、Even Fibonacci numbers

    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

    题目大意:

    斐波那契数列中的每一项被定义为前两项之和。从1和2开始,斐波那契数列的前十项为:

    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    考虑斐波那契数列中数值不超过4百万的项,找出这些项中值为偶数的项之和。

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
      
    #define N 4000000
      
    int a[1001];
      
    void solve()
    {
       int a,b,c,n,count=2;
       a=1,c=0,b=2;
       n=3;
       while(c<=N)
       {
         c=a+b;
         if(n%2!=0)
         {
            a=c;
         }
         else
         {
            b=c;
         }
         n++;
         if(c%2==0)
         {
           count+=c;
         }
       }
       printf("%d",count);
    }
      
    int main()
    {
      solve();
      return 0;
    }
    View Code
    Answer:4613732
    Completed on Tue, 2 Apr 2013, 06:36
     

    3、Largest prime factor

    The prime factors of 13195 are 5, 7, 13 and 29.

    What is the largest prime factor of the number 600851475143 ?

    题目大意:

    13195的质数因子有5,7,13和29.

    600851475143的最大质数因子是多少?

    #include<stdio.h>
    #include<math.h>
    #include<stdbool.h>
    #define N 600851475143
    
    bool prim(int n)
    {
        int i;
        for(i=2; i*i<=n; i++)
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
    
    int main()
    {
        long long s=sqrt(N);
        while(s--)
        {
            if(s%2!=0 && prim(s) && (N%s==0))
            {
                
                printf("%lld
    ",s);
                break;
            }
        }
        return 0;
    }
    View Code
    Answer:6857
    Completed on Tue, 23 Jul 2013, 17:18
     

     4、Largest palindrome product

    A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

    Find the largest palindrome made from the product of two 3-digit numbers.

    题目大意:

    一个回文数指的是从左向右和从右向左读都一样的数字。最大的由两个两位数乘积构成的回文数是9009 = 91 * 99.

    找出最大的有由个三位数乘积构成的回文数。

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<stdbool.h>
    
    
    bool palindromic(int n)  //判断一个整数是否为回文数
    {
        char s[10];
        sprintf(s,"%d",n);  //将整数n保存在字符数组s中
        int i,len;
        len=strlen(s);
        for(i=0; i<len/2; i++)
        {
            if(s[i]!=s[len-i-1])
                return false;
        }
        return true;
    }
    
    bool have_the_factor(int n)   //判断是否含有两个3位数的因数
    {
        int s=999;
        int r,b;
        while(s>100)
        {
            if((n%s)==0 && ((n/s)>100  && (n/s)<1000))
                return true;
            s--;
        }
        return false;
    }
    
    int main()
    {
        int i=1000000;
        while(i>0)
        {
            if(palindromic(i) && have_the_factor(i))
            {
                printf("%d
    ",i);
                break;
            }
            i--;
        }
        return 0;
    }
    View Code
    Answer:906609
    Completed on Wed, 24 Jul 2013, 04:34
     

    2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

    What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

    题目大意:

    2520是最小的能被1-10中每个数字整除的正整数。

    最小的能被1-20中每个数整除的正整数是多少?

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
      
    #define N 20
      
    int gcd(int a, int b)
    {
      if(b==0)
        return a;
      else
        return gcd(b,a%b);
    }
      
    int lcm(int a, int b)
    {
      return a/(gcd(a,b))*b;
    }
      
      
    void solve()
    {
       int i,s=2;
       for(i=3; i<=N; i++)
       {
          s=lcm(s,i);
       }
       printf("%d
    ",s);
    }
      
    int main()
    {
      solve();
      return 0;
    }
    View Code
    Answer:232792560
    Completed on Tue, 2 Apr 2013, 07:21
  • 相关阅读:
    vue系列教程:插值
    docker load 镜像时出现:open /var/lib/docker/tmp/docker-import-500852078/repositories: no such file or dir
    Day 18: 记filebeat内存泄漏问题分析及调优
    Filebeat 启动关闭流程
    docker加速
    Docker镜像保存save、加载load(外网转移至内网)
    filebeat.yml(中文配置详解)
    kafka介绍
    基于统一开发平台的微服务架构转型升级之路 | 某国有大型银行案例
    转 -Filebeat + Redis 管理 LOG日志实践
  • 原文地址:https://www.cnblogs.com/wuyudong/p/projecteuler1-5.html
Copyright © 2020-2023  润新知