(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.
题目大意:
3797这个数很有趣。它本身是质数,而且如果我们从左边不断地裁去数字,得到的仍然都是质数:3797,797,97,7。而且我们还可以从右向左裁剪:3797,379,37,3,得到的仍然都是质数。
找出全部11个这样从左向右和从右向左都可以裁剪的质数。
注意:2,3,5和7不被认为是可裁剪的质数。
- //(Problem 37)Truncatable primes
- // Completed on Thu, 31 Oct 2013, 13:12
- // Language: C
- //
- // 版权所有(C)acutus (mail: acutus@126.com)
- // 博客地址:http://www.cnblogs.com/acutus/
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<ctype.h>
- #include<stdlib.h>
- #include<stdbool.h>
- bool isprim(int n)
- {
- int i=;
- if(n==) return false;
- for(; i*i<=n; i++)
- {
- if(n%i==) return false;
- }
- return true;
- }
- bool truncatable_prime(int n)
- {
- int i,j,t,flag=;
- char s[];
- int sum=;
- sprintf(s,"%d",n);
- int len=strlen(s);
- if(!isprim(s[]-'') || !isprim(s[len-]-'')) return false;
- for(i=; i<len-; i++)
- {
- t=s[i]-'';
- if(t== || t== || t== || t== || t== || t==) return false;
- }
- for(i=; i<len-; i++)
- {
- for(j=i; j<len-; j++)
- {
- sum+=s[j]-'';
- sum*=;
- }
- sum+=s[j]-'';
- if(!isprim(sum)) return false;
- sum=;
- }
- j=len-;
- i=;
- while(j>i)
- {
- for(i=; i<j; i++)
- {
- sum+=s[i]-'';
- sum*=;
- }
- sum+=s[i]-'';
- if(!isprim(sum)) return false;
- sum=;
- i=;
- j--;
- }
- return true;
- }
- int main()
- {
- int sum,count;
- sum=count=;
- int i=;
- while()
- {
- if(isprim(i) && truncatable_prime(i))
- {
- count++;
- sum+=i;
- //printf("%d\n",i);
- }
- i=i+;
- if(count==) break;
- }
- printf("%d\n",sum);
- return ;
- }
Answer:
|
748317 |
(Problem 37)Truncatable primes的更多相关文章
- (Problem 35)Circular primes
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, ...
- (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 ...
- (Problem 49)Prime permutations
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual ...
- (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 ...
- (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 ...
- (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 ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- (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 ...
- (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 ...
随机推荐
- Sencha Touch 2 结合HTML5的本地存储创建数据库实现增、删、改、查
大家好!我是范范.本人刚接触ST2到现在刚刚两个月,6月1号接的项目,那时才知道有Sencha Touch2这个东西,到现在两个月了期间的幸酸就不说了.今天说说在项目中用到的HTML5的本地存储.可能 ...
- Android 怎样在linux kernel 中读写文件
前言 欢迎大家我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net ...
- 联想S720/S720i通刷刷机包 Vibe V1.0
ROM介绍 基于官方最新S116底包制作,保证足够的稳定性. 增加VIBE元素,看起来更加大气.美观. 首次增加VIBE元素,720i执行起来无压力,720可能会有点卡.自行酌情刷入. 有bug请文明 ...
- 使用命令行将Excel数据表导入Mysql中的方法小结
从Excel数据表导入MySQL,已经做过好几次了,但每次都会碰到各种问题:invalid utf8 character string, data too long, ...,浪费了不少时间 为了提高 ...
- slots
class Student(object): pass s = Student() s.name = 'Michael' print(s.name) def set_age(self, age): s ...
- php基础知识(很简单一套适合零基础的朋友学习)
红色的一般都是重点,还有自己的一些废话 运算符 算术运算符: 基本运算(除数不能为0) 比较运算符: 大小比较(类型比较), 如果两个类型不一样,系统会自动转换成统一类型 赋值运算符: 基本赋值和运算 ...
- python自学笔记(十一)关于函数及书写格式
1.函数是抽象的第一步 1.1 有关高压锅 1.2 函数是抽象出来的结构,是总结,是方法 1.3 多用函数 2.如何定义函数 2.1 def是关键词, ...
- C文件操作(转载)
/*1.文件的打开(fopen函数)fopen函数用来打开一个文件,其调用的一般形式为:文件指针=fopen(文件名,文件操作方式):例如:FILE *fpinfpin=fopen("c:\ ...
- Altium designer 10如何设置标题栏
一.修改设置 1.执行Design-Document Option,打开文档属性对话框,设置其中title等参数. 2.执行Place-Text String,按TAB键,将Text属性中设置为&qu ...
- 飘逸的python - 一个最简单的服务器
python拥有这种单独起一个服务器监听端口的能力,用标准库的wsgiref就行. from wsgiref.simple_server import make_server def simple_a ...