How to resolve the algorithm Abundant odd numbers step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abundant odd numbers step by step in the CLU programming language

Table of Contents

Problem Statement

An Abundant number is a number n for which the   sum of divisors   σ(n) > 2n, or,   equivalently,   the   sum of proper divisors   (or aliquot sum)       s(n) > n.

12   is abundant, it has the proper divisors     1,2,3,4 & 6     which sum to   16   ( > 12 or n);        or alternately,   has the sigma sum of   1,2,3,4,6 & 12   which sum to   28   ( > 24 or 2n).

Abundant numbers are common, though even abundant numbers seem to be much more common than odd abundant numbers. To make things more interesting, this task is specifically about finding   odd abundant numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant odd numbers step by step in the CLU programming language

Source code in the clu programming language

% Integer square root
isqrt = proc (s: int) returns (int)
    x0: int := s / 2
    if x0 = 0 then 
        return(s)
    else
        x1: int := (x0 + s/x0) / 2
        while x1 < x0 do
            x0 := x1
            x1 := (x0 + s/x0) / 2
        end
        return(x0)
    end
end isqrt 

% Calculate aliquot sum (for odd numbers only)
aliquot = proc (n: int) returns (int)
    sum: int := 1
    for i: int in int$from_to_by(3, isqrt(n)+1, 2) do
        if n//i = 0 then
            j: int := n / i
            sum := sum + i
            if i ~= j then
                sum := sum + j
            end
        end
    end
    return(sum)
end aliquot

% Generate abundant odd numbers
abundant_odd = iter (n: int) yields (int)
    while true do
        if n < aliquot(n) then yield(n) end
        n := n + 2
    end
end abundant_odd

start_up = proc ()
    po: stream := stream$primary_output()
    
    count: int := 0
    for n: int in abundant_odd(1) do
        count := count + 1
        if count <= 25 cor count = 1000 then
            stream$putl(po, int$unparse(count) 
                        || ":\t" 
                        || int$unparse(n)
                        || "\taliquot: "
                        || int$unparse(aliquot(n)))
            if count = 1000 then break end
        end
    end
    
    for n: int in abundant_odd(1000000001) do
        stream$putl(po, "First above 1 billion: " 
                        || int$unparse(n)
                        || " aliquot: "
                        || int$unparse(aliquot(n)))
        break
    end
end start_up

  

You may also check:How to resolve the algorithm Generator/Exponential step by step in the OCaml programming language
You may also check:How to resolve the algorithm Empty program step by step in the 11l programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the VBA programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the R programming language
You may also check:How to resolve the algorithm URL encoding step by step in the V (Vlang) programming language