-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp23.rb
66 lines (54 loc) · 968 Bytes
/
p23.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
# Problem #23 for Project Euler
# Author: Tyler Ewing
# Date: 7/15/2012
class Integer
def factors
a = Array.new
a.push(1)
(2..Math.sqrt(self)).each do |i|
if self % i == 0
a.push(i)
a.push(self/i)
end
end
return a
end
# def sum_of_factors1
# t1 = Time.now
# total = 1
# limit = Math.sqrt(self)
# (2..limit).each do |n|
# if self%n == 0
# total += n + (self/n)
# total -= n if n == (self/n)
# end
# end
# t2 = Time.now
# puts "#{(t2-t1)*1000.0}"
# total
# end
# This is faster
def sum_of_factors
# t1 = Time.now
total = 1
low, high = 2, self/2
while low <= high do
if self%low == 0
total += low + high
total -= low if low == high
end
low +=1
high = self/low
end
# t2 = Time.now
# puts "#{(t2-t1)*1000.0}"
total
end
def abundant?
self.sum_of_factors > self
end
end
ar = Array.new
(12..28123).each{ |n| ar << n if n.abundant? }
# puts ar.size
# puts ar[-1]