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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Chowla numbers step by step in the Lua 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 Lua programming language

Source code in the lua programming language

function chowla(n)
    local sum = 0
    local i = 2
    local j = 0
    while i * i <= n do
        if n % i == 0 then
            j = math.floor(n / i)
            sum = sum + i
            if i ~= j then
                sum = sum + j
            end
        end
        i = i + 1
    end
    return sum
end

function sieve(limit)
    -- True denotes composite, false denotes prime.
    -- Only interested in odd numbers >= 3
    local c = {}
    local i = 3
    while i * 3 < limit do
        if not c[i] and (chowla(i) == 0) then
            local j = 3 * i
            while j < limit do
                c[j] = true
                j = j + 2 * i
            end
        end
        i = i + 2
    end
    return c
end

function main()
    for i = 1, 37 do
        print(string.format("chowla(%d) = %d", i, chowla(i)))
    end
    local count = 1
    local limit = math.floor(1e7)
    local power = 100
    local c = sieve(limit)
    local i = 3
    while i < limit do
        if not c[i] then
            count = count + 1
        end
        if i == power - 1 then
            print(string.format("Count of primes up to %10d = %d", power, count))
            power = power * 10
        end
        i = i + 2
    end

    count = 0
    limit = 350000000
    local k = 2
    local kk = 3
    local p = 0
    i = 2
    while true do
        p = k * kk
        if p > limit then
            break
        end
        if chowla(p) == p - 1 then
            print(string.format("%10d is a number that is perfect", p))
            count = count + 1
        end
        k = kk + 1
        kk = kk + k
        i = i + 1
    end
    print(string.format("There are %d perfect numbers <= 35,000,000", count))
end

main()


  

You may also check:How to resolve the algorithm Flatten a list step by step in the Clojure programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Sleep step by step in the C++ programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Maxima programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the hexiscript programming language