-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd_lcm.rb
115 lines (98 loc) · 2.88 KB
/
gcd_lcm.rb
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#GCD LCM
#using Euclid's algo
#oddly enough the order doesn't seem to matter
#worst runtime is 5 * number of digits of the smaller integer
def gcd(a, b)
return a if b == 0
gcd(b, a % b)
end
def lcm(a,b)
return b * a / gcd(a,b)
end
#using the prime representation of numbers finding the lcm should be trivial
#consider writing a function to do this
def fiddle_with_Euclid_for_n(n)
n.times do |i|
puts "i = #{i}, n = #{n}, gcd(n, i) = #{gcd(n, i)}"
puts "i = #{i}, n = #{n}, lcm(n, i) = #{lcm(n, i)}"
end
end
#consider adding a visual squares and rectangles sort of notation
#consider making something to convert a continued_fraction of the modern array notation into a/b (?) (clearly wouldn't work for all reals)
#so why bother?
#note that this doesn't work for all reals, just rationals
#TODO : make this stuff work for all reals
#TODO : make this stuff work for complex numbers as well
#quadratic irrationals are things like this a + (b)^(1/2) where b is not a perfect sqaure
#to see if n is irrational take it's square root, pull out a , if b is not perfect^2 then n is irrational
#basically, the square root of any natural number n where n is not a perfect^2 is irrational
#is the square_root of n a quadratic irration?
#(5) => true
def quadratic_irrational?( n )
#TODO : COMPLETE THIS AT SOME POINT
#TODO : REMEMBER WHY I WAS WRITING THIS IN THE FIRST PLACE...
require './perfect_square.rb'
return perfect_square( n )
end
#####################################################
# => Stuff with CONTINUED FRACTIONS #
#####################################################
def continued_fraction(a, b)
integers = []
#algo, take out the integer, take the reciprocal, repeat until no remainder or a == 1 (hmm...)
#save the integrers
#loop invariant??
while (b != 0) #[a/b] = a/b (integer) + b / a % b
integers << a / b
temp = a
a = b
b = temp % b
end
puts integers
return integers
end
#modern array notation #59/2 = [5;1,3,2]
def notation_1(array)
print '['
print "#{array[0]};"
(1...array.size - 1).each do |i|
print "#{array[i]},"
end
print "#{array[array.size-1]}]"
puts ""
end
#exponential notation #59/2 = 5 + (1 + (3 + 2)^-1)^-1)^-1
def notation_2(array)
print "#{array[0]} + "
(1...array.size - 1).each do |i|
print "(#{array[i]} + "
end
print "#{array[array.size - 1]}"
(1...array.size).each do |i|
print ')^-1'
end
puts ""
end
#weird fractional notation
#52 / 9 =
#5 + 1
# ----
# 1 + 1
# ----
# 3 + 1
# ----
# 2
#TODO : fix the formatting in this so that 1/1 and 1/3 and 1/2 line up into columns
def notation_3(array)
puts "#{array.first} + 1"
puts " ----"
(1...array.size - 1).each do |i|
print " " * i * 3
puts "#{array[i]} + 1"
s = " " * (i * 3 + 3)
s << "----"
puts s
end
print " " * array.size
puts "#{array.last}"
end