diff options
[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.rkt | 68 |
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)) |
