Sieving A Polynomial
May 30, 2017
I watch Stack Overflow and Reddit for questions about prime numbers. Most of the questions are basic, many are confused, a few are interesting, and some are just bizarre. A recent question falls in the latter category:
I need to make a list of the prime factors with multiplicity of all the numbers up to 300,001 that are equivalent to 1 modulo 4. My strategy is to make a list of the numbers 1 modulo 4, then calculate the factors of each. I don’t know how to calculate factors, but I know it’s complicated, so I figure somebody has already built a factoring program and put it on the web. Does anybody know a web API that I can call to determine the factors of a number?
For instance, if we limit the numbers to 50, the desired output is:
5 (5) 9 (3 3) 13 (13) 17 (17) 21 (3 7) 25 (5 5) 29 (29) 33 (3 11) 37 (37) 41 (41) 45 (3 3 5) 49 (7 7)
Calling a web API 75,000 times is a spectacularly wrong way to build the list, but I pointed the questioner to Wolfram|Alpha. Long-time readers of this blog know how to solve the problem directly, which we will do in today’s exercise.
Your task is to write a program to factor all the numbers up to 300,001 that are equivalent to 1 modulo 4. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Simple Python3 with lots of space for optimization, possibly
Output
In python:
Above solution takes 0.42 seconds for all numbers up to 75000 on my machine. Below solution uses a bigger wheel and goes for Brent algorithm trial division for larger numbers. Takes 0.08 seconds for first 75000.
Good ‘ol shell:
There’s factor in coreutils? That’s cool.
(And surely the spec includes 1.)
@Rutger: Please time the following function using the same parameters as your other timings:
@Jussi: Unix has had
factor
forever. It uses naive trial division.@Praxis, I never knew.
The info page for the GNU coreutils factor says it uses “the Pollard Rho algorithm” which is “particularly effective for numbers with relatively small factors” (when it’s compiled with the GNU MP library). It recommends other methods for factoring products of large primes.
@Jussi: The V7 source code is in assembler, HERE; the man page is HERE.
I’m not an assembler expert, but I see things like
that looks an awful lot like trial division (there is no division in the Pollard Rho algorithm), and something like
that looks an awful lot like trial division by odd numbers.
I also note that the man page mentions the time complexity as O(sqrt n), which is characteristic of trial division, not Pollard Rho.
I’ve always been under the impression that Unix
factor
used trial division. I’m not surprised that the GNU folks changed the algorithm.@Praxis, thanks. I see also an interfance change between V7 and GNU factor: V7 takes one argument (or none), GNU takes any number of arguments and reports on all of them.
To understate the effect, there’s a noticable difference between the running times of these (on a Red Hat server with recent coreutils, but the point should be robust):
@programmingpraxis: tried timing your reply, but it uses an undefined primes() function.
@Rutger: Sorry. I added
primes
andprod
, both of which are called by the function.Had a little more modifications to the code, but here is the result: 0.0156 seconds.
@Rutger: So to process n = 75,000, it’s 0.42 seconds for trial division by 2 and odds, a five-times improvement to 0.08 seconds for a 2,3,5,7-wheel (since n = 75,000 you never call Brent-rho), and another five-times improvement to 0.0156 for sieving. And sieving has a slower order of growth than the other two, O(log n log log n) compared to O(sqrt n). Sieving always wins!
Thank you for doing the timing comparison.
May 30th, 2017.c:
May 30th, 2017.out:
On an Apple Power Mac G4 (AGP Graphics) (450MHz processor, 1GB memory) to run the solution took:
approximately forty-six seconds on Mac OS 9.2.2 (International English) (the solution interpreted using Leonardo IDE 3.4.1);
approximately one second on Mac OS X 10.4.11 (the solution compiled using Xcode 2.2.1).
(I’m just trying to solve the problems posed by this ‘site whilst I try to get a job; I’m well aware that my solutions are far from the best – but, in my defence, I don’t have any traditional qualifications in computer science :/ )