給你一超低效能程式碼
如果你願意等到天荒地老的話
下面這個可以用
num = int(input())
num1 = 1
a = 'num1 % 2 == 0'
for i in range(3, num + 1):
b = (f' and num1 % {i} == 0')
a = a + b
while True:
num1 += 1
if eval(a):
break
print('最小數字:', num1)
10
最小數字: 2520
搭車時突然想到的
就順便打一下
正常版土法煉鋼
效能正常
兩迴圈解決
num = int(input())
num1 = 2
a = 'num1 % 2 == 0'
for i in range(3, num + 1):
b = (f' and num1 % {i} == 0')
a = a + b
num1 *= i
for i in range(num, 1, -1):
while True:
num1 = num1 / i
if not eval(a):
num1 *= i
break
print('最小數字:', int(num1))
前面有人問的質因數
拿來用
num = int(input())
def prime_factoring(n):
divisor = 2
factors = []
while divisor * divisor <= n:
if n % divisor:
divisor += 1
else:
n //= divisor
factors.append(divisor)
if n > 1:
factors.append(n)
#利用set不重覆的特性取得質數種類
tmp_set=set(ii for ii in factors)
final=[]
for jj in tmp_set:
#利用list.count()函式取得加總結果
final.append([jj,factors.count(jj)])
return final
c = {}
for i in range(2, num + 1):
b = prime_factoring(i)
for j in range(len(b)):
if b[j][0] not in c:
c[b[j][0]] = b[j][1]
elif b[j][1] > c[b[j][0]]:
c[b[j][0]] = b[j][1]
k = 1
for i in c:
k *= i ** c[i]
k