Writing a function isPrime using recursion.

I'm supposed to write a function isPrime that checks whether or not a given integer is a prime number or not. The function has to use recursion. The only advice I was given, was to use a helper function. I still have no clue how to do it :confused: I'm new to Haskell by the way..please help.. -- View this message in context: http://www.nabble.com/Writing-a-function-isPrime-using-recursion.-tp20003309... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Hello Kalashnikov, Thursday, October 16, 2008, 2:41:05 AM, you wrote:
I'm supposed to write a function isPrime that checks whether or not a given integer is a prime number or not. The function has to use recursion. The only advice I was given, was to use a helper function.
seems that russian institutes also started to teach haskell :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

At the risk of doing someone's homework... A naive solution is to do trial division by all integers from 2 up to sqrt n. {- isPrime :: Integer -> BoolisPrime n | n < 2 = False | otherwise = f 2 n where f k n = if k > isqrt then True else undefined -- exercise for the reader -} and where isqrt n returns floor (sqrt n) Here, f is the helper function, and is only in scope inside the definition of isPrime. This is a common haskell idiom- a helper function that is not quite general purpose enough to be made a standalone function can be defined "on the fly" and doesn't need a name or type signature. You could fancy this up to make it more efficient. I've also seen probabilistic functions that can handle huge numbers, but I can't remember if they are recursive.
participants (3)
-
Bulat Ziganshin
-
Kalashnikov
-
Nathan Bloomfield