[Haskell-cafe] Prime numbers

Maarten Hazewinkel maarten.hazewinkel at gmail.com
Tue Dec 20 11:16:15 EST 2005


Daniel,

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?


Regards,

Maarten


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... :(
>
> Cheers,
> Daniel.
>
>
>
> --
>       /\/`) http://oooauthors.org
>      /\/_/  http://opendocumentfellowship.org
>     /\/_/
>     \/_/    I am not over-weight, I am under-tall.
>     /
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list