def primes(n):
array = [i for i in range(2,n+1)]
p = 2
while p <= n:
i = 2*p
while i <= n:
array[i-2] = 0
i += p
p += 1
return [num for num in array if num > 0]
def anagram(a):
# initialize a list
anagram_list = []
for i in a:
for j in a:
if i != j and (sorted(str(i))==sorted(str(j))):
anagram_list.append(i)
return anagram_list
if __name__ == '__main__':
print("The Prime Numbers are:\n",primes(1000),"\n")
a = primes(1000)
print("Prime Numbers between 0 to 100:")
T100 = a[:25]
print(T100,"\n")
print("The Anagram elements from 0 to 100 are listed :", anagram(T100),"\n")
print("Prime Numbers between 101 to 200:")
T200 = a[25:46]
print(T200,"\n")
print("The Anagram elements from 101 to 200 are listed :",anagram(T200),"\n")
print("Prime Numbers between 201 to 300:")
T300 = a[46:62]
print(T300,"\n")
print("The Anagram elements from 201 to 300 are listed :",anagram(T300),"\n")
print("Prime Numbers between 301 to 400:")
T400 = a[62:78]
print(T400,"\n")
print("The Anagram elements from 301 to 400 are listed :",anagram(T400),"\n")
print("Prime Numbers between 401 to 500:")
T500 = a[78:95]
print(T500,"\n")
print("The Anagram elements from 401 to 500 are listed :",anagram(T500),"\n")
print()
print("Prime Numbers between 501 to 600:")
T600 = a[95:109]
print(T600,"\n")
print("The Anagram elements from 501 to 600 are listed :",anagram(T600),"\n")
print("Prime Numbers between 601 to 700:")
T700 = a[109:125]
print(T700,"\n")
print("The Anagram elements from 601 to 700 are listed :",anagram(T700),"\n")
print("Prime Numbers between 701 to 800:")
T800 = a[125:139]
print(T800,"\n")
print("The Anagram elements from 701 to 800 are listed :",anagram(T800),"\n")
print()
print("Prime Numbers between 801 to 900:")
T900 = a[139:154]
print(T900,"\n")
print("The Anagram elements from 801 to 900 are listed :",anagram(T900),"\n")
print("Prime Numbers between 901 to 1000:")
T1000 = a[154:168]
print(T1000,"\n")
print("The Anagram elements from 901 to 1000 are listed :",anagram(T1000),"\n")
输出:
The Prime Numbers are:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
Prime Numbers between 0 to 100:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
The Anagram elements from 0 to 100 are listed : [13, 17, 31, 37, 71, 73, 79, 97]
Prime Numbers between 101 to 200:
[101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
The Anagram elements from 101 to 200 are listed : [113, 131, 137, 139, 173, 179, 193, 197]
Prime Numbers between 201 to 300:
[211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
The Anagram elements from 201 to 300 are listed : [239, 293]
Prime Numbers between 301 to 400:
[307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397]
The Anagram elements from 301 to 400 are listed : [313, 331, 337, 373, 379, 397]
Prime Numbers between 401 to 500:
[401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499]
The Anagram elements from 401 to 500 are listed : [419, 491]
Prime Numbers between 501 to 600:
[503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599]
The Anagram elements from 501 to 600 are listed : []
Prime Numbers between 601 to 700:
[601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691]
The Anagram elements from 601 to 700 are listed : [613, 619, 631, 691]
Prime Numbers between 701 to 800:
[701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797]
The Anagram elements from 701 to 800 are listed : []
Prime Numbers between 801 to 900:
[809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887]
The Anagram elements from 801 to 900 are listed : []
Prime Numbers between 901 to 1000:
[907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
The Anagram elements from 901 to 1000 are listed : [919, 991]
如果您是 python 开发人员,您也可以根据需要重新构建它。如果是 Python 新手,请学习 List 的概念。