Nekaj vaj, TODO
parent
b21ded727b
commit
b66834ae2b
|
@ -101,23 +101,152 @@ Iterativne procedure:
|
||||||
(if (> a b)
|
(if (> a b)
|
||||||
result
|
result
|
||||||
(iter (next a) (* result (term a)))))
|
(iter (next a) (* result (term a)))))
|
||||||
(iter a 0))
|
(iter a 1))
|
||||||
|
|
||||||
(define (piblizek n)
|
(define (piblizek n)
|
||||||
(define (stevec n)
|
(define (stevec m)
|
||||||
(+ n 2))
|
(+ m 2))
|
||||||
(* 4 (/ (prod-iter (lambda (n) (* (- n 1) (+ n 1)))
|
(* 4 (/ (prod-iter (lambda (a) (* (- a 1) (+ a 1)))
|
||||||
3
|
3
|
||||||
stevec
|
stevec
|
||||||
n)
|
n)
|
||||||
(prod-iter (lambda (n) (* n n))
|
(prod-iter (lambda (b) (* b b))
|
||||||
3
|
3
|
||||||
stevec
|
stevec
|
||||||
n))))
|
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
|
#+end_src
|
||||||
|
|
||||||
#+RESULTS:
|
#+RESULTS:
|
||||||
|
|
||||||
TODO popravi!
|
TODO popravi!! ^
|
||||||
|
|
Loading…
Reference in New Issue