-
Notifications
You must be signed in to change notification settings - Fork 0
/
equations.rb
86 lines (74 loc) · 1.33 KB
/
equations.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
def isPrime(n)
if n<=1; return false end
if n<4; return true end
if n%2==0; return false end
if n<9; return true end
if n%3==0; return false end
r=Math.sqrt(n).floor
f=5
while f<=r
if n%f==0; return false end
if n%(f+2)==0; return false end
f=f+6
end
return true
end
def genPrimes(limit)
crosslimit = Math.sqrt(limit).floor
sieve = Array.new(limit+1) { |i| false }
(4..limit).step(2) { |i|
sieve[i]=true
}
(3..crosslimit).step(2) { |i|
if !sieve[i]
(i*i..limit).step(2*i) { |j|
sieve[j] = true
}
end
}
sieve[0..1] = [true,true]
primes=[]
sieve.each_with_index { |i,idx|
if !i ; primes.push idx end
}
return primes
end
def isTriangular?(n)
math = (Math.sqrt(8*n + 1) + 1)/2
math%1==0
end
def isPentagonal?(n)
math = (Math.sqrt(24*n + 1) + 1)/6
math%1==0
end
def isHexagonal?(n)
math = (Math.sqrt(8*n + 1) + 1)/4
math%1==0
end
def isHeptagonal?(n)
math = (Math.sqrt(40*n + 9) + 3)/10
math%1==0
end
def isOctagonal?(n)
math = (Math.sqrt(12*n + 4) + 2)/6
math%1==0
end
def getTriangular(n)
n*(n+1)/2
end
def getPentagonal(n)
n*(3*n-1)/2
end
def getHexagonal(n)
n*(2*n-1)
end
def getHeptagonal(n)
n*(5*n-3)/2
end
def getOctagonal(n)
n*(3*n-2)
end
def factorial(n)
if n==0 ; return 1 end
(1..n).inject(:*)
end