-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.rkt
executable file
·64 lines (57 loc) · 1.62 KB
/
functions.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#lang racket/base
(provide pi-approx)
(provide pi-approx-opt1)
(provide pi-approx-opt2)
(define (pi-approx number)
(define (dots-in-circle n)
(let y-loop ([y 0])
(if (= y n)
0
(+ (let x-loop ([x 0])
(if (= x n)
0
(+ (if (< (sqrt (+ (* x x) (* y y)))
n)
1
0)
(x-loop (+ x 1)))))
(y-loop (+ y 1))))))
(* 4
(/ (dots-in-circle number) ; Punkte im Kreis
(* number number)))) ;Punkte im Quadrat
(define (pi-approx-opt1 number)
(define (dots-in-circle n)
(let y-loop ([y 0])
(if (= y n)
0
(+ (let x-loop ([x 0])
(if (> (sqrt (+ (* x x) (* y y)))
n)
x
(x-loop (+ x 1))))
(y-loop (+ y 1))))))
(* 4
(/ (dots-in-circle number) ; Punkte im Kreis
(* number number)))) ;Punkte im Quadrat
; Zählt nur an der Kreislinie entlang statt über die ganze Fläche, dadurch O(n) statt O(n²).
(define (pi-approx-opt2 number)
(define (dots-in-circle n)
(define (x-loop x y)
(if (< (sqrt (+ (* x x) (* y y)))
n)
x
(x-loop (- x 1) y)))
(let y-loop ([y 0]
[start-x n]
[counter 0])
(if (= y n)
counter
(let ([found-x
(+ 1 (x-loop start-x y))])
(y-loop (+ y 1)
found-x
(+ counter
found-x))))))
(* 4
(/ (dots-in-circle number) ; Punkte im Kreis
(* number number)))) ;Punkte im Quadrat