-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12_triangular.rb
More file actions
107 lines (97 loc) · 2.11 KB
/
Copy path12_triangular.rb
File metadata and controls
107 lines (97 loc) · 2.11 KB
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
def sum(a,b)
return ((a+b)/2.0) * ((b-a)+1)
end
def factors(n)
# beginning_time = Time.now
factors = []
f = 1
until f > n
factors << f if n % f == 0
f += 1
end
# puts 'factors'
# puts factors
# end_time = Time.now
# puts "Time elapsed #{(end_time - beginning_time)} seconds"
return factors.length
end
def factors2(n)
# beginning_time = Time.now
factors = 0
f = 1
until f > n
factors += 1 if n % f == 0
f += 1
end
# puts 'factors'
# puts factors
# end_time = Time.now
# puts "Time elapsed #{(end_time - beginning_time)} seconds"
return factors
end
#for tri_num(100)
# Time elapsed 2.234939 seconds without checking for primes
# 2.22685 with checking for primes
# 2.215695 with max += depending on how much more to go
def triangular_num(divisors)
beginning_time = Time.now
max = 1
f_length = 1
until f_length > divisors
# if (divisors - f_length) > 20
# max += 10
# else
max += 1
# end
# max += 1
# puts 'max' + max.to_s
tri = sum(1,max)
# puts 'tri' + tri.to_s
if is_prime?(tri)
f_length = 2
else
f_length = factors2(tri)
end
# puts 'f_length' + f_length.to_s
end
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)} seconds"
puts max
puts f_length
return tri
end
def is_prime?(n)
# beginning_time = Time.now
return false if n == 1
return true if n == 2
f = 2
until f == Math.sqrt(n).ceil + 1
if (n % f == 0)
end_time = Time.now
# puts "Time elapsed #{(end_time - beginning_time)} seconds"
return false
end
f += 1
end
# end_time = Time.now
# puts "Time elapsed #{(end_time - beginning_time)} seconds"
return true
end
def triangular_num2(divisors)
beginning_time = Time.now
max = 1
f_length = 1
until f_length > divisors
max += 1
# puts 'max' + max.to_s
tri = sum(1,max)
# puts 'tri' + tri.to_s
f_length = factors(tri)
# puts 'f_length' + f_length.to_s
end
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)} seconds"
puts max
puts f_length
return tri
end