0007-编程打卡:将正整数分解质因数
本帖最后由 不二如是 于 2022-8-29 17:47 编辑一星答案:
from math import *
#判断n是否为素数
def isprime(n):
if n <= 1:
return 0
m = int(sqrt(n))+1
for x in range(2,m):
if n%x == 0:
return 0
return 1
#利用递归分解n并打印质因数
def bprime(n):
if isprime(n):
print(n)
else:
x = 2
while x <= int(n/2):
if n%x == 0:
print(x)
return bprime(n/x)
x = x + 1
二星答案:
#利用递归
def fishc(n, lst=[]):
primes = * int(n**0.5 + 1)
primes, primes = False, False
for i, prime in enumerate(primes):
if prime:
for j in range(i * i, int(n**0.5 + 1), i):
primes = False
primelist =
for p in primelist:
if n % p == 0:
return fishc(n / p, lst + )
return lst +
三星答案:
**** Hidden Message *****
基础语法:
https://www.bilibili.com/video/BV1c4411e77t
算法讲解:
https://www.bilibili.com/video/BV1HT4y1K7DY
页:
[1]