123456789101112131415161718192021222324252627282930313233 |
- #!/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.393s
- func proper_sigma(n) {
- n.sigma - n
- }
- var total = 0
- for a in (2 ..^ 10000) {
- var b = proper_sigma(a)
- next if (b <= a)
- var c = proper_sigma(b)
- if (c == a) {
- say "#{a} #{b}"
- total += a+b
- }
- }
- say total
|