[Haskell-cafe] Prime numbers
maarten.hazewinkel at gmail.com
Tue Dec 20 11:16:15 EST 2005
Could it be that the arguments to either divides or mod should be reversed?
Currently it seems to be testing whether the candidate prime (n)
divides the possible factor (m).
Or am I to tired to read the code straight?
On 12/20/05, Daniel Carrera <daniel.carrera at zmsl.com> wrote:
> John Peterson wrote:
> > Add a type signature:
> > prime :: Integer -> Bool
> > It's defaulting to Int and you're getting overflows
> Thanks. Hmm... it's still not working.
> Btw, I mis-reported the problem. The offending number is 38466629, which
> is /not/ prime but the sample program reports as prime.
> 38466629 = 31 * 1240859
> The offending program is:
> prime :: Integer -> Bool
> prime n = not (factors 2 n)
> factors :: Integer -> Integer -> Bool
> factors m n | m == n = False
> | m < n = divides m n || factors (m+1) n
> divides :: Integer -> Integer -> Bool
> divides a b = (mod a b == 0)
> The math behind the program seems correct... :(
> /\/`) http://oooauthors.org
> /\/_/ http://opendocumentfellowship.org
> \/_/ I am not over-weight, I am under-tall.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe