[Haskell-cafe] Prime numbers

2005-12-20 Thread Daniel Carrera
Hello all, Trying to learn Haskell here... In a Haskell tutorial I found a function deciding if a number is prime: --//-- prime n = not (factors 2 n) factors m n | m == n = False | m n = divides m n || factors (m+1) n divides a b = (mod a b == 0) --//-- Reference:

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Jens Fisseler
Hi Daniel! How do I know that 38466629 is prime? What about this: 38466629 = 31 x 1240859 Regards, Jens ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Daniel Carrera
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.

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Daniel Carrera
Jens Fisseler wrote: What about this: 38466629 = 31 x 1240859 Yes, I wrote backwards. The offending program says that it's prime but it's not. Cheers, Daniel. -- /\/`) http://oooauthors.org /\/_/ http://opendocumentfellowship.org /\/_/ \/_/I am not over-weight, I am

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Jens Fisseler
Hi Daniel! You just have to change the arguments of the 'mod'-function: old: divides a b = (mod a b == 0) new: divides a b = (mod b a == 0) Regards, Jens ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Maarten Hazewinkel
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 [EMAIL PROTECTED]

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Robert Dockins
-divides a b = (mod a b == 0) +divides a b = (mod b a == 0) On Dec 20, 2005, at 11:09 AM, Daniel Carrera 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

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Malcolm Wallace
Daniel Carrera [EMAIL PROTECTED] writes: This function seems to produce a wrong result on the number 38466629 (warning, slow computation). Here is an easier-to-find problem: it tells me the number 4 is prime. Regards, Malcolm ___ Haskell-Cafe

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Daniel Carrera
Robert Dockins wrote: -divides a b = (mod a b == 0) +divides a b = (mod b a == 0) Oh, thanks. My program assumed one way to define 'divides' and the example assumed the other. When I wrote it I was thinking of (divides a) being a function that tells me if the input divides 'a'. Thanks!

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Daniel Carrera
Henning Thielemann wrote: factors :: Integer - Integer - Bool factors m n | m == n = False | m n = divides m n || factors (m+1) n Btw. I find the recursion harder to understand than the explicit definition: factors n = any (divides n) [2..(n-1)] For what it's worth, I also

Re: [Haskell-cafe] Prime numbers

2005-12-20 Thread Bill Wood
On Tue, 2005-12-20 at 16:53 +, Daniel Carrera wrote: Henning Thielemann wrote: factors :: Integer - Integer - Bool factors m n | m == n = False | m n = divides m n || factors (m+1) n Btw. I find the recursion harder to understand than the explicit definition: