(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 ...
随机推荐
- linux下安装python3.3.4
下载安装包 # wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz 解压 # tar -xzvf Python-3.3.4.tgz ...
- C模块回调Lua函数的两种方法
作者:ani_di 版权所有,转载务必保留此链接 http://blog.csdn.net/ani_di C模块回调Lua函数的两种方法 lua和C通过虚拟栈这种交互方式简单而又可靠,缺点就是C做栈平 ...
- HBA简介及原理
HBA,即主机总线适配器英文“Host Bus Adapter”缩写.是一个使计算机在服务器和存储装置间提供输入/输出(I/O)处理和物理连接的电路板和/或集成电路适配器. 简介 主机总线适配器(Ho ...
- golang printf
1: 打印包括字段在内的实例的完整信息 同 %+V fmt.Printf("Hello world! %v","hufeng") 输出:Hello world ...
- docker基础入门之二
一.docker文件系统: linuxFS包括boot file system 和 root file system boot file system (bootfs),包含bootloader和ke ...
- 关于ue上传图片到七牛云设置key
多图上传设置key: dialogs文件下面,image文件下面的image.html,链接webuploader.js,不链接webuploader.min.js webuploader.js里面 ...
- Jquery 方法大全
一.JQuery常用的方法 :(JQuery中90%都是方法,没有参数是获取,带参数是设置) $("#id").css('backgroundColor','blue'); .cs ...
- MATLAB - 为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像?
Q:为什么imshow(g,[])可以正常显示,而imshow(g)却显示空白图像? A:数据类型如果是double,imshow的处理范围是0-1数据类型如果是uint8,imshow的处理范围是0 ...
- 解决mac 10.11 以后 无法使用未签名第三驱动
解决 最新版 mac 系统 无法使用未签名第三驱动 10.12.多 我的情况是 10.11.4 Beta (15E27e) 使用绿联usb网卡不正常. 下面的命令为检测驱动是否装载的一些命令.sudo ...
- [NOIP2013提高组] CODEVS 3287 火车运输(MST+LCA)
一开始觉得是网络流..仔细一看应该是最短路,再看数据范围..呵呵不会写...这道题是最大生成树+最近公共祖先.第一次写..表示各种乱.. 因为要求运输货物质量最大,所以路径一定是在最大生成树上的.然后 ...