Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed
in such a way:
28 = 22 + 23 + 24
33 = 32 + 23 + 24
49 = 52 + 23 + 24
47 = 22 + 33 + 24
How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power?
先推断数的大致范围:sqrt(50000000)=7081
求出2~7081之间的全部质数
然后三层循环便利出全部能表示出的50000000以内的整数
#include <iostream>
#include <string>
#include <map>
using namespace std; bool isPrime[7081];
int prime[2000]; void judge()
{
for (int i = 2; i < 7081; i++)
{
if (isPrime[i])
{
for (int j = i; j*i < 7081; j++)
isPrime[j*i] = 0;
}
}
} int getPrime()
{
int count = 0;
for (int i = 2; i < 7081; i++)
{
if (isPrime[i])
{
prime[count++] = i;
}
}
return count;
} int main()
{
memset(isPrime, true, sizeof(isPrime));
judge();
int num = getPrime();
map<int, int>mp;
for (int i = 0; i < num; i++)
{
int a = prime[i] * prime[i] * prime[i] * prime[i];
if (a>50000000)
break;
for (int j = 0; j < num; j++)
{
int b = prime[j] * prime[j] * prime[j];
if (a + b>50000000)
break;
for (int k = 0; k < num; k++)
{
int c = prime[k] * prime[k];
if (a + b + c>50000000)
break;
mp[a + b + c]++;
}
}
} cout << mp.size() << endl;
system("pause");
return 0;
}
Project Euler:Problem 87 Prime power triples的更多相关文章
- Project Euler:Problem 77 Prime summations
It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + ...
- Project Euler:Problem 41 Pandigital prime
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- Project Euler:Problem 63 Powerful digit counts
The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...
- Project Euler:Problem 47 Distinct primes factors
The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...
- Project Euler:Problem 86 Cuboid route
A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...
- Project Euler:Problem 76 Counting summations
It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...
- Project Euler:Problem 89 Roman numerals
For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...
- Project Euler:Problem 37 Truncatable primes
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...
随机推荐
- Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)
转载自https://github.com/pzxwhc/MineKnowContainer/issues/25 包括:1. 栈溢出(StackOverflowError)2. 堆溢出(OutOfMe ...
- 六. 异常处理7.throw:异常的抛出
到目前为止,你只是获取了被Java运行时系统抛出的异常.然而,程序可以用throw语句抛出明确的异常.Throw语句的通常形式如下: throw ThrowableInstance;这里,Thr ...
- pr_debug、dev_dbg等动态调试二
内核版本:Linux-3.14 作者:彭东林 邮箱:pengdonglin137@163.com 下面我们简要分析 1: echo -n "file demo.c +p" > ...
- 快速创建一个的指定大小的内容全为0xFF的文件
比如需要创建一个大小为2KB,内容为全0xFF的文件 步骤只有两步: 第一步. dd if=/dev/zero of=./test.img bs=1 count=2048 第二步. 使用WinHex文 ...
- IdHTTPServer(indy10)开发REST中间件
IdHTTPServer(indy10)开发REST中间件 浏览器通过“get”方式查询数据URL样例:http://127.0.0.1:7777/query?sql=select * from t1 ...
- ubuntu配置 测试环境 记录
1 更新源 进入 /etc/apt/sources.list sudo vim进入, 更改为如下源 # See http://help.ubuntu.com/community/UpgradeNot ...
- Maven配置Spring+Hibernate Shiro权限控制项目
前言:在Eclipse中安装好Maven插件,然后创建一个Sample项目.在Eclipse中检出Shiro的官方演示样例.地址http://svn.apache.org/repos/asf/shir ...
- django 分页django-pure-pagination
虽然django自带了一个paginator,但不是很方便,我们使用django-pure-pagination github地址https://github.com/jamespacileo/dja ...
- go语言编程小tips
go语言一个比较方便的特性是你不需要显示的定义一个变量.例如,在c语言中,你想要使用一个int型变量,那么代码如下 int i; i =0; i++; 而在go语言中, i := 0; i++ 这样你 ...
- snmp弱口令引起的信息泄漏
0x00 snmp协议简介 snmp协议即简单网络管理协议(SNMP,Simple Network Management Protocol).目前一共有3个版本:V1,V2c,V3.V3是最新的版本, ...