From b66834ae2b83769ab7fb453b9a877c6da7a13a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jurij=20Podgor=C5=A1ek?= Date: Wed, 12 Jun 2024 19:33:26 +0200 Subject: [PATCH] Nekaj vaj, TODO --- zapiski/g1smo/1_3_zapiski_vaje.org | 143 +++++++++++++++++++++++++++-- 1 file changed, 136 insertions(+), 7 deletions(-) diff --git a/zapiski/g1smo/1_3_zapiski_vaje.org b/zapiski/g1smo/1_3_zapiski_vaje.org index 119e55d..fda463a 100644 --- a/zapiski/g1smo/1_3_zapiski_vaje.org +++ b/zapiski/g1smo/1_3_zapiski_vaje.org @@ -101,23 +101,152 @@ Iterativne procedure: (if (> a b) result (iter (next a) (* result (term a))))) - (iter a 0)) + (iter a 1)) (define (piblizek n) - (define (stevec n) - (+ n 2)) - (* 4 (/ (prod-iter (lambda (n) (* (- n 1) (+ n 1))) + (define (stevec m) + (+ m 2)) + (* 4 (/ (prod-iter (lambda (a) (* (- a 1) (+ a 1))) 3 stevec n) - (prod-iter (lambda (n) (* n n)) + (prod-iter (lambda (b) (* b b)) 3 stevec n)))) - (piblizek 10) + (+ (piblizek 10000) 0.0) +#+end_src + +#+RESULTS: +: 3.1417497371492673 + +** 1.32 Akumulator (reduce funkcija) + +#+begin_src scheme :exports both + (define (accumulate combiner null-value term a next b) + (if (> a b) + null-value + (combiner (term a) (accumulate combiner null-value term (next a) next b)))) + + (define (sum term a next b) + (accumulate + 0 term a next b)) + + (define (cube n) (* n n n)) + + (define (sum-cubes a b) (sum cube a (lambda (n) (+ n 1)) b)) + + (define (prod term a next b) + (accumulate * 1 term a next b)) + + (define (piblizek n) + (define (stevec m) + (+ m 2)) + (* 4 (/ (prod (lambda (a) (* (- a 1) (+ a 1))) + 3 + stevec + n) + (prod (lambda (b) (* b b)) + 3 + stevec + n)))) + + (list (sum-cubes 1 10) (+ (piblizek 10000) 0.0)) +#+end_src + +#+RESULTS: +| 3025 | 3.1417497371492673 | + +** 1.32b Akumulator iterativno (reduce funkcija) + +#+begin_src scheme :exports both + (define (accumulate-iter combiner null-value term a next b) + (define (combine a combination) + (if (> a b) + combination + (combine (next a) (combiner combination (term a))))) + (combine a null-value)) + + (define (sum term a next b) + (accumulate-iter + 0 term a next b)) + + (define (cube n) (* n n n)) + + (define (sum-cubes a b) (sum cube a (lambda (n) (+ n 1)) b)) + + (define (prod term a next b) + (accumulate-iter * 1 term a next b)) + + (define (piblizek n) + (define (stevec m) + (+ m 2)) + (* 4 (/ (prod (lambda (a) (* (- a 1) (+ a 1))) + 3 + stevec + n) + (prod (lambda (b) (* b b)) + 3 + stevec + n)))) + + (list (sum-cubes 1 10) (+ (piblizek 10000) 0.0)) +#+end_src + +#+RESULTS: +| 3025 | 3.1417497371492673 | + +** 1.33 filtriran akumulator + +*** a) vsota kvadratov praštevil med a in b +#+begin_src scheme + (define (accumulate-filter combiner null-value term a next b predikat?) + (if (> a b) + null-value + (if (predikat? a) + (combiner (term a) (accumulate-filter combiner null-value term (next a) next b predikat?)) + (accumulate-filter combiner null-value term (next a) next b predikat?)))) + + (define (je-prastevilo? a) + (define (preveri-prastevilo? x a) + (if (> x (/ a 2)) + #t + (and (not (integer? (/ a x))) + (preveri-prastevilo? (+ x 1) a)))) + (preveri-prastevilo? 2 a)) + + ;; (vsota kvadratov praštevil med 1 in 12 je 209) + (= (accumulate-filter + 0 (lambda (n) (* n n)) 1 (lambda (n) (+ n 1)) 12 je-prastevilo?) + 209) +#+end_src + +#+RESULTS: +: #t + +*** a) produkt vseh naravnih števil manjših od n ki so "relativno pra-n" (torej si z n ne delijo skupnega delitelja razen n) + +#+begin_src scheme + (define (accumulate-filter combiner null-value term a next b predikat?) + (if (> a b) + null-value + (if (predikat? a) + (combiner (term a) (accumulate-filter combiner null-value term (next a) next b predikat?)) + (accumulate-filter combiner null-value term (next a) next b predikat?)))) + + (define (id a) a) + (define (inc a) (+ a 1)) + (define (IN a b) (and a b)) + + (define (relativni-prastevili? a b) + (accumulate-filter IN #t (lambda (x) (and (not (integer? (/ a x))) + (not (integer? (/ b x))))) + 2 inc a id)) + + (define (produkt-relativnih-prastevil n) + (accumulate-filter * 1 id 1 inc n (lambda (x) (relativni-prastevili? x n)))) + + (produkt-relativnih-prastevil 9) #+end_src #+RESULTS: -TODO popravi! +TODO popravi!! ^