Project Euler Question 3 Help

后端 未结 16 1359
攒了一身酷
攒了一身酷 2020-12-08 08:25

I\'m trying to work through Project Euler and I\'m hitting a barrier on problem 03. I have an algorithm that works for smaller numbers, but problem 3 uses a very, very large

16条回答
  •  眼角桃花
    2020-12-08 09:01

    For starters, instead of beginning your search at n / 2, start it at the square root of n. You'll get half of the factors, the other half being their complement.

    eg:

    n = 27
    start at floor(sqrt(27)) = 5
    is 5 a factor? no
    is 4 a factor? no
    is 3 a factor? yes. 27 / 3 = 9. 9 is also a factor.
    is 2 a factor? no.
    factors are 3 and 9.
    

提交回复
热议问题