Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Functional Programming / hw1.rkt
blob: 056df23318803e5b5805aefff50fbc71bc239531 (plain) (blame)
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
#lang racket

;;; Задача 1

(define (sum-digits n)
  (if (= n 0)
    0
    (+ (sum-digits (quotient n 10)) (remainder n 10))))

(define (count-specials k a b)
  (if (or (<= k 0) (> a b))
    0
    (+
      (count-specials k (add1 a) b)
      (if (and (= (remainder a k) 0) (= (remainder (sum-digits a) k) 0))
        1
        0))))

;;; Задача 2

(define (swap-last2-digits n)
  (+ (* (quotient n 100) 100) (* (remainder n 10) 10) (quotient (remainder n 100) 10)))

(define (rotate-left n)
  (if (< n 9)
    n
    (swap-last2-digits (+ (* (rotate-left (quotient n 10)) 10) (remainder n 10)))))

(define (pow10 n)
  (if (= n 0)
    1
    (* 10 (pow10 (sub1 n)))))

(define (rotate-left-preserved n d)
  (rotate-left (remainder n (pow10 d))))