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

Published on 12 May 2024 09:40 PM
#J

How to resolve the algorithm Mertens function step by step in the J 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 J programming language

Source code in the j programming language

mu    =: 0:`(1 - 2 * 2|#@{.)@.(1: = */@{:)@(2&p:)"0
M     =: +/@([: mu 1:+i.)

m1000 =: (M"0) 1+i.1000
zero  =: +/ m1000 = 0
cross =: +/ (-.*.1:|]) m1000 ~: 0 

echo 'The first 99 Merten numbers are'
echo 10 10$ __, 99{.m1000
echo 'M(N) is zero ',(":zero),' times.'
echo 'M(N) crosses zero ',(":cross),' times.'
exit''


  

You may also check:How to resolve the algorithm Catmull–Clark subdivision surface step by step in the J programming language
You may also check:How to resolve the algorithm Pointers and references step by step in the Sidef programming language
You may also check:How to resolve the algorithm Sierpinski triangle/Graphical step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Variable-length quantity step by step in the C programming language
You may also check:How to resolve the algorithm Wieferich primes step by step in the Forth programming language