I'm completely new to Stack Overflow, and a complete newbie when it comes to coding. However, I'm working hard and trying to get creative with my self learning.
Right now, I'm trying to code an LCM function which takes multiple numbers as input, and returns the LCM (I know there is a much simpler code for the LCM of 2 numbers). However, I have seem to have hit a wall. Not sure what I'm doing wrong but my code isn't working. That is, its giving me an incorrect result.
The code is as below:
def product(sequence):
base = 1
for i in range(0,len(sequence)):
base *= sequence[i]
i += 1
return base
def is_prime(x):
if x < 2:
return True
else:
for n in range(2,x):
if x%n == 0:
return False
return True
def lcm(my_list):
base = []
new_list = sorted(my_list)
for x in range(0,len(new_list)):
for i in new_list:
for j in range(min(new_list),max(new_list)+1):
if is_prime(j) == True:
if i//j == 1 and i%j == 0:
new_list[x] = 1
elif i//j > 1 and i%j == 0:
new_list[x] == i//j
else:
new_list[x] = i
x = x+1
base.append(j)
return product(base)
I would appreciate any constructive feedback regarding my code, especially if anyone can highlight the reason this isn't working.
Thanks in advance python community!