How do I solve this question? I've intuitively understood that I am required to find the LCM of the number between [1,n] but I cannot figure out how to code this without using the math library and function defining processes in Python. I did find some solutions for C++ but they are beyond my understanding.
You need to multiply the numbers that are not a divisor of the other numbers
A simple (non optimal) algorithm is to descend the list of numbers and check if any smaller number is a divisor, if it is then add it to a blacklist.
Then multiply all numbers that are not blacklisted:
N = 10
number = 1
skip = set()
for i in range(N, 0, -1):
for j in range(i-1, 1, -1):
if j in skip:
continue
if not i%j:
print(f'{j} divides {i}, skipping {j}')
skip.add(j)
if i not in skip:
number *= i
output for N=10:
5 divides 10, skipping 5
2 divides 10, skipping 2
3 divides 9, skipping 3
4 divides 8, skipping 4
30240
Well the easiest approach without any optimization would be sth like:
def int_div(n):
outp = 0
while(True):
outp += 1
res_list = []
for i in range(1,n+1): res_list.append(outp%i)
if sum(res_list) == 0:
break
return outp
But it works well just for small numbers <=16.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.