elvecent

Lab1

Oct 24th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fact n = product [1..n]
  2.  
  3. gcd' a 0 = a
  4. gcd' a b = gcd' b (a `mod` b)
  5.  
  6. sqr x = x*x
  7.  
  8. isPrime 2 = True
  9. isPrime x = x > 0 && odd x && primeIter x 3
  10.     where primeIter x y = if sqr y > x
  11.                             then True
  12.                             else x `mod` y /= 0 && primeIter x (y+2)
  13.  
  14. revInt x = iter x 0
  15.     where iter 0 res = res
  16.           iter x acc = iter (x `div` 10) (acc * 10 + x `mod` 10)
  17.  
  18. solveSqr a b c =  if d<0
  19.                  then []
  20.                  else if d == 0
  21.                  then [(-b)/(2*a)]
  22.                  else [((-b)-(sqrt d))/(2*a),((-b)+(sqrt d))/(2*a)]
  23.                  where d = b*b - 4*a*c
  24.                  
  25.                  
  26. maxSolution a b c = case solution of
  27.                    [] -> error "D<0!"
  28.                    otherwise -> maximum solution
  29.                    where solution = solveSqr a b c
  30.  
  31. root::(Double -> Double) -> Double -> Double -> Double -> Double
  32. root f a b eps = if b - a < eps
  33.                 then m
  34.                 else if f m  == 0 then m
  35.                 else if f a * f m > 0
  36.                 then root f m b eps
  37.                 else root f a m eps
  38.                 where m = (b + a)/2
Advertisement
Add Comment
Please, Sign In to add comment