2024-03-11 10:34:40 +01:00
|
|
|
# Lösung: Was hoppelt den da?
|
2024-03-11 10:32:49 +01:00
|
|
|
|
2024-03-11 10:39:04 +01:00
|
|
|
|
|
|
|
```$2#!/usr/bin/env -S clojure -M
|
|
|
|
|
|
|
|
(def generationen 14)
|
|
|
|
|
|
|
|
(defn fib
|
|
|
|
"Berechnet die Fibonacci-Zahl nach `n` Schritten."
|
|
|
|
[n]
|
|
|
|
{ :pre [(>= n 0)]
|
|
|
|
:post [(>= % 0)] }
|
|
|
|
(loop [n_2 0 n_1 1 step n]
|
|
|
|
(cond
|
|
|
|
(= step 0)
|
|
|
|
n_2
|
|
|
|
:else
|
|
|
|
(recur n_1 (+ n_2 n_1) (dec step)))))
|
|
|
|
|
|
|
|
(defn fibs
|
|
|
|
"Berechnet alle Fibonacci-Zahlen bis `n` einschießlich
|
|
|
|
und gibt sie als Vektor zurück."
|
|
|
|
[n]
|
|
|
|
{ :pre [(>= n 0)] }
|
|
|
|
|
|
|
|
(let [v (range (inc n))]
|
|
|
|
(map #(fib %) v)))
|
|
|
|
|
|
|
|
(defn kaninchen
|
|
|
|
[]
|
|
|
|
(println "Nach" generationen "Generationen haben wir" (fib generationen) "Kaninchen")
|
|
|
|
(println "Die Population entwickelt sich so:" (fibs generationen)))
|
|
|
|
|
|
|
|
(kaninchen)
|
|
|
|
|
|
|
|
```
|