Skip to content

Instantly share code, notes, and snippets.

GRID = [
%w!08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08!,
%w!49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00!,
%w!81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65!,
%w!52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91!,
%w!22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80!,
%w!24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50!,
%w!32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70!,
%w!67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21!,
%w!24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72!,
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
@table = {1 => 1}
result = [0,0]
def next_number(n)
if n.even?
n / 2
else
3 * n + 1
end
number = 2**1000
sum = 0
number.to_s.each_char { |n| sum += n.to_i }
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
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!,
months_table =
{
1 => 0,
2 => 3,
3 => 3,
4 => 6,
5 => 1,
6 => 4,
7 => 6,
8 => 2,
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)))
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.10
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))