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 ...
随机推荐
- (转)NSString 类的使用
官网连接地址:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Strings/Articles/Creat ...
- HashSet、LinkedHashSet和TreeSet
关键技术: HashSet采用散列函数对元素进行排序,是专门为快速查询而设计的.存入HashSet的对象必须定义hashCode方法. TreeSet采用红黑树的数据结构进行排序元素,使用它可以从Se ...
- [P2023][AHOI2009]维护序列(线段树)
题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...
- 九. 常用类库、向量与哈希1.Java基础类库
Java 的类库是 Java 语言提供的已经实现的标准类的集合,是 Java 编程的 API(Application Program Interface),它可以帮助开发者方便.快捷地开发 Java ...
- The expression being assigned to optional parameter `v2' must be a constant or default value
今天写代码的时候遇到一个问题以前没有遇到过的问题,就是当我给一个对象参数赋值默认值的时候,报错了,代码如下 public void ShowOrHiddenKuang(bool isShow,Vect ...
- 解决android模拟器连接本机服务器”Connection refused”问题
在本机用模拟器连接 localhost 的服务器不成功,经查询是我反了一个小错误. android 模拟器其本身的localhost就是它自己的ip,而如果我要连接本机的localhost则需要将 ...
- 使用React开发
阅读目录 React的组件生命周期 JSX 语法 父组件传向子组件 子组件传向父(爷)组件 getDefaultProps && getInitialState 获取真实的DOM节点 ...
- util.select.js
ylbtech-JavaScript-util: util.select.js 筛选工具 1.A,JS-效果图返回顶部 1.B,JS-Source Code(源代码)返回顶部 1.B.1, m.y ...
- 文件流:"fopen","fclose",“ftell”"fseek","fgets","fprintf" ,“feof”,"fwrite","fread"
char const* filename="D:/hello.txt"; 路径名使用的是“/”或者使用 转义字符“\\”: "fopen", FILE *fp= ...
- 2017.8.23 postgresql的外键
1.增加/删除外键的语法 ALTER TABLE t_permission ADD CONSTRAINT fkey FOREIGN KEY (fd_resid) REFERENCES t_resour ...