How to resolve the algorithm Factors of an integer step by step in the Red programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Factors of an integer step by step in the Red programming language

Table of Contents

Problem Statement

Compute the   factors   of a positive integer. These factors are the positive integers by which the number being factored can be divided to yield a positive integer result. (Though the concepts function correctly for zero and negative integers, the set of factors of zero has countably infinite members, and the factors of negative integers can be obtained from the factors of related positive numbers without difficulty;   this task does not require handling of either of these cases). Note that every prime number has two factors:   1   and itself.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Factors of an integer step by step in the Red programming language

Source code in the red programming language

Red []

factors: function [n [integer!]] [
    n: absolute n
    collect [
        repeat i (sq: sqrt n) - 1 [
            if n % i = 0 [
                keep i
                keep n / i
            ]
        ]
        if sq = sq: to-integer sq [keep sq]
    ]
]

foreach num [
    24
   -64        ; negative
    64        ; square
    101       ; prime
    123456789 ; large
][
    print mold/flat sort factors num
]

  

You may also check:How to resolve the algorithm Babylonian spiral step by step in the Python programming language
You may also check:How to resolve the algorithm Repunit primes step by step in the Sidef programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the TXR programming language
You may also check:How to resolve the algorithm Currying step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Mathematica/Wolfram Language programming language