primes returns composites
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
bsdgames (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
Binary package hint: bsdgames
Yesterday I reported that primes on my hardware yields values beyond the documented range. The man page claims:
DIAGNOSTICS
Out of range or invalid input results in an appropriate error message
being written to standard error.
I doubt that it's a security vulnerability since primes isn't on the live CD.
Thus this should result in an error message. Instead primes returns a composite number.
$ primes 7757777773 7757777787
7757777777
In j notation, q: is the factor verb and */ means "insert multiply". The precedence rule for sequences of nouns and verbs is simply right to left evaluation.
q:7757777777x
65827 117851
*/q:7757777777x
7757777777
(Or you could try Factor[7757777777] at Wolfram Alpha, or use a calculator.)
ProblemType: Bug
DistroRelease: Ubuntu 10.10
Package: bsdgames 2.17-19
ProcVersionSign
Uname: Linux 2.6.35-25-generic x86_64
Architecture: amd64
Date: Fri Feb 25 19:45:06 2011
InstallationMedia: Ubuntu 10.10 "Maverick Meerkat" - Release amd64 (20101007)
ProcEnviron:
PATH=(custom, no user)
LANG=en_US.utf8
SHELL=/bin/bash
SourcePackage: bsdgames
Indeed, the primes program uses Eratosthenes sieve using pre-calculated array of primes up to 65537, leading to possible errors (and, in this case real errors, as 65539 is also a prime) when range hits (65537+2)^2.
There are a few possible fixes, that I can think of:
a) don't support 64-bit numbers, set upper limit to UINT32_MAX
b) support 64-bit numbers, but set upper limit to (MAX(primes) + 2)**2 - 1
c) add some number of pre-calculated primes to array, and repeat b (supporting the whole 64-bit range would take 203280221 primes (which could be stored in a 4-byte uints, leading to ~800 megabytes of memory)
d) recursively use the sieve to prime itself to reach unlimited range (this adds computation time and algorithm complexity)