the code for project euler #5 doesn't works

trulybright

the question of project euler #5 is: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

result=1
done=False
while not done:
    result+=1
    for x in range(2,21):
        if result%x!=0:
            break
        elif x==20:
            done=True
print(result)

but it takes too long time when it runs. well, it seems that this code doesn't work. what is the problem in this code?

furas

Because it has to be divided by 20 so you can check only multiples of 20 - this way you have to check a lot less numbers.

If number is divided by 20 so it has to be divided by 2, 10 (20=2*10), 4, 5 (20=4*5) so you don't have to check for 2,4,5,10. The same way you can eliminate other numbers.

Finally I got list 20, 19, 18, 17, 16, 15, 14, 13, 11

import time

start = time.time()

result = 0
done = False

while not done:
    result += 20
    for x in [20, 19, 18, 17, 16, 15, 14, 13, 11]:
        if result % x != 0:
            break
    else:
       done = True

end = time.time()

print('result:', result)
print('time in seconds:', end-start)

Result:

result: 232792560
time in seconds: 6.6211323738098145

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Project euler 12 python code doesn't run, is it slow or what?

From Dev

Trying to complete Project Euler #5, passes all syntax checks but doesn't work

From Dev

Project Euler #5 Javascript

From Dev

Code works, but "WHERE" doesn't

From Dev

Code works for java but doesn't works for Android

From Dev

Can't find error in code (Project Euler #11 (Haskell))

From Dev

Euler's method in MATLAB: code doesn't work

From Dev

Spring Hibernate project doesn't works

From Dev

Java Code for Project Euler #12

From Dev

Why sometimes this code works and sometimes it doesn't?

From Dev

My code is inefficent, multiples of 3 and 5 from Project Euler but with a 10 second timeout conditon

From Dev

Ruby: #8 on Project Euler, solution doesn't seem to work, but I feel so close

From Dev

Project euler exercise 10 on Java. isPrime method doesn't work in the loop

From Dev

Project Euler # 8- Find biggest product of n adjacent digits in number. Code works only for some values of n

From Dev

HTML 5 Canvas not working in my project, but same code works in JSFiddle?

From Dev

Project Euler #5 in Java: stuck in the end of program

From Dev

Project Euler #1: Multiples of 3 and 5

From Dev

When testing react project npm test works but jest doesn't

From Dev

Slowdown by removing useless code (Project Euler 23)

From Dev

Project Euler - How is this haskell code so fast?

From Dev

Where is the NilClass in this code? (Project Euler #8)

From Dev

What is wrong with this code for Project Euler #3?

From Dev

Scala Shapeless Code for Project Euler #1

From Dev

Scala Shapeless Code for Project Euler #2

From Dev

Project Euler #7: is anything wrong in the code?

From Dev

Project Euler - How is this haskell code so fast?

From Dev

Where is the NilClass in this code? (Project Euler #8)

From Dev

What is wrong with this code for Project Euler #3?

From Dev

Project Euler Solution 9 Code Not Working

Related Related

  1. 1

    Project euler 12 python code doesn't run, is it slow or what?

  2. 2

    Trying to complete Project Euler #5, passes all syntax checks but doesn't work

  3. 3

    Project Euler #5 Javascript

  4. 4

    Code works, but "WHERE" doesn't

  5. 5

    Code works for java but doesn't works for Android

  6. 6

    Can't find error in code (Project Euler #11 (Haskell))

  7. 7

    Euler's method in MATLAB: code doesn't work

  8. 8

    Spring Hibernate project doesn't works

  9. 9

    Java Code for Project Euler #12

  10. 10

    Why sometimes this code works and sometimes it doesn't?

  11. 11

    My code is inefficent, multiples of 3 and 5 from Project Euler but with a 10 second timeout conditon

  12. 12

    Ruby: #8 on Project Euler, solution doesn't seem to work, but I feel so close

  13. 13

    Project euler exercise 10 on Java. isPrime method doesn't work in the loop

  14. 14

    Project Euler # 8- Find biggest product of n adjacent digits in number. Code works only for some values of n

  15. 15

    HTML 5 Canvas not working in my project, but same code works in JSFiddle?

  16. 16

    Project Euler #5 in Java: stuck in the end of program

  17. 17

    Project Euler #1: Multiples of 3 and 5

  18. 18

    When testing react project npm test works but jest doesn't

  19. 19

    Slowdown by removing useless code (Project Euler 23)

  20. 20

    Project Euler - How is this haskell code so fast?

  21. 21

    Where is the NilClass in this code? (Project Euler #8)

  22. 22

    What is wrong with this code for Project Euler #3?

  23. 23

    Scala Shapeless Code for Project Euler #1

  24. 24

    Scala Shapeless Code for Project Euler #2

  25. 25

    Project Euler #7: is anything wrong in the code?

  26. 26

    Project Euler - How is this haskell code so fast?

  27. 27

    Where is the NilClass in this code? (Project Euler #8)

  28. 28

    What is wrong with this code for Project Euler #3?

  29. 29

    Project Euler Solution 9 Code Not Working

HotTag

Archive