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

后端 未结 20 1108
醉梦人生
醉梦人生 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:34

    Break down the number as a prime factorization.

    All primes less than 20 are:

    2,3,5,7,11,13,17,19
    

    So the bare minimum number that can be divided by these numbers is:

    2*3*5*7*11*13*17*19
    

    Composites:

    4,6,8,9,10,12,14,15,16,18,20 = 2^2, 2*3, 2^3, 3^2, 2*5, 2^2*3, 2*7, 3*5, 2*3^2, 2^2*5
    

    Starting from the left to see which factors needed:

    • 2^3 to build 4, 8, and 16
    • 3 to build 9
    • Prime factorization: 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19 = 232,792,560

提交回复
热议问题