Need a hint/advice as to how to factor very large numbers in JavaScript

后端 未结 2 957

My task is to produce an array containing all the prime numbers up to a 12-digit number.

I tried to emulate the Sieve of Eratosthenes by first making a function e

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-26 19:40

    up to 12 digits is 100,000,000,000. That's a lot of primes (~ N/log N = 3,948,131,653).

    So, make a sieve up to 10^6, compress it into the array of ~78,500 core primes, and use them to sieve segment by segment all the way up to your target. Use primes up to the square root of the current upper limit of the segment. The size of the segment is usually chosen so that it fits into system cache. After sieving each segment, collect its primes.

    This is known as segmented sieve of Eratosthenes.

提交回复
热议问题