How to resolve the algorithm Mertens function step by step in the CLU programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Mertens function step by step in the CLU programming language

Table of Contents

Problem Statement

The Mertens function M(x) is the count of square-free integers up to x that have an even number of prime factors, minus the count of those that have an odd number. It is an extension of the Möbius function. Given the Möbius function μ(n), the Mertens function M(x) is the sum of the Möbius numbers from n == 1 through n == x.

This is not code golf.   The stackexchange link is provided as an algorithm reference, not as a guide.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Mertens function step by step in the CLU programming language

Source code in the clu programming language

% Generate Mertens numbers up to a given limit
mertens = proc (limit: int) returns (array[int])
    M: array[int] := array[int]$fill(1,limit,0)
    M[1] := 1
    for n: int in int$from_to(2,limit) do
        M[n] := 1
        for k: int in int$from_to(2,n) do
            M[n] := M[n] - M[n/k]
        end
    end
    return (M)
end mertens

start_up = proc ()
    max = 1000

    po: stream := stream$primary_output()
    M: array[int] := mertens(max)

    stream$putl(po, "The first 99 Mertens numbers are:")
    for y: int in int$from_to_by(0,90,10) do
        for x: int in int$from_to(0,9) do
            stream$putright(po, int$unparse(M[x+y]), 3)
            except when bounds:
                stream$putright(po, "", 3)
            end
        end
        stream$putl(po, "")
    end

    eqz: int := 0
    crossz: int := 0
    for i: int in int$from_to(2,max) do
        if M[i]=0 then
            eqz := eqz + 1
            if M[i-1]~=0 then crossz := crossz + 1 end
        end
    end

    stream$putl(po, "M(N) is zero " || int$unparse(eqz) || " times.")
    stream$putl(po, "M(N) crosses zero " || int$unparse(crossz) || " times.")
end start_up

  

You may also check:How to resolve the algorithm Prime decomposition step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Modified random distribution step by step in the Ada programming language
You may also check:How to resolve the algorithm Modified random distribution step by step in the J programming language
You may also check:How to resolve the algorithm Spelling of ordinal numbers step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Sleep step by step in the Axe programming language