📚 The CoCalc Library - books, templates and other resources
License: OTHER
module Arithmetik where12-- Aufgabe 1.13-- Berechnung der Potenz durch e-faches multiplizieren von b4-- Benötigt e Rekursionsschritte5pow1 :: Double -> Int -> Double6pow1 b 0 = 17pow1 b e = b * (pow1 b (e-1))89-- Aufgabe 1.210-- Berechnung der Potenz pow2(b,e) = b^e11-- Benötigt O(log_2(e)) Rekursionsschritte12pow2 :: Double -> Int -> Double13pow2 b 0 = 114pow2 b e = if odd e15then b * pow2 b (e-1)16else pow2 (b*b) (quot e 2)1718-- Aufgabe 1.319-- Berechnung der Potenz pow3(b,e) = b^e mit einer Hilfsfunktion20pow3 :: Double -> Integer -> Double21pow3 b e22| e < 0 = error "Der Exponent muss nicht-negativ sein"23| otherwise = pow3h b e 1 where24pow3h b e acc25| e == 0 = acc26| odd e = pow3h b (e-1) (acc*b)27| otherwise = pow3h (b*b) (quot e 2) acc2829-- Aufgabe 1.430-- Suche größte natürliche Zahl x, sodass x^e <= r31-- Prinzipiell könnte e auch Double sein, aber wenn x und e32-- natürliche Zahlen sind, könnte man o.B.d.A r abrunden.33root :: Int -> Int -> Int34root e r = rootH 0 r35where rootH a b36| b-a == 1 = a37| floor (pow1 (fromIntegral(quot (a+b) 2)) e) <= r = rootH (quot (a+b) 2) b38| otherwise = rootH a (quot (a+b) 2)3940-- Aufgabe 1.5: Primzahlcheck41isPrime :: Integer -> Bool42isPrime 0 = False43isPrime 1 = False44isPrime x = not (hasDivisor (root 2 x) 2)45where hasDivisor upperBound i46| i > upperBound = False47| mod x i == 0 = True48| otherwise = hasDivisor upperBound (i+1)495051