Rosetta Code

Factors of an integer

Compute all positive factors of a small positive integer with trial division up to its square root.

Intro View source
Source rosettacode/popular/factors_of_an_integer.vibe
# title: Factors of an integer
# source: https://rosettacode.org/wiki/Factors_of_an_integer
# category: Rosetta Code
# difficulty: Intro
# summary: Compute all positive factors of a small positive integer with trial division up to its square root.
# tags: popular, math, factors, loops
# vibe: 0.2

def factors_of(number)
  lower = []
  upper = []
  candidate = 1

  while candidate * candidate <= number
    if number % candidate == 0
      lower = lower.push(candidate)
      other = number / candidate
      if other != candidate
        upper = [other] + upper
      end
    end
    candidate = candidate + 1
  end

  lower + upper
end

def run
  {
    forty_five: factors_of(45),
    fifty_three: factors_of(53),
    sixty_four: factors_of(64)
  }
end
Output
Press run to execute run from this example.
rosetta-code popular math factors loops browser-runner