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. LeetCode Weekly Contest 18B

    1. 496. Next Greater Element I 暴力的话,复杂度也就1000 * 1000 = 1e6, 在1s的时限内完全可以. 当然,有许多优化方法,利用stack维护递减序列的方法 ...

  2. IO流读取文件内容时,出现空格的问题(未找到原因)

    import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExceptio ...

  3. bind()函数的作用

    bind()函数是Function原型上的一个属性,当某个函数调用此方法时,可以通过向bind()函数传入执行对象和调用bind的函数的参数来改变函数的执行对象 /*问题:改变func执行环境,使之输 ...

  4. hihoCoder挑战赛31

    #1595 : Numbers 时间限制:8000ms 单点时限:1000ms 内存限制:256MB 描述 给定n个整数常数c[1], c[2], ..., c[n]和一个整数k.现在需要给2k个整数 ...

  5. angular实现动态的留言板案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. 部署Seafile服务

    介绍 官网:https://www.seafile.com 客户端/服务端下载:https://www.seafile.com/download/ 中文安装教程(MySQL版):http://manu ...

  7. DBGridEh checkbox的一个问题

    function TCustomDBGridEh.CheckBeginRowMoving(MouseX, MouseY: Integer; CheckInOnly: Boolean): Boolean ...

  8. Apex语言(一)开发环境

    1.注册salesforce开发者https://developer.salesforce.com/ 2.开发者登录https://login.salesforce.com/ 3.Apex开发者工具 ...

  9. MyBatis 基础入门

    MyBatis 是一个半自动化的持久层的框架,能让开发者专注SQL本身 JDBC 连接数据库的硬编码问题,通过config,mapper配置文件解决 Mybatis开发需要关注的文件 l POJO类( ...

  10. centos7安装nginx(基础篇)

    安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境. 一. gcc 安装安装 nginx 需要先 ...