Find the largest prime number factor?

前端 未结 7 1761
别那么骄傲
别那么骄傲 2021-01-29 08:40

I need to find The prime factors of 13195 are 5, 7, 13 and 29. / * Largest is 377. * / What is the largest prime factor of the number 600851475143 ?

#include<         


        
7条回答
  •  野的像风
    2021-01-29 09:27

    The most important thing that is wrong here is that your code is too slow: even if you fix other issues, such as using a wrong data type for your integers and trying out some divisors that are definitely not prime, iterating by one down from 10^11 will simply not finish in your computer's lifetime is extremely wasteful.

    I highly recommend that you read through the example on page 35 of this classic book, where Dijkstra takes you through the process of writing a program printing the first 1000 prime numbers. This example should provide enough mathematical intuition to you to speed up your own calculations, including the part where you start your search from the square root of the number that you are trying to factor.

提交回复
热议问题