本帖最后由 B1tetheDust 于 2022-3-16 19:43 编辑 import time
import math
# Method 1-------------------------------------------------------------------------------------------------------------#
def func1(num_range):
divisors = [i for i in range(1, num_range + 1)]
is_divisible = min_div_num = 0
while not is_divisible:
min_div_num += 30
is_divisible = min((min_div_num % num == 0) for num in divisors)
return min_div_num
# Method 2-------------------------------------------------------------------------------------------------------------#
def find_prime(a_num):
is_prime = 1
for i in range(2, int(math.sqrt(a_num) + 1)):
if a_num % i == 0:
is_prime = 0
return is_prime
def func2(num_range):
divisors = [i for i in range(1, num_range + 1)]
primes = [j for j in divisors if find_prime(j)]
mult = 1
for k in primes:
mult *= k
min_div_num = is_divisible = 0
while not is_divisible:
min_div_num += mult
is_divisible = min((min_div_num % num == 0) for num in divisors)
return min_div_num
# Method 3-------------------------------------------------------------------------------------------------------------#
def gcd(num_a, num_b):
if num_a == num_b:
return num_a
elif num_a < num_b:
num_a, num_b = num_b, num_a
return gcd(num_a, num_b)
else:
mod_a, mod_b = num_a & 1, num_b & 1
if mod_a and mod_b:
return gcd(num_b, num_a - num_b)
elif not mod_a and mod_b:
return gcd(num_a >> 1, num_b)
elif mod_a and not mod_b:
return gcd(num_a, num_b >> 1)
else:
return gcd(num_a >> 1, num_b >> 1) << 1
def lcm(num_a, num_b):
gcdivisor = gcd(num_a, num_b)
return (num_a * num_b) / gcdivisor
def func3(num_range):
lcmultiple = 1
for i in range(1, num_range + 1):
lcmultiple = lcm(int(lcmultiple), int(i))
return lcmultiple
# Method 4-------------------------------------------------------------------------------------------------------------#
def gcd1(num1, num2):
if num1 < num2:
num1, num2 = num2, num1
while num2 != 0:
num1, num2 = num2, num1 % num2
return num1
def lcm1(num1, num2):
gcdivisor = gcd1(num1, num2)
a = num1 * num2 / gcdivisor
return a
def func4(num_range):
lcmultiple = 1
for i in range(1, num_range + 1):
lcmultiple = lcm1(int(lcmultiple), int(i))
return lcmultiple
#----------------------------------------------------------------------------------------------------------------------#
test_num = 20
#----------------------------------------------------------------------------------------------------------------------#
start1 = time.perf_counter()
print('The smallest positive number is %d' % func1(test_num))
print('Method 1 takes %f s' % (time.perf_counter() - start1))
#----------------------------------------------------------------------------------------------------------------------#
start2 = time.perf_counter()
print('The smallest positive number is %d' % func2(test_num))
print('Method 2 takes %f s' % (time.perf_counter() - start2))
#----------------------------------------------------------------------------------------------------------------------#
start3 = time.perf_counter()
print('The smallest positive number is %d' % func3(test_num))
print('Method 3 takes %f s' % (time.perf_counter() - start3))
#----------------------------------------------------------------------------------------------------------------------#
start4 = time.perf_counter()
print('The smallest positive number is %d' % func4(test_num))
print('Method 4 takes %f s' % (time.perf_counter() - start4))
前两个是自己想的,后面两个都借鉴了别人的算法,最快的是Method 4,还是别人的方法快。Sigh. |