Project Euler 5 in Python - How can I optimize my solution?

后端 未结 20 1071
醉梦人生
醉梦人生 2020-11-30 08:00

I\'ve recently been working on Project Euler problems in Python. I am fairly new to Python, and still somewhat new as a programmer.

In any case, I\'ve ran into a sp

20条回答
  •  一生所求
    2020-11-30 08:57

    List comprehensions are faster than for loops.

    Do something like this to check a number:

    def get_divs(n):
        divs = [x for x in range(1,20) if n % x == 0]
        return divs
    

    You can then check the length of the divs array to see if all the numbers are present.

提交回复
热议问题