project euler 10 Summation of primes
来源:互联网 发布:mac压缩包打不开 编辑:程序博客网 时间:2024/05/16 14:17
题目:
https://projecteuler.net/problem=10
题意:
Summation of primes
Problem 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的素数的总和
思路:
用筛选法求出素数表,然后求和就可以了,复杂度可以认为是
代码:
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N = 2000000 + 10;bool is_prime[N];int prime[N];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", sum); return 0;}
阅读全文
0 0
- 【Project Euler】【Problem 10】Summation of primes
- project euler problem 10:Summation of primes
- 10 Summation of primes - Project Euler
- Project Euler Problem 10: Summation of primes
- project euler 10 Summation of primes
- Project Euler 010 Summation of primes
- Problem 10:Summation of primes
- Problem 10:Summation of primes
- PE 10 Summation of primes
- Problem 10 Summation of primes
- (Problem 10)Summation of primes
- projecteuler---->problem=10----Summation of primes
- Problem 10 Summation of primes (素数和)
- summation of four primes
- 110705 Summation of Four Primes
- Summation of Four Primes - PC110705
- 10168 Summation of Four Primes
- ProjectEuler-Problem10-Summation of primes
- redis安装和redis集群配置
- 如何访问虚拟机(另一台设备)上运行的 Web 项目
- Linux下配置JDK
- JDBC为什么要使用PreparedStatement而不是Statement
- 网络工程师成长日记162-网络工程师的定位
- project euler 10 Summation of primes
- 菜鸟程序员—Andriod布局
- 深圳外包及不考虑公司名单
- Nginx干货(一)隐藏Nginx标识与版本号
- 分享一个用keil5编写TI(德州仪器)TM4C123GH6PM单片机的教程(一)
- 网络工程师成长日记160-我选择放弃那些抱怨的人
- myeclipse中卸载jrebel
- java后端的代码规范
- svn使用