mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
22 lines
537 B
Ruby
22 lines
537 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require_relative './square_root'
|
||
|
require_relative './ceil'
|
||
|
|
||
|
# Calculates the aliquot sum of a number (the sum of all proper divisors of a number)
|
||
|
class AliquotSum
|
||
|
class << self
|
||
|
def call(number)
|
||
|
divisors(number).sum
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def divisors(number)
|
||
|
low_divisors = (1..Ceil.call(SquareRoot.call(number))).select { |num| (number % num).zero? }
|
||
|
high_divisors = low_divisors.map { |div| number / div }
|
||
|
(low_divisors + high_divisors).uniq - [number]
|
||
|
end
|
||
|
end
|
||
|
end
|