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. javaWeb中URLEncoder.encode编码需要调用两次

    今天碰到一个问题,在Controller类中一个方法跳转到该类中的另一个方法,带着中文参数,在跳转之前对该参数进行编码: msg = java.net.URLEncoder.encode(msg,&q ...

  2. postgresql 常规操作以及检查备份

    一.建表时,复制源表的信息test=# test=# \d test.t1 Table "test.t1" Column | Type | Collation | Nullable ...

  3. linux编译安装protobuf2.5.0

    1.下载安装包 https://github.com/google/protobuf/releases?after=v3.0.0-alpha-4.1 找到相应的版本下载 2.解压安装包 #.tar.g ...

  4. python简易版学生管理系统

    #coding=utf- def showInfo(): print("**************") print(" 学生管理系统") print(&quo ...

  5. 《剑指offer 第二版》题解

    剑指Offer 按题号排序 面试题 3:数组中重复的数字 面试题 4:二维数组中的查找 面试题 5:替换空格 面试题 6:从头到尾打印链表 面试题 7:重建二叉树 面试题 8:二叉树的下一个节点 面试 ...

  6. Java 实时论坛 - Sym 1.3.0 发布

    简介 Sym 是一个用 Java 写的实时论坛,欢迎来体验! 初衷 Sym 的诞生是有如下几点原因: 我们想实现一种新的网络社区体验,独立博客+社区互动 大多数论坛用户体验不够现代化,想做一个和聊 Q ...

  7. hdu3861 The King’s Problem 强连通缩点+DAG最小路径覆盖

    对多校赛的题目,我深感无力.题目看不懂,英语是能懂的,题目具体的要求以及需要怎么做没有头绪.样例怎么来的都不明白.好吧,看题解吧. http://www.cnblogs.com/kane0526/ar ...

  8. MySQL 5.6 Reference Manual-14.6 InnoDB Table Management

    14.6 InnoDB Table Management 14.6.1 Creating InnoDB Tables 14.6.2 Moving or Copying InnoDB Tables to ...

  9. 【Oracle】DBMS_STATS.GATHER_TABLE_STATS分析表

    表分析,简单的说,就是收集表和索引的信息,CBO根据这些信息决定SQL最佳的执行路径.通过对表的分析,可以产生一些统计信息,通过这些信息oracle的优化程序可以进行优化. 语法: DBMS_STAT ...

  10. Windows Phone - 按钮/button 控件

    System.Windows.Controls.Button   button控件一.button控件的各种样式的展示可以通过 …… 来给控件定义公共的样式调用样式的方法:在Button控件上添加样式 ...