Project Euler:Problem 37 Truncatable primes
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and
7. Similarly we can work from right to left: 3797, 379, 37, and 3.
Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
#include <iostream>
#include <string>
using namespace std; bool prim(int a)
{
if (a == 1)
return false;
for (int i = 2; i*i <= a; i++)
{
if (a%i == 0)
return false;
}
return true;
} bool tr_prim(int a)
{
int num = a;
int count = 0;
int tmp[10] = { 0 };
while (a)
{
if (!prim(a))
return false;
count++;
tmp[count] = a % 10;
a /= 10;
}
for (int i = count; i > 1; i--)
{
num = num - tmp[i] * pow(10, i - 1);
if (!prim(num))
return false;
}
return true;
} int main()
{ int sum = 0;
for (int i = 10; i <= 1000000; i++)
{
if (tr_prim(i))
{
//cout << i << endl;
sum += i;
}
}
cout << sum << endl;
system("pause");
return 0;
Project Euler:Problem 37 Truncatable primes的更多相关文章
- Project Euler:Problem 58 Spiral primes
Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...
- 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 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 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 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- 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 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 + ...
随机推荐
- php !=和!==
今天测试了一下!=和!== <?phpheader("Content-type: text/html; charset=utf-8"); if (1!="1&quo ...
- 问题集锦 ~ CSS
#button标签点击后出现点边框 input {outline: none;} button::-moz-focus-inner {border: none;}
- 等等空格用法
平时经常用到 空格转移字符,记住一个 表示一个字符就可以了. Non-Breaking SPace 记住它是什么的缩写,更有助于我们记忆和使用.下面的字符转义自己视图翻译一下. 记录一下,空格的转 ...
- poj1111 Image Perimeters 广搜
题目大意: 输入一个矩阵,再输入其中一个“X”的位置(从1开始).从该位置向八个方向扩展,如果是“X”就可以并在一起.问最后得到的模块的周长是多少. 解题思路: 按照广搜的思路来做.用一个二维的数组标 ...
- SQL Server数据库备份的几个建议
1.定期进行数据备份(完备或差异备份)和日志备份. 2.使用压缩备份来减少磁盘空间占用和提高备份效率. 3.定期检查磁盘剩余空间和备份文件增长情况,以确保有足够空间进行下一次备份. 4.使用校验和(C ...
- JDBC+MYSQL初始学习
JDBC+MYSQL初始学习 一.学习准备 Eclipse 开发工具 + mysql数据库+navicat 数据库连接工具 Mysql的数据库连接驱动jar包 + testing测试集成+mave ...
- 励志:98岁老爷爷用Windows系统自带画图软件制作的神作
哈尔拉斯科,是一位很出名的老爷爷,他70岁才接触MS Paint(就是我们熟知的Windows自带的画图软件).他曾经是一名图形艺术家,但是之前他都是手工创作.他熟知怎么用双手进行艺术创作.但是后来, ...
- 利用chrome浏览器断电调试确定函数触发的位置
比如某天遇到这样一个问题,页面有一个按钮,上面绑定了事件可能是多个事件,然后我们点击后出现了bug,我们要如何快速定位到这个事件,如果页面只有一个js或少量的js,我们一个打开查找,也可以接受.但是如 ...
- 脚本_部署LNMP平台
#!bin/bash#功能:部署LNMP平台,实际运行脚本时,需要去除备注.#作者:liusingbonfunction menu { //定义函数menu ...
- idea中SVN的运用
1.1.1 上传代码时可以指定忽略一些上传目录 1.1.2 设置项目上传的路径 1.1.3 解决上传路径中包含“svn”目录问题 上传 从 SVN 服务器中检出代码到工作空间