Python 实现一个函数 return 一个列表,其中 y[i] 是素数 <= 到 i
Python implementing a function to return a list where y[i] is the number of prime numbers <= to i
c) 使用您在 (b) 部分中的函数,实现函数 primesCount(n),它给出
一个整数 n,returns 长度为 n 的列表 y,由 y[i] = 素数的数量给出
小于或等于 i,因为 i = 0, 1, . . . ,n-1。
示例 Input/Output:
输入数字:6
[0, 0, 1, 2, 2, 3]
第 (b) 部分功能:
def generatePrimes(n):
prime = [False for i in range(n)]
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i)
prime[i] = True
return prime
全部代码(b部分+c部分函数):
import math
def generatePrimes(n):
prime = [False for i in range(n)] # creates a list of n-length all consisting of F elem
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i) # unecessary
prime[i] = True #change False to True if number is prime
return prime # return the list (indexes with False are composite numbers while indexes
with True are prime number
def primesCount(n):
I = []
for i in range(n):
I.append(generatePrimes(i))
return I
n = int(input("Enter a number: "))
print(primesCount(n))
预计input/output:
input: Enter a number: 6
output: [0, 0, 1, 2, 2, 3]
实际input/output:
input: Enter a number: 6
output: [[], [False], [False, False], [False, False, True],[False, False, True, True]]
我需要的是将 False 和 True 转换为整数,例如 False + False + False + True + True = 0 + 0 + 1 + 1 = 2
我想将上面的输出转换为
[0, 0, 1, 2, 2, 3] ( [假] = 0, [假 + 假] = 0, [假, 假, 真] = 1...)
我为此绞尽脑汁好几个小时似乎无法正常工作
def generatePrimes(n):
prime = [False for i in range(n)] # creates a list of n-length all consisting of F elem
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i) # unecessary
prime[i] = True #change False to True if number is prime
final_output = []
counter = 0
for value in prime:
if prime:
counter += 1
final_output.append(counter)
return final_output
只需遍历每个值并在值为真时迭代一个计数器。把这个放在 return 素数之前。
我已经更新了你的代码。请看一下
import math
def generatePrimes(n):
prime = [False for i in range(n)] # creates a list of n-length all consisting of F elem
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i) # unecessary
prime[i] = True #change False to True if number is prime
return prime # return the list (indexes with False are composite numbers while indexes
#with True are prime number
def primesCount(n):
I = []
for i in range(n+1):
if len(generatePrimes(i)):
I.append(sum(generatePrimes(i)))
return I
n = int(input("Enter a number: "))
print(primesCount(n))
c) 使用您在 (b) 部分中的函数,实现函数 primesCount(n),它给出 一个整数 n,returns 长度为 n 的列表 y,由 y[i] = 素数的数量给出 小于或等于 i,因为 i = 0, 1, . . . ,n-1。 示例 Input/Output: 输入数字:6 [0, 0, 1, 2, 2, 3]
第 (b) 部分功能:
def generatePrimes(n):
prime = [False for i in range(n)]
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i)
prime[i] = True
return prime
全部代码(b部分+c部分函数):
import math
def generatePrimes(n):
prime = [False for i in range(n)] # creates a list of n-length all consisting of F elem
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i) # unecessary
prime[i] = True #change False to True if number is prime
return prime # return the list (indexes with False are composite numbers while indexes
with True are prime number
def primesCount(n):
I = []
for i in range(n):
I.append(generatePrimes(i))
return I
n = int(input("Enter a number: "))
print(primesCount(n))
预计input/output:
input: Enter a number: 6
output: [0, 0, 1, 2, 2, 3]
实际input/output:
input: Enter a number: 6
output: [[], [False], [False, False], [False, False, True],[False, False, True, True]]
我需要的是将 False 和 True 转换为整数,例如 False + False + False + True + True = 0 + 0 + 1 + 1 = 2
我想将上面的输出转换为 [0, 0, 1, 2, 2, 3] ( [假] = 0, [假 + 假] = 0, [假, 假, 真] = 1...)
我为此绞尽脑汁好几个小时似乎无法正常工作
def generatePrimes(n):
prime = [False for i in range(n)] # creates a list of n-length all consisting of F elem
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i) # unecessary
prime[i] = True #change False to True if number is prime
final_output = []
counter = 0
for value in prime:
if prime:
counter += 1
final_output.append(counter)
return final_output
只需遍历每个值并在值为真时迭代一个计数器。把这个放在 return 素数之前。
我已经更新了你的代码。请看一下
import math
def generatePrimes(n):
prime = [False for i in range(n)] # creates a list of n-length all consisting of F elem
I = []
for i in range(2, n):
if i > 1:
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
break
else:
I.append(i) # unecessary
prime[i] = True #change False to True if number is prime
return prime # return the list (indexes with False are composite numbers while indexes
#with True are prime number
def primesCount(n):
I = []
for i in range(n+1):
if len(generatePrimes(i)):
I.append(sum(generatePrimes(i)))
return I
n = int(input("Enter a number: "))
print(primesCount(n))