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

  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. Matlab单元(Cell)数据的应用

    MATLAB里面的cell有的翻译为单元有的翻译为细胞型数据.它是MATLAB的一种特殊数据类型,可以将它看作是一种无所不包的广义矩阵.组成cell的元素可以是任何一种数据类型的常数或者常量,每一个元 ...

  2. android UI进阶之用ViewPager实现欢迎引导页面[转]

    ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ViewPager主要用来组织一组数据,并且通过左右滑动的方式来展示. ...

  3. zoj 2376 Ants

    #include<stdio.h> #include<stdlib.h> ]; int main(void) { int t,n,m,i,len,max,min,mx,mi; ...

  4. 解决iOS7中UITableView在使用autolayout时layoutSubviews方法导致的crash

    近期公司项目上线后,出现了大量的crash,发生在iOS7系统上,和UITableView相关: Auto Layout still required after executing -layoutS ...

  5. C#编写的windows服务安装后启动提示“服务启动后又停止了”

    使用C#编写的windows服务安装到服务器上行后进行启动时,总是提示“服务启动后又停止了”. 检查了服务逻辑是没问题,安装在开发本地也是正常,网上查了资料说是可能是服务没有注册,我检查了服务是正常注 ...

  6. JS图表组件 highcharts 简单的介绍

    把highcharts拿来做个简单的介绍,希望更多的朋友可以用到这个用来做图表的js插件. preparation Highcharts Highcharts是一个制作图表的纯Javascript类库 ...

  7. dans le quartier

    culture /kyltyr/ 文化 une école [ekɔl] un cinéma un musée une église un théâtre  [teɑtr] un opéra  [ɔp ...

  8. WTL error C3861: 'DDX_Control': identifier not found

    error C3861: 'DDX_Control': identifier not found 继承类加上     public CWinDataExchange<CMainDlg>, ...

  9. Java学习之对象实例化

    一个对象实例化过程:Person p = new Person();1,JVM会读取指定的路径下的Person.class文件,并加载进内存,并会先加载Person的父类(如果有直接的父类的情况下). ...

  10. iptables 简单配置

    通过命令 netstat -tnl 可以查看当前服务器打开了哪些端口  Ssh代码   netstat -tnl     查看防火墙设置  Ssh代码   iptables -L -n      开放 ...