Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2012-10-29T09:52:50+00:00 → 2012-10-30T14:30:11+00:00
<language>Scheme</language> | |
Aufgabe 1 | |
<code id="508e4d9ac99d3"> | |
(define gerade | |
(lambda (x y) | |
(if (equal? (+ (modulo x 2) y) 0 ) y | |
(gerade x (+ y 1))))) | |
(define gerade-x | |
(lambda (x) | |
(gerade x 0))) | |
</code> | |
Wenn x gerade, dann 0, sonst stoppt nicht. | |
<run id="508e4d62ec556"> | |
(gerade-x 6) | |
</run> | |
<run id="508e4d62ec611"> | |
(gerade-x 3) | |
</run> | |
Aufgabe 2 | |
<code id="508e4c0c33d52"> | |
(define h1 | |
(lambda (x y) | |
(if (equal? (- (modulo x 2) y) 0) y | |
(h1 x (+ y 1))))) | |
(define h1-x | |
(lambda (x) | |
(h1 x 0))) | |
</code> | |
Wenn x gerade, dann 0, sonst 1. | |
<run id="508e4c0c34489"> | |
(h1-x 4) | |
</run> | |
<run id="508e4c0c3459e"> | |
(h1-x 5) | |
</run> | |
Aufgabe 3 | |
| − | <code id="508e4c0c33e2b"> |
| + | <code id="508fe47169df2"> |
| + | (define potenz |
| + | (lambda (zahl exponent) |
| + | (if (= exponent 0) |
| + | 1 |
| + | (* zahl (potenz zahl (- exponent 1)))))) |
| + | |
(define monus-minus | |
(lambda (x y) | |
(if (<= x y) 0 (- x y)))) | |
(define h2 | |
(lambda (x y) | |
| − | (if (equal? (monus-minus (pow x 2) y) 0) y |
| + | (if (equal? (monus-minus (potenz x 2) y) 0) y |
(h2 x (+ y 1))))) | |
(define h2-x | |
(lambda (x) | |
(h2 x 0))) | |
</code> | |
x-quadrat | |
<run id="508e4c0c346a5"> | |
(h2-x 3) | |
</run> | |
Aufgabe 4 | |
| − | <code id="508e4c0c33f02"> |
| + | <code id="508fe47169e6c"> |
| + | (define potenz |
| + | (lambda (zahl exponent) |
| + | (if (= exponent 0) |
| + | 1 |
| + | (* zahl (potenz zahl (- exponent 1)))))) |
| + | |
(define h3 | |
(lambda (x y) | |
| − | (if (equal? (monus-minus (pow y 2) x) 0) y |
| + | (if (equal? (monus-minus (potenz y 2) x) 0) y |
(h3 x (+ y 1))))) | |
(define h3-x | |
(lambda (x) | |
(h3 x 0))) | |
</code> | |
Liefert 0. | |
<run id="508e4c0c347ac"> | |
(h3-x 5) | |
</run> | |
Aufgabe 5 | |
<code id="508e4c0c33ff4"> | |
(define prim? | |
(lambda (n) | |
(letrec | |
((helfer | |
(lambda (t) | |
(cond | |
((> t (sqrt n)) 1) | |
((= (remainder n t) 0) 0) | |
(else | |
(helfer (+ t 2))))))) | |
(cond | |
((= n 1) 0) | |
((= n 2) 1) | |
((= (remainder n 2) 0) 0) | |
(else | |
(helfer 3)))))) | |
(define x-kleiner-y | |
(lambda (x y) | |
(if (< x y) 1 0))) | |
(define h4 | |
(lambda (x y k) | |
(if (equal? (- (* (x-kleiner-y x y) (prim? y)) k) 0) k | |
(h4 x y (+ k 1))))) | |
(define h4-x-y | |
(lambda (x y) | |
(h4 x y 0))) | |
</code> | |
Wenn y eine Primzahl größer als x, dann 1, sonst 0. | |
<run id="508e4c0c348d3"> | |
(h4-x-y 3 5) | |
</run> | |
<run id="508e4c0c349f6"> | |
(h4-x-y 6 5) | |
</run> | |
Aufgabe 6 | |
<code id="508e4c0c340c9"> | |
(define potenz | |
(lambda (zahl exponent) | |
(if (= exponent 0) | |
1 | |
(* zahl (potenz zahl (- exponent 1)))))) | |
(define betrag | |
(lambda (x) | |
(cond | |
[(< x 0) (* -1 x)] | |
[else x]))) | |
(define signum | |
(lambda (x) | |
(cond | |
[(> x 0) 1] | |
[(= x 0) 0] | |
[(< x 0) -1] | |
[else #f]))) | |
(define g | |
(lambda (m n k) | |
(cond | |
[(equal? (potenz (- (* k m) n) (signum m)) 0) k] | |
[else (g m n (+ k 1))]))) | |
(define g-m-n | |
(lambda (m n) | |
(g m n 0))) | |
</code> | |
Wenn m ungleich 0 UND m teilt n. | |
<run id="508e4c0c34afa"> | |
(g-m-n 4 8) | |
</run> | |
<run id="508e4c0c34be4"> | |
(g-m-n 4 6) | |
</run> | |
<run id="508e4ebeb28f4"> | |
(g-m-n 0 6) | |
</run> |