How to resolve the algorithm Factors of an integer step by step in the CLU programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Factors of an integer step by step in the CLU 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 CLU programming language
Source code in the clu programming language
isqrt = proc (s: int) returns (int)
x0: int := s/2
if x0=0 then return(s) end
x1: int := (x0 + s/x0)/2
while x1
x0, x1 := x1, (x1 + s/x1)/2
end
return(x0)
end isqrt
factors = iter (n: int) yields (int)
yield(1)
for i: int in int$from_to(2,isqrt(n)) do
if n//i=0 then
yield(i)
if i*i ~= n then yield(n/i) end
end
end
yield(n)
end factors
start_up = proc ()
po: stream := stream$primary_output()
a: array[int] := array[int]$[3135, 45, 64, 53, 45, 81]
for n: int in array[int]$elements(a) do
stream$puts(po, "Factors of " || int$unparse(n) || ":")
for f: int in factors(n) do
stream$puts(po, " " || int$unparse(f))
end
stream$putl(po, "")
end
end start_up
You may also check:How to resolve the algorithm Sum to 100 step by step in the 11l programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the R programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the C# programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the Go programming language