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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

Chowla numbers are also known as:

The chowla number of   n   is   (as defined by Chowla's function):

The sequence is named after   Sarvadaman D. S. Chowla,   (22 October 1907 ──► 10 December 1995), a London born Indian American mathematician specializing in number theory.

German mathematician Carl Friedrich Gauss (1777─1855) said:

Chowla numbers can also be expressed as:

Let's start with the solution:

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

Source code in the clu programming language

% Chowla's function
chowla = proc (n: int) returns (int)
    sum: int := 0
    i: int := 2
    while i*i <= n do
        if n//i = 0 then
            sum := sum + i
            j: int := n/i
            if i ~= j then
                sum := sum + j
            end
        end
        i := i + 1
    end
    return(sum)
end chowla

% A number is prime iff chowla(n) is 0
prime = proc (n: int) returns (bool)
    return(chowla(n) = 0)
end prime

% A number is perfect iff chowla(n) equals n-1
perfect = proc (n: int) returns (bool)
    return(chowla(n) = n-1)
end perfect

start_up = proc ()
    LIMIT = 35000000
    po: stream := stream$primary_output()
    
    % Show chowla(1) through chowla(37)
    for i: int in int$from_to(1, 37) do
        stream$putl(po, "chowla(" || int$unparse(i) || ") = "
                        || int$unparse(chowla(i)))
    end
    
    % Count primes up to powers of 10
    pow10: int := 2        % start with 100
    primecount: int := 1   % assume 2 is prime, then test only odd numbers
    candidate: int := 3
    while pow10 <= 7 do
        if candidate >= 10**pow10 then
            stream$putl(po, "There are " 
                        ||  int$unparse(primecount)
                        ||  " primes up to "
                        ||  int$unparse(10**pow10))
            pow10 := pow10 + 1
        end
        if prime(candidate) then primecount := primecount + 1 end
        candidate := candidate + 2
    end
    
    % Find perfect numbers up to 35 million
    perfcount: int := 0
    k: int := 2
    kk: int := 3
    while true do
        n: int := k * kk
        if n >= LIMIT then break end
        if perfect(n) then
            perfcount := perfcount + 1
            stream$putl(po, int$unparse(n) || " is a perfect number.")
        end
        k := kk + 1
        kk := kk + k
    end
    stream$putl(po, "There are " || int$unparse(perfcount) ||
                    " perfect numbers < 35,000,000.")
end start_up

  

You may also check:How to resolve the algorithm Function definition step by step in the Ruby programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Brat programming language
You may also check:How to resolve the algorithm K-means++ clustering step by step in the Fortran programming language
You may also check:How to resolve the algorithm O'Halloran numbers step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Oforth programming language