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的更多相关文章

  1. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

  2. 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 ...

  3. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 + ...

随机推荐

  1. docker(部署常见应用):docker部署mysql

    上节回顾:docker(部署常见应用):docker部署nginx docker部署mysql:5.7.26 # 下载镜像 docker pull mysql: # 查看镜像 docker image ...

  2. LeetCode Weekly Contest 22

    1. 532. K-diff Pairs in an Array 分析:由于要唯一,所以要去重,考虑k=0,时候,相同的数字需要个数大于1,所以,先用map统计个数,对于k=0,特判,对于其他的,遍历 ...

  3. 1.Ventuz 介绍

    Ventoz能做什么? Ventuz是一款实时图文包装内容创作.制作和播出控制软件.Ventuz专注于高端视听内容的制作,包括交互展示和大型活动.视频墙.广播电视在线包装及演播室舞台及灯光控制等领域. ...

  4. rem简单实现移动端适配

    rem:移动web开发 默认字体大小是16px 在<html>中设置字体大小 与em的区别: em是在父级设置字体大小受影响 移动端适配 首先获取屏幕的宽度 计算当前屏幕宽度和640的比例 ...

  5. NEFU 116 两仪剑法 【求最小公倍数】

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/status.php?problem_id=116&order=1 解题思路:求最小公倍数 #include&l ...

  6. day28 re(正则)模块

    目录 re模块有什么用? re模块的基础使用 元字符 终极 贪婪模式 非贪婪模式 re模块高级 comple match和search re.split() sub和subn 分组 re模块有什么用? ...

  7. Python编码显示中文乱码

    爬虫时出现问题: import requests data=requests.get('http://roll.news.sina.com.cn/')print(data.text) 输出结果中文显示 ...

  8. IOS:兼容ios6和低版本

    viewDidUnload在ios6开始被弃用了,所以我们在这里处理内存警告的这类问题,这个时候我们就要把相应的处理放在 didReceiveMemoryWarning中. - (void)didRe ...

  9. MySQL导入到SQLServer

    Mysql是在Linux环境服务器,MSSQL在windows服务器上 1.在MSServer中安装VPN 2.为VPN配置Mysql服务器账号 3.账号中的文件 4.在MSSQL服务器上安装mysq ...

  10. 2016年工作中遇到的问题1-10:select-for-update锁表

    1.select... for update锁表.注意事项:事务下使用,@Transactional如果用主键,只锁住1行记录如果不用主键,会锁住多条记录,mysql下测试,查询1条,锁住1行,查询2 ...