-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_fun.rb
56 lines (55 loc) · 891 Bytes
/
math_fun.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
class Fixnum
def is_prime
x = 2
while x <= Math.sqrt(self)
if self % x == 0
return false
end
x += 1
end
true
end
def get_factors
factors = []
x = 1
count = self
while x < count
if count % x == 0
factors << x << self/x
count = self/x
end
x += 1
end
factors.sort!
end
def self.produce_pythagorean_triplet?(num1, num2)
triplet = Math.sqrt(num1**2 + num2**2)
if triplet % 1 == 0
return true
end
false
end
def self.triangle_number(num)
tri_num = 0
while num > 0
tri_num += num
num -=1
end
tri_num
end
end
class Bignum
def get_factors
factors = []
x = 1
count = self
while x < count
if count % x == 0
factors << x << self/x
count = self/x
end
x += 1
end
factors.sort!
end
end