12345678910111213141516171819202122232425262728293031323334353637 |
- #!/usr/bin/ruby
- # Daniel "Trizen" Șuteu
- # https://github.com/trizen
- # For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284.
- # The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
- # Evaluate the sum of all the amicable numbers under 10000.
- # https://projecteuler.net/problem=21
- # Runtime: 0.417s
- var sum = 0
- var mem = Hash()
- for i in (2 ..^ 10000) {
- var d = i.sigma-i
- if (mem.exists(d)) {
- var n = mem{d}
- if (n.sigma-n == d) {
- sum += d+n
- say "#{d} #{n}"
- }
- mem.delete(d)
- }
- elsif (i != d) {
- mem{i} = d
- }
- }
- say "Sum: #{sum}"
|