目录
1.题目
2.中文翻译
3.代码
4.代码中部分程序段思路详解
4.1 素数判定高效率代码: 编辑
4.2 最小的素数是 2
4.3 python中如何计算一个数各个位的和
题目描述:
A prime number is a positive number, which is divisible by exactly two different integers. A digit prime is a prime number whose sum of digits is also prime. For example the prime number 41 is a digit prime because 4 + 1 = 5 and 5 is a prime number. 17 is not a digit prime because 1 + 7 = 8, and 8 is not a prime number. In this problem your job is to find out the number of digit primes within a certain range less than 1000000.
输入:
First line of the input file contains a single integer N (0 < N ≤ 500000) that indicates the total number of inputs. Each of the next N lines contains two integers t1 and t2 (0 < t1 ≤ t2 < 1000000).
输出:
For each line of input except the first line produce one line of output containing a single integer that indicates the number of digit primes between t1 and t2 (inclusive).
样例输入:
3
10 20
10 100
100 10000
样例输出:
1
10
576
题目描述:
素数是一个正数,正好可以被两个不同的整数整除。数字素数是一个素数,其数字之和也是素数。例如,素数41是数字素数,因为4+1=5并且5是素数。17不是数字素数,因为1+7=8,而8不是素数。在这个问题中,你的工作是找出在小于1000000的特定范围内的数字素数。
输入:
输入文本的第一行包含一个整数N(0 输出: 对于除第一行以外的每一行输入,生成一行输出,其中包含一个整数,表示t1和t2(包括t1和t2)之间的数字素数。 样例输入: 样例输出: 3
10 20
10 100
100 10000
1
10
576
3.代码
#encoding=utf-8
#判定素数
def isprime(num):
if num<2:
return False
else:
for i in range(2,int(num**1/2)+1):
if num%i==0:
return False
return True
#计算一个数各个位的和
def sum_digits(num):
result=0
str_num=str(num)
for i in str_num:
result+=int(i)
return result
#output_list存储待输出的素数个数
output_list=[]
n = int(input())
#主程序
for i in range(n):
count=0
t1, t2 = map(int, input().split())
for i in range(t1,t2+1):
if isprime(i) and isprime(sum_digits(i)):
count+=1
else:
continue
output_list.append(count)
#输出
for j in output_list:
print(j)
4.代码中部分程序段思路详解
4.1 素数判定高效率代码:
4.2 最小的素数是 2
4.3 python中如何计算一个数各个位的和