要求:使用enumerate() 和filter()
from math import sqrt
def isPrime(m):
for i in range(2, int(sqrt(m) + 1)):
if m % i == 0:
return False
return True
n=int(input("Please enter a 3-digit number: "))
for index, item in enumerate(filter(isPrime, list(range(2, n)))):
print(f"The {index}th prime number less than {n}: {item}")
# Output:
Please enter a 3-digit number: 345
The 0th prime number less than 345: 2
The 1th prime number less than 345: 3
The 2th prime number less than 345: 5
The 3th prime number less than 345: 7
The 4th prime number less than 345: 11
The 5th prime number less than 345: 13
The 6th prime number less than 345: 17
The 7th prime number less than 345: 19
The 8th prime number less than 345: 23
The 9th prime number less than 345: 29
The 10th prime number less than 345: 31
The 11th prime number less than 345: 37
The 12th prime number less than 345: 41
The 13th prime number less than 345: 43
The 14th prime number less than 345: 47
The 15th prime number less than 345: 53
The 16th prime number less than 345: 59
The 17th prime number less than 345: 61
The 18th prime number less than 345: 67
The 19th prime number less than 345: 71
The 20th prime number less than 345: 73
The 21th prime number less than 345: 79
The 22th prime number less than 345: 83
The 23th prime number less than 345: 89
The 24th prime number less than 345: 97
The 25th prime number less than 345: 101
The 26th prime number less than 345: 103
The 27th prime number less than 345: 107
The 28th prime number less than 345: 109
The 29th prime number less than 345: 113
The 30th prime number less than 345: 127
The 31th prime number less than 345: 131
The 32th prime number less than 345: 137
The 33th prime number less than 345: 139
The 34th prime number less than 345: 149
The 35th prime number less than 345: 151
The 36th prime number less than 345: 157
The 37th prime number less than 345: 163
The 38th prime number less than 345: 167
The 39th prime number less than 345: 173
The 40th prime number less than 345: 179
The 41th prime number less than 345: 181
The 42th prime number less than 345: 191
The 43th prime number less than 345: 193
The 44th prime number less than 345: 197
The 45th prime number less than 345: 199
The 46th prime number less than 345: 211
The 47th prime number less than 345: 223
The 48th prime number less than 345: 227
The 49th prime number less than 345: 229
The 50th prime number less than 345: 233
The 51th prime number less than 345: 239
The 52th prime number less than 345: 241
The 53th prime number less than 345: 251
The 54th prime number less than 345: 257
The 55th prime number less than 345: 263
The 56th prime number less than 345: 269
The 57th prime number less than 345: 271
The 58th prime number less than 345: 277
The 59th prime number less than 345: 281
The 60th prime number less than 345: 283
The 61th prime number less than 345: 293
The 62th prime number less than 345: 307
The 63th prime number less than 345: 311
The 64th prime number less than 345: 313
The 65th prime number less than 345: 317
The 66th prime number less than 345: 331
The 67th prime number less than 345: 337
import math
def prime(m):
for i in range(2, round(math.sqrt(m))+1):
if m % i == 0:
return
return True
n=int(input())
print(list(filter(prime, list(range(2, n)))))
可以得到列表,但是没有用enumerate() ,用这个干什么?
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
public static void main(String[] args) { boolean isSanWei = true; System.out.println("请输入一个三位整数"); //键盘输入 Scanner scanner = new Scanner(System.in); int intPut = scanner.nextInt(); //判断是否为3位数 if ((intPut >= 1000 || intPut < 100) ) { while (isSanWei) { System.out.println("输入的不是三位整数请重新输入"); scanner = new Scanner(System.in); intPut = scanner.nextInt(); if ((intPut < 1000) && (intPut >= 100)) { isSanWei = false; } } } //获取素数 int i = 2, j = 0, m; label1: for (i = 2; i < intPut; i++) { int a = (int) (Math.ceil(Math.sqrt(i))); for (m = 2; m <= a; m++) { if (i % m == 0) { continue label1; } } System.out.print(i + "\t"); j++; if (j % 5 == 0)//5个就换行 System.out.println(); } }