Rosetta Code

Aliquot sequence classifications

Build short aliquot sequences and classify whether they terminate, loop, or are perfect.

Medium View source
Source rosettacode/popular/aliquot_sequence_classifications.vibe
# title: Aliquot sequence classifications
# source: https://rosettacode.org/wiki/Aliquot_sequence_classifications
# category: Rosetta Code
# difficulty: Medium
# summary: Build short aliquot sequences and classify whether they terminate, loop, or are perfect.
# tags: popular, math, sequences, divisors
# vibe: 0.4

def proper_divisor_sum(number)
  if number == 1
    return 0
  end

  total = 1
  factor = 2

  while factor * factor <= number
    if number % factor == 0
      total = total + factor
      other = number / factor
      if other != factor
        total = total + other
      end
    end
    factor = factor + 1
  end

  total
end

def aliquot_sequence(start_value, limit)
  values = []
  seen = {}
  current = start_value

  while values.length < limit && !seen.fetch(current.to_s, false)
    values << current
    seen[current.to_s] = true
    current = proper_divisor_sum(current)
  end

  values << current
  values
end

def classification(sequence)
  if sequence.last == 0
    "terminating"
  elsif sequence.last == sequence.first
    "perfect"
  else
    "looping"
  end
end

def run
  [6, 10, 12].map do |start|
    sequence = aliquot_sequence(start, 8)
    {
      start:,
      classification: classification(sequence),
      sequence:
    }
  end
end
Output
Press run to execute run from this example.