Answer the question
In order to leave comments, you need to log in
How to optimize the algorithm to perform the 5 task from the Euler project?
Good afternoon!
Wrote an algorithm to implement this task .
def euler5(limit):
hasNumberFound = False
iterator = 1
while not hasNumberFound:
num = limit * iterator
for divider in range(2, limit):
if num % divider != 0:
break
else:
hasNumberFound = True
return num
iterator += 1
print( euler5(20) )
Answer the question
In order to leave comments, you need to log in
The problem can be solved without brute force.
To begin with, take all the numbers from 2 to 20 and decompose into prime factors.
3 = 31
4 = 22
5 = 51
6 = 21·31
7 = 71
8 = 23
9 = 32
10 = 21·51
11 = 111
12 = 22·31
13 = 131
14 = 21·71
15 = 31·51
16 = 24
17 = 171
18 = 21·32
19 = 191
20 = 22·51
So why are you iterating over an iterator over 1?
Find all prime numbers <= limit and multiply. Increment the iterator by this product.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question