module Arithmetik where
--import Prelude hiding (sum)
import Data.List
pow1 b 0 = 1
pow1 b e = b * pow1 b (e - 1)
pow2 b 0 = 1
pow2 b e
| even e = pow2 (b * b) (e `div` 2)
| otherwise = b * pow2 (b * b) (e `div` 2)
pow3 b e
| e < 0 = error "Exponent negativ"
| otherwise = pow3acc 1 b e
where
pow3acc acc b 0 = acc
pow3acc acc b e
| even e = pow3acc acc (b * b) (e `div` 2)
| otherwise = pow3acc (acc * b) (b * b) (e `div` 2)
sum1 [] = 0
sum1 (x:xs) = x + sum1 xs
and1 [] = True
and1 (x:xs) = x && and1 xs
sum3 xs = foldr (+) 0 xs
foldr2 f i [] = i
foldr2 f i (x:xs) = x `f` (foldr2 f i xs)
foldl2 f i [] = i
foldl2 f i (x:xs) = foldl2 f (x `f` i) xs
und True True = True
und x y = False
andr xs = foldr2 und True xs
andl xs = foldl2 und True xs
andl' xs = foldl' und True xs
sum2 xs = sumacc 0 xs
where
sumacc acc [] = acc
sumacc acc (x:xs) = (sumacc $! (acc + x)) xs
main = do
putStrLn "Hello World"
x <- getLine
putStrLn x
--print (sum2 [1..100000000])
root e r
| e < 1 = error "Zu kleiner Wurzelexponent"
| r < 0 = error "Negativer Radikand"
| otherwise = searchRoot 0 (r+1)
where searchRoot a b
| b - a == 1 = a
| x > r = searchRoot a mid
| otherwise = searchRoot mid b
where
x = pow3 mid e
mid = (a + b) `div` 2
isPrime n = isPrimeHelp n [2 .. root 2 n]
isPrimeHelp n [] = True
isPrimeHelp n (x:xs) = n `mod` x /= 0 && isPrimeHelp n xs
isPrime' n
| n < 2 = False
| otherwise = all (\x -> n `mod` x /= 0) [2 .. root 2 n]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys)
| x < y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
--mergeSort xs@[x,_,z,_] = [x,z] ++ xs
mergeSort [] = []
mergeSort [x] = [x]
mergeSort xs = merge (mergeSort a) (mergeSort b)
where half = length xs `div` 2
(a,b) = splitAt half xs