zoukankan      html  css  js  c++  java
  • (Problem 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.

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<ctype.h>
    #include<stdlib.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\n",sum);
    
    	return 0;
    }
    

    Answer:
    142913828922

  • 相关阅读:
    代理模式
    适配器模式
    策略模式
    原型模式
    内存溢出
    jvm常用参数
    单例模式
    抽象工厂
    工厂方法模式
    选择器代码
  • 原文地址:https://www.cnblogs.com/cpoint/p/3367371.html
Copyright © 2011-2022 走看看