8 Replies - 1588 Views - Last Post: 24 January 2010 - 07:30 AM

#1 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

prime number tester (HASKELL)

Posted 21 April 2009 - 05:27 PM

Description: the function isprime finds if the number is prime or not
isprime x | x == 2 = True
          | mod x 2 == 0 = False
          | divisibleByOdd x 3 == False = True
          | otherwise = False

divisibleByOdd x y | mod x y == 0 = True
                   | y*y <= x = divisibleByOdd x (y+2)
                   | otherwise = False


Is This A Good Question/Topic? 0
  • +

Replies To: prime number tester (HASKELL)

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: prime number tester (HASKELL)

Posted 21 April 2009 - 05:27 PM

Description: the function isprime finds if the number is prime or not
isprime x | x < 2 = False
          | x == 2 || x == 3 = True 
          | mod x 2 == 0 = False
          | divisibleByOdd x 3 == False = True
          | otherwise = False

divisibleByOdd x y | mod x y == 0 = True
                   | y*y <= x = divisibleByOdd x (y+2)
                   | otherwise = False


Was This Post Helpful? 0
  • +
  • -

#3 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:18 AM

divisors :: Int -> [Int] divisors x = [x `div` n| n <- [x,x-1..1], x `mod`n==0&&x>=0] isPrime :: Int -> String isPrime x |divisors x == [1,x] ="A Prime Number" |x<=0 = error "Not a positive prime" |otherwise ="Not A Prime Number"
Was This Post Helpful? 0
  • +
  • -

#4 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:18 AM

divisors :: Int -> [Int] divisors x = [x `div` n| n <- [x,x-1..1], x `mod`n==0&&x>=0] isPrime :: Int -> String isPrime x |divisors x == [1,x] ="A Prime Number" |x<=0 = error "Not a positive prime" |otherwise ="Not A Prime Number"
Was This Post Helpful? 0
  • +
  • -

#5 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:20 AM

divisors :: Int -> [Int] divisors x = [x `div` n| n <- [x,x-1..1], x `mod`n==0&&x>=0] --isPrime is based on divisors isPrime :: Int -> String isPrime x |divisors x == [1,x] ="A Prime Number" |x<=0 = error "Not a positive prime" |otherwise ="Not A Prime Number"
Was This Post Helpful? 0
  • +
  • -

#6 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:20 AM

the full code isnt showin..why not =S?
Was This Post Helpful? 0
  • +
  • -

#7 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:23 AM

divisors :: Int -> [Int] divisors x = [x `div` n| n <- [x,x-1..1], x `mod`n==0&&x>=0] isPrime :: Int -> String isPrime x |divisors x == [1,x] ="A Prime Number" |x<=0 = error "Not a positive prime" |otherwise ="Not A Prime Number"
Was This Post Helpful? 0
  • +
  • -

#8 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:26 AM

how do i paste code in forum? isnt working
Was This Post Helpful? 0
  • +
  • -

#9 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:30 AM

simpler way [code] divisors :: Int -> [Int] divisors x = [x `div` n| n <- [x,x-1..1], x `mod`n==0&&x>=0] isPrime :: Int -> String isPrime x |divisors x == [1,x] ="A Prime Number" |x<=0 = error "Not a positive prime" |otherwise ="Not A Prime Number" [code/]
Was This Post Helpful? 0
  • +
  • -

#10 Eman_321  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 24-January 10

Re: prime number tester (HASKELL)

Posted 24 January 2010 - 07:30 AM

what the...lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1