Prime factor of 300 000 000 000?

前端 未结 19 679
无人及你
无人及你 2021-01-03 10:23

I need to find out the prime factors of over 300 billion. I have a function that is adding to the list of them...very slowly! It has been running for about an hour now and i

相关标签:
19条回答
  • 2021-01-03 11:01

    You could use the sieve of Eratosthenes to find the primes and see if your number is divisible by those you find.

    0 讨论(0)
  • 2021-01-03 11:02

    Semi-prime numbers of that size are used for encryption, so I am curious as to what you exactly want to use them for.

    That aside, there currently are not good ways to find the prime factorization of large numbers in a relatively small amount of time.

    0 讨论(0)
  • 2021-01-03 11:03

    Here's one site where you can get answers: Factoris - Online factorization service. It can do really big numbers, but it also can factorize algebraic expressions.

    0 讨论(0)
  • 2021-01-03 11:04

    Factoring big numbers is a hard problem. So hard, in fact, that we rely on it to keep RSA secure. But take a look at the wikipedia page for some pointers to algorithms that can help. But for a number that small, it really shouldn't be taking that long, unless you are re-doing work over and over again that you don't have to somewhere.

    For the brute-force solution, remember that you can do some mini-optimizations:

    • Check 2 specially, then only check odd numbers.
    • You only ever need to check up to the square-root of the number (if you find no factors by then, then the number is prime).
    • Once you find a factor, don't use the original number to find the next factor, divide it by the found factor, and search the new smaller number.
    • When you find a factor, divide it through as many times as you can. After that, you never need to check that number, or any smaller numbers again.
    • If you do all the above, each new factor you find will be prime, since any smaller factors have already been removed.
    0 讨论(0)
  • 2021-01-03 11:04

    It shouldn't take that long, even with a relatively naive brute force. For that specific number, I can factor it in my head in about one second.

    You say you don't want solutions(?), but here's your "subtle" hint. The only prime factors of the number are the lowest three primes.

    0 讨论(0)
  • 2021-01-03 11:06

    I spent some time on this since it just sucked me in. I won't paste the code here just yet. Instead see this factors.py gist if you're curious.

    Mind you, I didn't know anything about factoring (still don't) before reading this question. It's just a Python implementation of BradC's answer above.

    On my MacBook it takes 0.002 secs to factor the number mentioned in the question (600851475143).

    There must obviously be much, much faster ways of doing this. My program takes 19 secs to compute the factors of 6008514751431331. But the Factoris service just spits out the answer in no-time.

    0 讨论(0)
提交回复
热议问题