2

I implemented the quadratic sieve in Haskell according to the basic algorithm specified on the Wikipedia page. It works great on most integers, however it fails to find a factorization on numbers N that are nth powers. For even powers (squares), the algorithm loops, and for odd powers I find several smooth numbers that are squares mod N (I have tested and confirmed this), yet every single derived congruence of squares (also tested and confirmed) leads only to a trivial factor.

I am reasonably sure that I implemented the Wikipedia algorithm to the letter. Is there a problem with that version of the algorithm that prevents it from handling nth powers, or is there a bug in my algorithm?

For some reason stackoverflow is having an issue formatting my code, so here you go: http://pastebin.com/miUxHKCh

Nick
  • 2,821
  • 5
  • 30
  • 35
  • 2
    Post the code so we can judge your implementation. – Burkhard Dec 07 '12 at 08:58
  • Ok, I can, but it's a fair chunk of code. Before I start rummaging for bugs, I wanted to see whether there was a conceptual/theoretical problem with the Wikipedia version. – Nick Dec 07 '12 at 09:32
  • Ok, I posted a link to the code. All of the functions called by the code that aren't defined in this file have been tested pretty thoroughly. `psqrt` (which finds quadratic residues mod p) works but has undefined behavior if the modulus isn't prime (but I only use it in one place, where the modulus is definitely prime). My guess is that the problem lies somewhere in `doSieve` or `sieve'` – Nick Dec 07 '12 at 09:53
  • `unSquare` looks like it'll loop infinitely for `0` and `1`. – hammar Dec 07 '12 at 10:49
  • 1
    `cs = ceiling $ (sqrt :: Float -> Float) $ fromIntegral n` <- You should definitely use `Double` there, `Float` limits you to rather small `n`. – Daniel Fischer Dec 07 '12 at 11:41
  • @hammar - that's fine, since I catch those two cases at the beginning of `qsieve`. – Nick Dec 07 '12 at 21:12
  • Hi @Nick. I was rummaging through the internet for implementations of Quadratic Sieve in Haskell for a project of mine, and was wondering if I could use and cite your implementation? Couldn't find a way to contact you hence commenting. – infiNity9819 Apr 30 '20 at 16:06

1 Answers1

3

The quadratic sieve, as I understand it, is not designed to definitely factor a number. Rather, it is designed to, in the typical case, usually factor a number.

The wikipedia entry, for example, at least as of today, describes what it presents as a "standard quadratic sieve without logarithm optimizations or prime powers". So it explicitly does not take prime powers into account.

Furthermore, as I understand it, factorization of numbers close to prime powers also doesn't work well in more efficient variations of the algorithm.

So the fault is not in your code, it is in the way the algorithm is usually presented (which glosses over issues such as whether it always works or just typically works, etc :-))

sclv
  • 38,665
  • 7
  • 99
  • 204