-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem21.rb
63 lines (51 loc) · 1.19 KB
/
Problem21.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
beginning_time = Time.now
## this attempted function doesn't work in loops, says undefined method map
# require 'prime'
# def divisors_of(number)
# primes, powers = number.prime_division.transpose
# exponents = powers.map{|i| (0..i).to_a}
# divisors = exponents.shift.product(*exponents).map do |powers|
# primes.zip(powers).map{|prime,power| prime ** power}.inject(:*)
# end
# divisors.sort.map{|div| div}-[number]
# end
# # This was too slow too
# class Integer
# def factors()
# (1..self/2).select{|n|(self%n).zero?}
# end
# end
class Integer
def factors
d = []
2.upto(Math.sqrt(self)) { |i|
if self % i == 0
d << i << (self / i)
end
}
d << 1 #this is not uniq
end
end
# p 284.factors.reduce(:+)
dA = 0
dB = 0
sumAmicable = 0
1.upto(10000) do |a|
dA = a.factors.reduce(:+)
if dA > a
a+1.upto(10000) do |b|
dB = b.factors.reduce(:+)
if dB == a
if dA ==b
if a!=b
sumAmicable+=dA+dB
puts a.to_s + " is amicable with " + b.to_s
end
end
end
end
end
end
puts sumAmicable
end_time = Time.now
puts "Time elapsed #{(end_time - beginning_time)} seconds"