Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Functional Programming
diff options
context:
space:
mode:
Commit9c46a128446ef88549fe79ab6f586284930fc966 (patch)
Author
 Syndamia <kamen@syndamia.com>
2022-10-20 11:26:57 +0300
Committer
 Syndamia <kamen@syndamia.com>
2022-10-20 11:26:57 +0300
Filesf5ed04508a4e9c6c5894934d475e2d0b4e604e13 /Year 2/Functional Programming
uni-projects-9c46a128446ef88549fe79ab6f586284930fc966.tar
uni-projects-9c46a128446ef88549fe79ab6f586284930fc966.tar.gz
uni-projects-9c46a128446ef88549fe79ab6f586284930fc966.zip
Parent9733f1336ee8054767a2516744cbd187cfa3b851 (diff)
[FP/P03] Added work from today's practicum
1 files changed, 68 insertions, 0 deletions
-rw-r--r--Year 2/Functional Programming/03-Practice-20.10.2022.rkt68
diff --git a/Year 2/Functional Programming/03-Practice-20.10.2022.rkt b/Year 2/Functional Programming/03-Practice-20.10.2022.rkt
new file mode 100644
index 0000000..7e00afb
--- /dev/null
+++ b/Year 2/Functional Programming/03-Practice-20.10.2022.rkt
@@ -0,0 +1,68 @@
+#lang racket
+
+;;; Задача 1
+
+(define (smallest-prime n)
+ (cond [(= (remainder n 2) 0) 2]
+ [(= (remainder n 3) 0) 3]
+ [else (define (find-prime-iter n i maxd)
+ (cond [(> (sub1 i) maxd) n]
+ [(= (remainder n (sub1 i)) 0) (sub1 i)]
+ [(= (remainder n (add1 i)) 0) (add1 i)]
+ [else (find-prime-iter n (+ i 6) maxd)]))
+ (find-prime-iter n 6 (sqrt n))]))
+
+(define (sum-prime-divisors n)
+ (define smallest-prime-of-n (smallest-prime n))
+ (if (= n 1)
+ 0
+ (+ smallest-prime-of-n (sum-prime-divisors (quotient n smallest-prime-of-n)))))
+
+;;; Задача 2
+
+(define (pow x n)
+ (if (= n 0)
+ 1
+ (* x (pow x (sub1 n)))))
+
+;;; Задача 3
+
+(define (count-occurences d n)
+ (cond [(= n 0) 0]
+ [(= (remainder n 10) d)
+ (add1 (count-occurences d (quotient n 10)))]
+ [else (count-occurences d (quotient n 10))]))
+
+;;; Задача 4
+
+(define (ascending? n)
+ (cond [(= n 0) #t]
+ [(< (remainder n 10) (remainder (quotient n 10) 10)) #f]
+ [else (ascending? (quotient n 10))]))
+
+;;; Задача 5
+
+(define (smallest-divisor n bigger-than)
+ (define (calc-iter i)
+ (if (= (remainder n i) 0)
+ i
+ (calc-iter (add1 i))))
+ (calc-iter (add1 bigger-than)))
+
+(define (perfect-number? n)
+ (define (calc-iter current-divisor current-sum)
+ (cond [(= current-divisor n) #f]
+ [(= current-sum n) #t]
+ [else (define divisor (smallest-divisor n current-divisor))
+ (calc-iter divisor (+ current-sum divisor))]))
+ (calc-iter 1 1))
+
+;;; Задача 6
+
+(define (calc-sum x n)
+ (define (calc-iter i prev-powd sum)
+ (define curr-powd (* prev-powd x))
+ (if (= i n)
+ sum
+ (calc-iter (add1 i) curr-powd (+ sum curr-powd))))
+ (calc-iter 0 1 1))