2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-19 11:38:05 +02:00
This commit is contained in:
Martin Thoma 2013-11-04 12:32:07 +01:00
parent 13a8a4bdea
commit 6a645ad80d
12 changed files with 288 additions and 0 deletions

View file

@ -0,0 +1,49 @@
module Arithmetik where
-- Aufgabe 1.1
-- Berechnung der Potenz durch e-faches multiplizieren von b
-- Benötigt e Rekursionsschritte
pow1 :: Double -> Int -> Double
pow1 b 0 = 1
pow1 b e = b * (pow1 b (e-1))
-- Aufgabe 1.2
-- Berechnung der Potenz pow2(b,e) = b^e
-- Benötigt O(log_2(e)) Rekursionsschritte
pow2 :: Double -> Int -> Double
pow2 b 0 = 1
pow2 b e = if odd e
then b * pow2 b (e-1)
else pow2 (b*b) (quot e 2)
-- Aufgabe 1.3
-- Berechnung der Potenz pow3(b,e) = b^e mit einer Hilfsfunktion
pow3 :: Double -> Integer -> Double
pow3 b e
| e < 0 = error "Der Exponent muss nicht-negativ sein"
| otherwise = pow3h b e 1 where
pow3h b e acc
| e == 0 = acc
| odd e = pow3h b (e-1) (acc*b)
| otherwise = pow3h (b*b) (quot e 2) acc
-- Aufgabe 1.4
-- Suche größte natürliche Zahl x, sodass x^e <= r
-- Prinzipiell könnte e auch Double sein, aber wenn x und e
-- natürliche Zahlen sind, könnte man o.B.d.A r abrunden.
root :: Int -> Int -> Int
root e r = rootH 0 r
where rootH a b
| b-a == 1 = a
| floor (pow1 (fromIntegral(quot (a+b) 2)) e) <= r = rootH (quot (a+b) 2) b
| otherwise = rootH a (quot (a+b) 2)
-- Aufgabe 1.5: Primzahlcheck
isPrime :: Integer -> Bool
isPrime 0 = False
isPrime 1 = False
isPrime x = not (hasDivisor (root 2 x) 2)
where hasDivisor upperBound i
| i > upperBound = False
| mod x i == 0 = True
| otherwise = hasDivisor upperBound (i+1)

View file

@ -0,0 +1,9 @@
SOURCE = Minted-Haskell
make:
pdflatex -shell-escape $(SOURCE).tex -output-format=pdf
pdflatex -shell-escape $(SOURCE).tex -output-format=pdf
make clean
clean:
rm -rf $(TARGET) *.class *.html *.log *.aux *.out *.glo *.glg *.gls *.ist *.xdy *.1 *.toc *.pyg

View file

@ -0,0 +1,34 @@
\documentclass[a4paper,12pt]{article}
\usepackage{amssymb} % needed for math
\usepackage{amsmath} % needed for math
\usepackage[utf8]{inputenc} % this is needed for german umlauts
\usepackage[ngerman]{babel} % this is needed for german umlauts
\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
\usepackage[margin=2cm]{geometry} %layout
\usepackage{minted} % needed for the inclusion of source code
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{Martin Thoma, Tutorium 4}
\rhead{Programmierparadigmen, Blatt 1}
\begin{document}
\renewcommand{\theFancyVerbLine}{
\sffamily\textcolor[rgb]{0.5,0.5,0.5}{\scriptsize\arabic{FancyVerbLine}}}
\inputminted[linenos,
numbersep=7pt,
gobble=0,
frame=lines,
framesep=2mm,
label=Arithmetik.hs,
fontsize=\footnotesize, tabsize=4]{haskell}{Arithmetik.hs}
\clearpage
\inputminted[linenos,
numbersep=7pt,
gobble=0,
frame=lines,
framesep=2mm,
label=ColorClassification.java,
fontsize=\footnotesize, tabsize=4]{haskell}{Sort.hs}
\end{document}

View file

@ -0,0 +1,54 @@
module Sort where
-- Aufgabe 2
-- insert list el: sorts an elmenet el into a sorted list
insert :: (Ord t) => [t] -> t -> [t]
insert [] x = [x]
insert [a] x
| x < a = [x, a]
| otherwise = [a, x]
insert (a:b:qs) x
| x < a = [x,a,b] ++ qs
| x < b = [a,x,b] ++ qs
| otherwise = [a,b] ++ insert qs x
-- sortH q r: Sorts an unsorted list r into a sorted list q
insertH :: (Ord t) => [t] -> [t] -> [t]
insertH q [] = q
insertH q [r] = insert q r
insertH q (r:rest) = insertH (insert q r) rest
-- insertSort list: sorts list
insertSort :: (Ord t) => [t] -> [t]
insertSort [] = []
insertSort [a] = [a]
insertSort (a:qs) = insertH [a] qs
-- Aufgabe 3
merge :: (Ord t) => [t] -> [t] -> [t]
merge [] x = x
merge x [] = x
merge (x:xs) (y:ys)
| x <= y = x : merge xs (y:ys)
| otherwise = y : merge ys (x:xs)
mergeSort :: (Ord t) => [t] -> [t]
mergeSort [] = []
mergeSort [x] = [x]
mergeSort xs = merge (mergeSort top) (mergeSort bottom) where
(top, bottom) = splitAt (div (length xs) 2) xs
-- Aufgabe 4
-- Teste
isSorted :: (Ord t) => [t] -> Bool
isSorted [] = True
isSorted [a] = True
isSorted (a:b:xs)
| (a <= b) && isSorted xs = True
| otherwise = False
insertSortedIsSorted :: (Ord t) => [t] -> Bool
insertSortedIsSorted xs = isSorted(insertSort xs)
mergeSortedIsSorted :: (Ord t) => [t] -> Bool
mergeSortedIsSorted xs = isSorted(mergeSort xs)