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.

题目大意:

3797这个数很有趣。它本身是质数,而且如果我们从左边不断地裁去数字,得到的仍然都是质数:3797,797,97,7。而且我们还可以从右向左裁剪:3797,379,37,3,得到的仍然都是质数。

找出全部11个这样从左向右和从右向左都可以裁剪的质数。
注意:2,3,5和7不被认为是可裁剪的质数。

  1. //(Problem 37)Truncatable primes
  2. // Completed on Thu, 31 Oct 2013, 13:12
  3. // Language: C
  4. //
  5. // 版权所有(C)acutus (mail: acutus@126.com)
  6. // 博客地址:http://www.cnblogs.com/acutus/
  7. #include<stdio.h>
  8. #include<math.h>
  9. #include<string.h>
  10. #include<ctype.h>
  11. #include<stdlib.h>
  12. #include<stdbool.h>
  13.  
  14. bool isprim(int n)
  15. {
  16. int i=;
  17. if(n==) return false;
  18. for(; i*i<=n; i++)
  19. {
  20. if(n%i==) return false;
  21. }
  22. return true;
  23. }
  24.  
  25. bool truncatable_prime(int n)
  26. {
  27. int i,j,t,flag=;
  28. char s[];
  29. int sum=;
  30. sprintf(s,"%d",n);
  31. int len=strlen(s);
  32.  
  33. if(!isprim(s[]-'') || !isprim(s[len-]-'')) return false;
  34.  
  35. for(i=; i<len-; i++)
  36. {
  37. t=s[i]-'';
  38. if(t== || t== || t== || t== || t== || t==) return false;
  39. }
  40.  
  41. for(i=; i<len-; i++)
  42. {
  43. for(j=i; j<len-; j++)
  44. {
  45. sum+=s[j]-'';
  46. sum*=;
  47. }
  48. sum+=s[j]-'';
  49. if(!isprim(sum)) return false;
  50. sum=;
  51. }
  52. j=len-;
  53. i=;
  54. while(j>i)
  55. {
  56. for(i=; i<j; i++)
  57. {
  58. sum+=s[i]-'';
  59. sum*=;
  60. }
  61. sum+=s[i]-'';
  62. if(!isprim(sum)) return false;
  63. sum=;
  64. i=;
  65. j--;
  66. }
  67. return true;
  68. }
  69.  
  70. int main()
  71. {
  72. int sum,count;
  73. sum=count=;
  74. int i=;
  75. while()
  76. {
  77. if(isprim(i) && truncatable_prime(i))
  78. {
  79. count++;
  80. sum+=i;
  81. //printf("%d\n",i);
  82. }
  83. i=i+;
  84. if(count==) break;
  85. }
  86. printf("%d\n",sum);
  87. return ;
  88. }
Answer:
748317

(Problem 37)Truncatable primes的更多相关文章

  1. (Problem 35)Circular primes

    The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, ...

  2. (Problem 47)Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2  7 15 = 3  5 The fi ...

  3. (Problem 49)Prime permutations

    The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...

  4. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  5. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

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

  7. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  8. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  9. (Problem 46)Goldbach's other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

随机推荐

  1. Sencha Touch 2 结合HTML5的本地存储创建数据库实现增、删、改、查

    大家好!我是范范.本人刚接触ST2到现在刚刚两个月,6月1号接的项目,那时才知道有Sencha Touch2这个东西,到现在两个月了期间的幸酸就不说了.今天说说在项目中用到的HTML5的本地存储.可能 ...

  2. Android 怎样在linux kernel 中读写文件

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  3. 联想S720/S720i通刷刷机包 Vibe V1.0

    ROM介绍 基于官方最新S116底包制作,保证足够的稳定性. 增加VIBE元素,看起来更加大气.美观. 首次增加VIBE元素,720i执行起来无压力,720可能会有点卡.自行酌情刷入. 有bug请文明 ...

  4. 使用命令行将Excel数据表导入Mysql中的方法小结

    从Excel数据表导入MySQL,已经做过好几次了,但每次都会碰到各种问题:invalid utf8 character string, data too long, ...,浪费了不少时间 为了提高 ...

  5. slots

    class Student(object): pass s = Student() s.name = 'Michael' print(s.name) def set_age(self, age): s ...

  6. php基础知识(很简单一套适合零基础的朋友学习)

    红色的一般都是重点,还有自己的一些废话 运算符 算术运算符: 基本运算(除数不能为0) 比较运算符: 大小比较(类型比较), 如果两个类型不一样,系统会自动转换成统一类型 赋值运算符: 基本赋值和运算 ...

  7. python自学笔记(十一)关于函数及书写格式

    1.函数是抽象的第一步       1.1 有关高压锅     1.2 函数是抽象出来的结构,是总结,是方法     1.3 多用函数     2.如何定义函数        2.1 def是关键词, ...

  8. C文件操作(转载)

    /*1.文件的打开(fopen函数)fopen函数用来打开一个文件,其调用的一般形式为:文件指针=fopen(文件名,文件操作方式):例如:FILE *fpinfpin=fopen("c:\ ...

  9. Altium designer 10如何设置标题栏

    一.修改设置 1.执行Design-Document Option,打开文档属性对话框,设置其中title等参数. 2.执行Place-Text String,按TAB键,将Text属性中设置为&qu ...

  10. 飘逸的python - 一个最简单的服务器

    python拥有这种单独起一个服务器监听端口的能力,用标准库的wsgiref就行. from wsgiref.simple_server import make_server def simple_a ...