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 ...
随机推荐
- 矩阵压缩写法 scipy spark.ml.linalg里都有,CRS,CCS
CRS 表示:Compressed Row Storage CCS 表示:Compressed Column Storage CRS的表示参考: https://blog.csdn.net/buptf ...
- Window mode
D3D window mode:Upper left (0,0)是左上角 OGL window mode:Lower left(0,0)是左下角 nvn API nvn::Device::SetWin ...
- ios开发中APP底部上滑不能调出如WiFi、蓝牙、播放等的设置页面的解决的方法
在开发的APP中我们通常通过手动底部上滑来调出WiFi.蓝牙.飞行模式等的设置页面.有时我们开发的APP无法调出. 解决的方法: 进入iPhone "设置" --> &quo ...
- selenium实现失败重运行
UI自动化脚本执行过程中存在非常多的不稳定性,例如网络的不稳定,浏览器无响应等等,这些失败往往并不是产品中的错误.那么这时我们往往需要对执行失败的测试用例进行多次重跑,确认其是否确实失败. 那么失败重 ...
- 2017.8.23 postgresql的外键
1.增加/删除外键的语法 ALTER TABLE t_permission ADD CONSTRAINT fkey FOREIGN KEY (fd_resid) REFERENCES t_resour ...
- [Angular] @ViewChild read custom directive and exportAs
For example we have a component: <Card ></Card> And a driective: <Card highlighted> ...
- [Algorithms] Using Dynamic Programming to Solve longest common subsequence problem
Let's say we have two strings: str1 = 'ACDEB' str2 = 'AEBC' We need to find the longest common subse ...
- [Algorithms] Solve Complex Problems in JavaScript with Dynamic Programming
Every dynamic programming algorithm starts with a grid. It entails solving subproblems and builds up ...
- 原来,多年以来,我一直是个curl/CRUD程序员
curl,就是create,update,remove,list的首字母简写.说是CRUD似乎更流行些,不过无所谓,知道是一个意思就好. curl程序员,就是增改删查程序员,中文说增删改查更加顺口. ...
- Win7删除虚拟机新建的用户怎么恢复
11月3日 VMware Workstation 7 Installed in Windows 7 AutoLogon Problem 安装VMware Workstation 7 在Windows ...