project euler 10 Summation of primes
2017-12-16 13:35
447 查看
题目:
https://projecteuler.net/problem=10题意:
Summation of primesProblem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
求小于2000000的素数的总和
思路:
用筛选法求出素数表,然后求和就可以了,复杂度可以认为是O(nlogn),注意结果会爆int代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2000000 + 10; bool is_prime ; int prime ; int prime_table(int n) { for(int i = 0; i < N; ++i) is_prime[i] = true; is_prime[0] = is_prime[1] = false; int cnt = 0; for(int i = 2; i < n; ++i) { if(is_prime[i]) { prime[cnt++] = i; for(int j = 2*i; j < n; j += i) is_prime[j] = false; } } return cnt; } int main() { int cnt = prime_table(N); ll sum = 0; for(int i = 0; i < cnt; ++i) { if(prime[i] >= 2000000) break; sum += prime[i]; } printf("%lld\n" 9e90 , sum); return 0; }
相关文章推荐
- 【Project Euler】【Problem 10】Summation of primes
- Project Euler 10 Summation of primes
- projecteuler---->problem=10----Summation of primes
- project euler problem 10:Summation of primes
- Problem 10 Summation of primes
- Project Euler 010 Summation of primes
- projecteuler---->problem=10----Summation of primes
- Project Euler Problem 10: Summation of primes
- 110705 Summation of Four Primes
- PE 010 Summation of primes
- UVA10168 Summation of Four Primes【筛选法】
- UVa 10168 Summation of Four Primes (“1+1+1+1”问题)
- UVA 10168 Summation of Four Primes(数论)
- UVA-10168 Summation of Four Primes 哥德巴赫猜想
- 10168 Summation of Four Primes
- Problem 10 of Find the sum of all the primes below two million.
- 数论刷题-uva【10168】- Summation of Four Primes
- UVA 10168 Summation of Four Primes(数论)
- virtual hust 2013.6.21 NEFU 挑战编程----数论 E - Summation of Four Primes
- (数论2.1.2)UVA 10168 Summation of Four Primes(欧拉筛法)