Skip to content

Instantly share code, notes, and snippets.

Exercise 1.11
(define (recursive-f n)
(if (< n 3)
n
(+ (recursive-f (- n 1))
(* 2 (recursive-f (- n 2)))
(* 3 (recursive-f (- n 3))))))
Exercise 1.10
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
Exercise 1.9
(define (+ a b)
(if (= a 0)
b
(inc (+ (dec a) b))))
;(+ 4 5)
;(inc (+ 3 5))
;(inc (inc (+ 2 5)))
Exercise 1.2
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* (* 3 (- 6 2)) (- 2 7)))
Exercise 1.3
(define (sum-of-high-squares a b c)
(+ (if (> a b) a b) (if (> b c) b c)))
months_table =
{
1 => 0,
2 => 3,
3 => 3,
4 => 6,
5 => 1,
6 => 4,
7 => 6,
8 => 2,
triangle = [
%w!75!,
%w!95 64!,
%w!17 47 82!,
%w!18 35 87 10!,
%w!20 04 82 47 65!,
%w!19 01 23 75 03 34!,
%w!88 02 77 73 07 63 67!,
%w!99 65 04 28 06 16 70 92!,
%w!41 41 26 56 83 40 80 70 33!,
def number_under_1000_to_s(n)
if n == 1000
'onethousand'
else
number_from_999_to_100_to_s(n)
end
end
def number_from_999_to_100_to_s(n)
if n > 99
number = 2**1000
sum = 0
number.to_s.each_char { |n| sum += n.to_i }
@table = {1 => 1}
result = [0,0]
def next_number(n)
if n.even?
n / 2
else
3 * n + 1
end
class Integer
# overwritten, I don't need modulus
def sum_progression
self * (self + 1) / 2
end
def integral_divisors_size
factors = self.factors
result = 1