a=[]
for i in range(2,num-1):
ifnum % i==0:
a.append(i)
if len(a)==0:
print('%d is prime'%(num))
else:
print(a) def bprime(num):
list1=[]
for i in range(2,int(num/2)+1):
if not num%i:
list1.append(i)
if list1==[]:
print('%d是质数' %num)
else:
print('%d的因子是' %num,end='')
print(list1) 不用判断素数,反正要因数返回,只要0个因数就是素数了
bprime = lambda n: or "%d is prime"%n def isPrime(n):
if n < 2:
return False
elif n == 2:
return True
else:
for i in range(2, int(n**0.5)+1):
if n%i==0:
return False
return True
def getFactors(n):
if isPrime(n):
print('{} is prime.'.format(n))
else:
allFactors = []
for i in range(2, n):
if n%i==0:
allFactors.append(i)
print(allFactors)
getFactors(10)
getFactors(30)
getFactors(13) 我是来看看两行是怎么写的 看看大神 高难度查看
看看 def bprime(n):
a = n//2 + 1
b = []
for i in range(2, a):
if n%i == 0:
b.append(i)
if len(b) == 0:
return "%d is prime"%n
else:
return b
#return None import math
def bprime(num):
#a = math.sqrt(num)
a = num//2
list_=[]
for each in range(2,num//2+1):
if num%each==0:
list_.append(each)
if list_:
print(list_)
else:
print('{} is prime'.format(num))
看看 def func(number):
number = int(number)
yes = False
for i in range(number):
if i == 0:
continue
if number % i == 0 and i != 1 and i != number:
yes = True
list = []
if yes:
for i in range(number):
if i == 0:
continue
if number % i == 0 and i != 1 and i != number:
list.append(i)
return list
else:
return("%s is prime"%str(number))
def bprime(num):
div_num=[]
if num < 2:
print('请重新输入一个大于等于2的自然数:')
num = int(input())
else:
for i in range(1,num+1):
if num % i == 0:
div_num.append(i)
if len(div_num) > 2:
return div_num
else:
return '{} is prime'.format(num)
num = int(input('请输入一个大于2的自然数:'))
print(bprime(num)) 1 学习
页:
1
[2]