Mertens function: Difference between revisions

m
(Add COBOL)
 
(24 intermediate revisions by 19 users not shown)
Line 32:
:* [[Möbius function]]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F mertens(count)
‘Generate Mertens numbers’
V m = [-1, 1]
L(n) 2 .. count
m.append(1)
L(k) 2 .. n
m[n] -= m[n I/ k]
R m
 
V ms = mertens(1000)
 
print(‘The first 99 Mertens numbers are:’)
print(‘ ’, end' ‘ ’)
V col = 1
L(n) ms[1.<100]
print(‘#2’.format(n), end' ‘ ’)
col++
I col == 10
print()
col = 0
 
V zeroes = sum(ms.map(x -> Int(x == 0)))
V crosses = sum(zip(ms, ms[1..]).map((a, b) -> Int(a != 0 & b == 0)))
print(‘M(N) equals zero #. times.’.format(zeroes))
print(‘M(N) crosses zero #. times.’.format(crosses))</syntaxhighlight>
 
{{out}}
<pre>
The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
M(N) equals zero 92 times.
M(N) crosses zero 59 times.
</pre>
 
=={{header|360 Assembly}}==
<syntaxhighlight lang="360asm">* Mertens function - 01/05/2023
MERTENS CSECT
USING MERTENS,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
SAVE (14,12) save previous context
ST R13,4(R15) link backward
ST R15,8(R13) link forward
LR R13,R15 set addressability
LA R0,1 1
STH R0,MM m(1)=1
LA R6,2 i=2
DO WHILE=(CH,R6,LE,=AL2(NN)) do i=2 to n
LR R1,R6 i
SLA R1,1 *2 (H)
LA R0,1 1
STH R0,MM-2(R1) m(i)=1
LA R7,2 j=2
DO WHILE=(CR,R7,LE,R6) do j=2 to i
LR R4,R6 i
SRDA R4,32 ~
LR R1,R7 j
DR R4,R1 i/j
LR R8,R5 d=i/j
LR R4,R6 i
SLA R4,1 *2 (H)
LH R2,MM-2(R4) m(i)
LR R1,R8 d
SLA R1,1 *2 (H)
LH R3,MM-2(R1) m(d)
SR R2,R3 m(i)-m(d)
STH R2,MM-2(R4) m(i)=m(i)-m(d)
LA R7,1(R7) j++
ENDDO , enddo j
LA R6,1(R6) i++
ENDDO , enddo i
XPRNT =C'the first 99 Mertens numbers are:',34 print buffer
LA R9,PG @buffer=pg
MVC PG,=CL80' ' clean buffer
MVC 0(3,R9),=CL3' ' output ' '
LA R9,3(R9) @buffer+=3
LA R7,9 j=9
LA R6,1 i=1
DO WHILE=(CH,R6,LE,=AL2(99)) do i=1 to 99
LR R1,R6 i
SLA R1,1 *2 (H)
LH R2,MM-2(R1) m(i)
XDECO R2,XDEC edit m(i)
MVC 0(3,R9),XDEC+9 output m(i)
LA R9,3(R9) @buffer+=3
BCTR R7,0 j=j-1
IF LTR,R7,Z,R7 THEN if j=0 then do;
LA R7,10 j=10
XPRNT PG,L'PG print buffer
LA R9,PG @buffer=pg
ENDIF , endif
LA R6,1(R6) i++
ENDDO , enddo i
SR R10,R10 zero=0
SR R11,R11 cross=0
LA R6,1 i=2
DO WHILE=(CH,R6,LE,=AL2(NN)) do i=2 to n
LR R1,R6 i
SLA R1,1 *2 (H)
LH R2,MM-2(R1) m(i)
IF LTR,R2,Z,R2 THEN if m(i)=0 then
LA R10,1(R10) zero=zero+1
LR R1,R6 i
BCTR R1,0 i-1
SLA R1,1 *2 (H)
LH R2,MM-2(R1) m(i-1)
IF LTR,R2,NZ,R2 THEN if m(i-1)^=0 then
LA R11,1(R11) cross=cross+1
ENDIF , endif
ENDIF , endif
LA R6,1(R6) i++
ENDDO , enddo i
MVC PG,=CL80' ' clean buffer
MVC PG(13),=C'm(i) is zero '
XDECO R10,XDEC edit zero
MVC PG+13(2),XDEC+10 output zero
MVC PG+15(7),=C' times.'
XPRNT PG,L'PG print buffer
MVC PGI,=H'0'
MVC PG,=CL80' ' clean buffer
MVC PG(18),=C'm(i) crosses zero '
XDECO R11,XDEC edit cross
MVC PG+18(2),XDEC+10 output cross
MVC PG+20(7),=C' times.'
XPRNT PG,L'PG print buffer
L R13,4(0,R13) restore previous savearea pointer
RETURN (14,12),RC=0 restore registers from calling save
NN EQU 1000 n
PG DS CL80 buffer
PGI DC H'0' buffer index
XDEC DS CL12 temp for xdeci xdeco
MM DS (NN)H m
REGEQU
END MERTENS</syntaxhighlight>
{{out}}
<pre>
the first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
m(i) is zero 92 times.
m(i) crosses zero 59 times.
</pre>
 
=={{header|8080 Assembly}}==
<langsyntaxhighlight lang="8080asm">MAX: equ 1000 ; Amount of numbers to generate
org 100h
;;; Generate Mertens numbers
Line 232 ⟶ 395:
tms: db ' times.$'
;;; Numbers are stored page-aligned after program
MM: equ ($/256)*256+256 </langsyntaxhighlight>
 
{{out}}
Line 249 ⟶ 412:
M(N) is zero 92 times.
M(N) crosses zero 59 times.</pre>
 
 
=={{header|8086 Assembly}}==
<langsyntaxhighlight lang="asm">MAX: equ 1000 ; Amount of Mertens numbers to generate
puts: equ 9 ; MS-DOS syscall to print a string
putch: equ 2 ; MS-DOS syscall to print a character
Line 353 ⟶ 515:
section .bss
mm: resb MAX ; Mertens numbers
M: equ mm-1 ; 1-based indexing</langsyntaxhighlight>
 
{{out}}
Line 370 ⟶ 532:
M(N) is zero 92 times.
M(N) crosses zero 59 times.</pre>
 
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
 
PROC MertensNumbers(INT ARRAY m INT count)
INT n,k
 
m(1)=1
FOR n=2 TO count
DO
m(n)=1
FOR k=2 TO n
DO
m(n)==-m(n/k)
OD
OD
RETURN
 
PROC PrintMertens(INT ARRAY m INT count)
CHAR ARRAY s(6)
INT i,col
 
PrintF("First %I Mertens numbers:%E ",count)
col=1
FOR i=1 TO count
DO
StrI(m(i),s)
PrintF("%3S",s)
col==+1
IF col=10 THEN
col=0 PutE()
FI
OD
RETURN
 
PROC Main()
DEFINE MAX="1001"
INT ARRAY m(MAX)
INT i,zeroCnt=[0],crossCnt=[0],prev=[0]
 
Put(125) PutE() ;clear the screen
PrintF("Calculation of Mertens numbers,%E please wait...")
MertensNumbers(m,MAX)
 
Put(125) PutE() ;clear the screen
PrintMertens(m,99)
 
FOR i=1 TO MAX
DO
IF m(i)=0 THEN
zeroCnt==+1
IF prev THEN
crossCnt==+1
FI
FI
prev=m(i)
OD
PrintF("%EM(n) is zero %I times for 1<=n<=%I.%E",zeroCnt,MAX-1)
PrintF("%EM(n) crosses zero %I times for 1<=n<=%I.%E",crossCnt,MAX-1)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Mertens_function.png Screenshot from Atari 8-bit computer]
<pre>
First 99 Mertens numbers:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
 
M(n) is zero 92 times for 1<=n<=1000.
 
M(n) crosses zero 59 times for 1<=n<=1000.
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|ALGOL W}}
...which is...
{{Trans|Fortran}}
<syntaxhighlight lang="algol68">BEGIN # compute values of the Mertens function #
# Generate Mertens numbers #
[ 1 : 1000 ]INT m;
m[ 1 ] := 1;
FOR n FROM 2 TO UPB m DO
m[ n ] := 1;
FOR k FROM 2 TO n DO m[ n ] -:= m[ n OVER k ] OD
OD;
# Print table #
print( ( "The first 99 Mertens numbers are:", newline ) );
print( ( " " ) );
INT k := 9;
FOR n TO 99 DO
print( ( whole( m[ n ], -3 ) ) );
IF ( k -:= 1 ) = 0 THEN
k := 10;
print( ( newline ) )
FI
OD;
# Calculate zeroes and crossings #
INT zero := 0;
INT cross := 0;
FOR n FROM 2 TO UPB m DO
IF m[ n ] = 0 THEN
zero +:= 1;
IF m[ n - 1 ] /= 0 THEN cross +:= 1 FI
FI
OD;
print( ( newline ) );
print( ( "M(N) is zero ", whole( zero, -4 ), " times.", newline ) );
print( ( "M(N) crosses zero ", whole( cross, -4 ), " times.", newline ) )
END</syntaxhighlight>
{{out}}
<pre>
The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
 
M(N) is zero 92 times.
M(N) crosses zero 59 times.
</pre>
 
=={{header|ALGOL W}}==
{{Trans|Fortran}}
<langsyntaxhighlight lang="algolw">begin % compute values of the Mertens function %
integer array M ( 1 :: 1000 );
integer k, zero, cross;
Line 405 ⟶ 702:
write( i_w := 2, s_w := 0, "M(N) is zero ", zero, " times." );
write( i_w := 2, s_w := 0, "M(N) crosses zero ", cross, " times." )
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 426 ⟶ 723:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">mertens←{
step ← {⍵,-⍨/⌽1,⍵[⌊n÷1↓⍳n←1+≢⍵]}
m1000 ← step⍣999⊢,1
Line 435 ⟶ 732:
⎕←'M(N) is zero ',(⍕zero),' times.'
⎕←'M(N) crosses zero ',(⍕cross),' times.'
}</langsyntaxhighlight>
 
{{out}}
Line 452 ⟶ 749:
M(N) is zero 92 times.
M(N) crosses zero 59 times.</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">mobius: function [n][
if n=0 -> return ""
if n=1 -> return 1
f: factors.prime n
 
if f <> unique f -> return 0
if? odd? size f -> return neg 1
else -> return 1
]
 
mertens: function [z][sum map 1..z => mobius]
 
print "The first 99 Mertens numbers are:"
loop split.every:20 [""]++map 1..99 => mertens 'a [
print map a 'item -> pad to :string item 2
]
 
print ""
 
mertens1000: map 1..1000 => mertens
print ["Times M(x) is zero between 1 and 1000:" size select mertens1000 => zero?]
 
crossed: new 0
fold mertens1000 [a,b][if and? zero? b not? zero? a -> inc 'crossed, b]
print ["Times M(x) crosses zero between 1 and 1000:" crossed]</syntaxhighlight>
 
{{out}}
 
<pre>The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2 -1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2 -3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3 -3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1 -2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2 -2 -1 -1 0 1 2 2 1 1 1
 
Times M(x) is zero between 1 and 1000: 92
Times M(x) crosses zero between 1 and 1000: 59</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">result := "first 100 terms:`n"
loop 100
result .= SubStr(" " Mertens(A_Index), -1) . (Mod(A_Index, 10) ? " " : "`n")
 
eqZero := crZero := 0, preced:=1
loop 1000
{
if !(x := Mertens(A_Index))
eqZero++, crZero += preced<>0 ? 1 : 0
preced := x
}
result .= "`nfirst 1000 terms:"
MsgBox, 262144, , % result .= "`nequal to zero : " eqZero "`ncrosses zero : " crZero
return
 
Mertens(n){
loop % n
result += Möbius(A_Index)
return result
}
 
 
Möbius(n){
if n=1
return 1
x := prime_factors(n)
c := x.Count()
sq := []
for i, v in x
if sq[v]
return 0
else
sq[v] := 1
return (c/2 = floor(c/2)) ? 1 : -1
}
 
prime_factors(n) {
if (n <= 3)
return [n]
ans := [], done := false
while !done {
if !Mod(n, 2)
ans.push(2), n /= 2
else if !Mod(n, 3)
ans.push(3), n /= 3
else if (n = 1)
return ans
else {
sr := sqrt(n), done := true, i := 6
while (i <= sr+6) {
if !Mod(n, i-1) { ; is n divisible by i-1?
ans.push(i-1), n /= i-1, done := false
break
}
if !Mod(n, i+1) { ; is n divisible by i+1?
ans.push(i+1), n /= i+1, done := false
break
}
i += 6
}}}
ans.push(Format("{:d}", n))
return ans
}</syntaxhighlight>
{{out}}
<pre>first 100 terms:
1 0 -1 -1 -2 -1 -2 -2 -2 -1
-2 -2 -3 -2 -1 -1 -2 -2 -3 -3
-2 -1 -2 -2 -2 -1 -1 -1 -2 -3
-4 -4 -3 -2 -1 -1 -2 -1 0 0
-1 -2 -3 -3 -3 -2 -3 -3 -3 -3
-2 -2 -3 -3 -2 -2 -1 0 -1 -1
-2 -1 -1 -1 0 -1 -2 -2 -1 -2
-3 -3 -4 -3 -3 -3 -2 -3 -4 -4
-4 -3 -4 -4 -3 -2 -1 -1 -2 -2
-1 -1 0 1 2 2 1 1 1 1
 
first 1000 terms:
equal to zero : 92
crosses zero : 59</pre>
 
=={{header|BASIC}}==
Line 461 ⟶ 879:
a little over 1 hour on the 8086 (using GWBASIC).
 
<langsyntaxhighlight BASIClang="basic">10 DEFINT C,Z,N,K,M: DIM M(1000)
20 M(1)=1
30 FOR N=2 TO 1000
Line 479 ⟶ 897:
170 PRINT "M(N) is zero";Z;"times."
180 PRINT "M(N) crosses zero";C;"times."
190 END</langsyntaxhighlight>
 
{{out}}
Line 497 ⟶ 915:
M(N) crosses zero 59 times.</pre>
 
==={{header|BashBASIC256}}===
<syntaxhighlight lang="freebasic">arraybase 1
<lang bash>#!/bin/bash
dim M(1000)
M[1] = 1
for n = 2 to 1000
M[n] = 1
for k = 2 to n
M[n] = M[n] - M[int(n/k)]
next k
next n
print "First 99 Mertens numbers:"
print " ";
for n = 1 to 99
print rjust(string(M[n]),3);
if n mod 10 = 9 then print
next n
numCruza = 0
numEsCero = 0
for n = 1 to 1000
if M[n] = 0 then
numEsCero += 1
if M[n-1] <> 0 then numCruza += 1
end if
next n
print
print "M(n) is zero "; numEsCero; " times."
print "M(n) crosses zero "; numCruza; " times."</syntaxhighlight>
{{out}}
<pre>Same as BASIC entry.</pre>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="freebasic">dim M(1000)
M(1) = 1
for n = 2 to 1000
M(n) = 1
for k = 2 to n
M(n) = M(n)-M(int(n/k))
next k
next n
print "First 99 Mertens numbers:"
print " ";
for n = 1 to 99
print using("###", M(n));
if n mod 10 = 9 then print
next n
numCruza = 0
numEsCero = 0
for n = 1 to 1000
if M(n) = 0 then
numEsCero = numEsCero +1
if M(n-1) <> 0 then numCruza = numCruza +1
end if
next n
print
print "M(n) is zero "; numEsCero; " times."
print "M(n) crosses zero "; numCruza; " times."</syntaxhighlight>
{{out}}
<pre>Same as BASIC entry.</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">DIM m(1000)
LET m(1) = 1
FOR n = 2 TO 1000
LET m(n) = 1
FOR k = 2 TO n
LET m(n) = m(n)-m(INT(n/k))
NEXT k
NEXT n
PRINT "First 99 Mertens numbers:"
PRINT " ";
FOR n = 1 TO 99
PRINT " ";
PRINT USING "##": m(n);
!IF REMAINDER(ROUND(n),10) = 9 THEN PRINT
IF MOD(n,10) = 9 THEN PRINT
NEXT n
LET numcruza = 0
LET numeszero = 0
FOR n = 1 TO 1000
IF m(n) = 0 THEN
LET numeszero = numeszero+1
IF m(n-1) <> 0 THEN LET numcruza = numcruza+1
END IF
NEXT n
PRINT
PRINT "M(n) is zero"; numeszero; "times."
PRINT "M(n) crosses zero"; numcruza; "times."
END</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">PROGRAM "Mertens"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
DIM M[1000]
M[1] = 1
FOR n = 2 TO 1000
M[n] = 1
FOR k = 2 TO n
M[n] = M[n] - M[INT(n/k)]
NEXT k
NEXT n
PRINT "First 99 Mertens numbers:"
PRINT " ";
FOR n = 1 TO 99
PRINT FORMAT$("###", M[n]);
IF n MOD 10 = 9 THEN PRINT
NEXT n
numCruza = 0
numEsCero = 0
FOR n = 1 TO 1000
IF M[n] = 0 THEN
INC numEsCero
IF M[n-1] <> 0 THEN INC numCruza
END IF
NEXT n
PRINT
PRINT "M(n) is zero"; numEsCero; " times."
PRINT "M(n) crosses zero"; numCruza; " times."
 
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="freebasic">dim M(1000)
M(1) = 1
for n = 2 to 1000
M(n) = 1
for k = 2 to n
M(n) = M(n) - M(int(n/k))
next k
next n
print "First 99 Mertens numbers:"
print " ";
for n = 1 to 99
print M(n) using("###");
if mod(n, 10) = 9 print
next n
numCruza = 0
numEsCero = 0
for n = 1 to 1000
if M(n) = 0 then
numEsCero = numEsCero + 1
if M(n-1) <> 0 numCruza = numCruza + 1
end if
next n
print
print "M(n) is zero ", numEsCero, " times."
print "M(n) crosses zero ", numCruza, " times."</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>=={{header|Bash}}==
<syntaxhighlight lang="bash">#!/bin/bash
MAX=1000
 
Line 531 ⟶ 1,108:
 
echo "M(N) is zero $zero times."
echo "M(N) crosses zero $cross times."</langsyntaxhighlight>
 
{{out}}
Line 550 ⟶ 1,127:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
manifest $( limit = 1000 $)
Line 585 ⟶ 1,162:
writef("M(N) is zero %N times.*N", eqz)
writef("M(N) crosses zero %N times.*N", crossz)
$)</langsyntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers are:
Line 602 ⟶ 1,179:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 654 ⟶ 1,231:
printf("M(n) crosses zero %d times for 1 <= n <= %d.\n", cross, max);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 674 ⟶ 1,251:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 716 ⟶ 1,293:
std::cout << "M(n) crosses zero " << cross << " times for 1 <= n <= 1000.\n";
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 734 ⟶ 1,311:
M(n) crosses zero 59 times for 1 <= n <= 1000.
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% 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</syntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
M(N) is zero 92 times.
M(N) crosses zero 59 times.</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MERTENS.
 
Line 801 ⟶ 1,436:
SUBTRACT 1 FROM N GIVING K,
IF M(K) IS NOT EQUAL TO ZERO,
ADD 1 TO CROSS-ZERO.</langsyntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers are:
Line 818 ⟶ 1,453:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
const MAX := 1000;
Line 880 ⟶ 1,515:
 
print("M(n) is zero "); print_i8(zero); print(" times\n");
print("M(n) crosses zero "); print_i8(cross); print(" times\n");</langsyntaxhighlight>
 
{{out}}
Line 901 ⟶ 1,536:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Mertens_function;
 
Line 996 ⟶ 1,631:
writeln(#10'Crosses zero ', m.crosses, ' times between 1 and 1000');
{$IFNDEF UNIX} readln; {$ENDIF}
end.</langsyntaxhighlight>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec mertens([*] short m) void:
word n,k;
m[1] := 1;
for n from 2 upto dim(m,1)-1 do
m[n] := 1;
for k from 2 upto n do
m[n] := m[n] - m[n/k]
od
od
corp
 
proc nonrec main() void:
[1001] short m;
word x, y, eqz, crossz;
mertens(m);
writeln("The first 99 Mertens numbers are:");
for y from 0 by 10 upto 90 do
for x from 0 upto 9 do
if x+y=0
then write(" ")
else write(m[x+y]:3)
fi
od;
writeln()
od;
eqz := 0;
crossz := 0;
for x from 2 upto dim(m,1)-1 do
if m[x]=0 then
eqz := eqz + 1;
if m[x-1]~=0 then crossz := crossz + 1 fi
fi
od;
writeln("M(N) is zero ",eqz," times.");
writeln("M(N) crosses zero ",crossz," times.")
corp</syntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
M(N) is zero 92 times.
M(N) crosses zero 59 times.</pre>
 
=={{header|Dyalect}}==
Line 1,002 ⟶ 1,691:
{{trans|Swift}}
 
<langsyntaxhighlight lang="dyalect">func mertensNumbers(max) {
let mertens = Array.emptyEmpty(max + 1, 1)
for n in 2..max {
for k in 2..n {
Line 1,018 ⟶ 1,707:
let columns = 20
print("First \(count - 1) Mertens numbers:")
 
for i in 0..<count {
if i % columns > 0 {
print(" ", terminator: "")
}
print(i == 0 ? " " : mertens[i].toStringToString().padLeftPadLeft(2, ' ') + " ", terminator: "")
if (i + 1) % columns == 0 {
print()
Line 1,040 ⟶ 1,729:
previous = m
}
 
print("M(n) is zero \(zero) times for 1 <= n <= \(max).")
print("M(n) crosses zero \(cross) times for 1 <= n <= \(max).")</langsyntaxhighlight>
 
{{out}}
Line 1,059 ⟶ 1,748:
M(n) is zero 92 times for 1 <= n <= 1000.
M(n) crosses zero 59 times for 1 <= n <= 1000.</pre>
 
=={{header|EasyLang}}==
{{trans|FutureBasic}}
<syntaxhighlight>
len mertens[] 1000
mertens[1] = 1
for n = 2 to 1000
mertens[n] = 1
for k = 2 to n
mertens[n] -= mertens[n div k]
.
.
print "First 99 Mertens numbers:"
write " "
numfmt 0 2
for n = 1 to 99
write mertens[n] & " "
if n mod 10 = 9
print ""
.
.
for n = 1 to 1000
if mertens[n] = 0
zeros += 1
if mertens[n - 1] <> 0
crosses += 1
.
.
.
print ""
print "In the first 1000 terms of the Mertens sequence there are:"
print zeros & " zeros"
print crosses & " zero crosses"
</syntaxhighlight>
{{out}}
<pre>
First 99 Mertens numbers:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
 
In the first 1000 terms of the Mertens sequence there are:
92 zeros
59 zero crosses
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Möbius_function#F.23 Möbius_function (F#)]
<langsyntaxhighlight lang="fsharp">
// Mertens function. Nigel Galloway: January 31st., 2021
let mertens=mobius|>Seq.scan((+)) 0|>Seq.tail
Line 1,070 ⟶ 1,811:
printfn "%d Zeroes\n####" (Seq.length (snd n.[0]))
printfn "Crosses zero %d times" (mertens|>Seq.take 1000|>Seq.pairwise|>Seq.sumBy(fun(n,g)->if n<>0 && g=0 then 1 else 0)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,122 ⟶ 1,863:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io kernel math math.extras
math.ranges math.statistics prettyprint sequences ;
 
Line 1,139 ⟶ 1,880:
2 <clumps> [ first2 [ 0 = not ] [ zero? ] bi* and ] count bl
pprint bl "zero crossings." print
] bi</langsyntaxhighlight>
{{out}}
<pre>
Line 1,160 ⟶ 1,901:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: AMOUNT 1000 ;
 
variable mertens AMOUNT cells allot
Line 1,210 ⟶ 1,951:
." M(N) is zero " . ." times." cr
." M(N) crosses zero " . ." times." cr
bye </langsyntaxhighlight>
 
{{out}}
Line 1,229 ⟶ 1,970:
 
=={{header|Fortran}}==
<langsyntaxhighlight Fortranlang="fortran"> program Mertens
implicit none
integer M(1000), n, k, zero, cross
Line 1,268 ⟶ 2,009:
50 format("M(N) crosses zero ",I2," times.")
write (*,50) cross
end program</langsyntaxhighlight>
 
{{out}}
Line 1,287 ⟶ 2,028:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function padto( i as ubyte, j as integer ) as string
return wspace(i-len(str(j)))+str(j)
end function
Line 1,320 ⟶ 2,061:
outstr = ""
end if
next n</langsyntaxhighlight>
{{out}}
<pre>
Line 1,336 ⟶ 2,077:
-4 -3 -4 -4 -3 -2 -1 -1 -2 -2
-1 -1 0 1 2 2 1 1 1 1
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn MertensFunction
long mertens(1000), n, k, crossesTotal = 0, zerosTotal = 0
mertens(1) = 1
for n = 2 to 1000
mertens(n) = 1
for k = 2 to n
mertens(n) = mertens(n) - mertens(n/k)
next
next
printf @"First 99 Mertens numbers:\n \b"
for n = 1 to 99
printf @"%3ld \b", mertens(n)
if ( n mod 10 == 9 ) then print
next
for n = 1 to 1000
if ( mertens(n) == 0 )
zerosTotal++
if mertens(n-1) != 0 then crossesTotal++
end if
next
print
printf @"mertens(n) array is zero %ld times.", zerosTotal
printf @"mertens(n) array crosses zero %ld times.", crossesTotal
end fn
 
fn MertensFunction
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
First 99 Mertens numbers:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
 
mertens(n) array is zero 92 times.
mertens(n) array crosses zero 59 times.
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,404 ⟶ 2,200:
fmt.Println("\n\nEquals zero", zeros, "times between 1 and 1000")
fmt.Println("\nCrosses zero", crosses, "times between 1 and 1000")
}</langsyntaxhighlight>
 
{{out}}
Line 1,424 ⟶ 2,220:
Crosses zero 59 times between 1 and 1000
</pre>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
import qualified Data.MemoCombinators as Memo
import Math.NumberTheory.Primes (unPrime, factorise)
Line 1,458 ⟶ 2,255:
mapM_ (\row -> mapM_ (printf "%3d" . mertens) row >> printf "\n") $ chunksOf 10 [10..99]
printf "\nM(n) is zero %d times for 1 <= n <= 1000.\n" $ countZeros [1..1000]
printf "M(n) crosses zero %d times for 1 <= n <= 1000.\n" $ crossesZero [1..1000]</langsyntaxhighlight>
{{out}}
<pre>The first 99 terms for M(1..99):
Line 1,477 ⟶ 2,274:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">mu =: 0:`(1 - 2 * 2|#@{.)@.(1: = */@{:)@(2&p:)"0
M =: +/@([: mu 1:+i.)
 
Line 1,488 ⟶ 2,285:
echo 'M(N) is zero ',(":zero),' times.'
echo 'M(N) crosses zero ',(":cross),' times.'
exit''</langsyntaxhighlight>
 
{{out}}
Line 1,507 ⟶ 2,304:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class MertensFunction {
 
Line 1,617 ⟶ 2,414:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,696 ⟶ 2,493:
 
'''Preliminaries'''
<syntaxhighlight lang="jq">
<lang jq>
def sum(s): reduce s as $x (null; . + $x);
 
Line 1,715 ⟶ 2,512:
end
| .prev = $a[$i] )
| .count;</langsyntaxhighlight>
'''Mertens Numbers'''
<syntaxhighlight lang="jq">
<lang jq>
# Input: $max >= 1
# Output: an array of size $max with $max mertenNumbers beginning with 1
Line 1,725 ⟶ 2,522:
.[$n-1]=1
| reduce range(2; $n+1) as $k (.;
.[$n-1] -= .[($n / $k) | floor - 1] ));</langsyntaxhighlight>
'''The Tasks'''
<langsyntaxhighlight lang="jq"># Task 0:
def mertens_number:
mertensNumbers[.-1];
Line 1,750 ⟶ 2,547:
(1000 | (task2, task3))
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,772 ⟶ 2,569:
=={{header|Julia}}==
The OEIS A002321 reference suggests the Mertens function has a negative bias, which it does below 1 million, but this bias seems to switch to a positive bias by 1 billion. There may simply be large swings in the bias overall, which get larger and longer as the sequence continues.
<langsyntaxhighlight lang="julia">using Primes, Formatting
 
function moebius(n::Integer)
Line 1,829 ⟶ 2,626:
 
foreach(maximinM, (1000, 1_000_000, 1_000_000_000))
</langsyntaxhighlight>{{out}}
<pre>
First 99 terms of the Mertens function for positive integers:
Line 1,872 ⟶ 2,669:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
DIMENSION M(1000)
Line 1,904 ⟶ 2,701:
PRINT FORMAT FC, CROSS
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,923 ⟶ 2,720:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Mertens]
Mertens[n_] := Total[MoebiusMu[Range[n]]]
Grid[Partition[Mertens /@ Range[99], UpTo[10]]]
Count[Mertens /@ Range[1000], 0]
SequenceCount[Mertens /@ Range[1000], {Except[0], 0}]</langsyntaxhighlight>
{{out}}
<pre>1 0 -1 -1 -2 -1 -2 -2 -2 -1
Line 1,941 ⟶ 2,738:
92
59</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE Mertens;
FROM InOut IMPORT WriteString, WriteInt, WriteCard, WriteLn;
 
CONST Max = 1000;
VAR n, k, x, y, zero, cross: CARDINAL;
M: ARRAY [1..Max] OF INTEGER;
 
BEGIN
M[1] := 1;
FOR n := 2 TO Max DO
M[n] := 1;
FOR k := 2 TO n DO
M[n] := M[n] - M[n DIV k];
END;
END;
 
WriteString("The first 99 Mertens numbers are:");
WriteLn();
FOR y := 0 TO 90 BY 10 DO
FOR x := 0 TO 9 DO
IF x+y=0 THEN WriteString(" ");
ELSE WriteInt(M[x+y], 3);
END;
END;
WriteLn();
END;
 
zero := 0;
cross := 0;
FOR n := 2 TO Max DO
IF M[n] = 0 THEN
zero := zero + 1;
IF M[n-1] # 0 THEN
cross := cross + 1;
END;
END;
END;
 
WriteString("M(n) is zero ");
WriteCard(zero,0);
WriteString(" times.");
WriteLn();
WriteString("M(n) crosses zero ");
WriteCard(cross,0);
WriteString(" times.");
WriteLn();
END Mertens.</syntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
M(n) is zero 92 times.
M(n) crosses zero 59 times.</pre>
 
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight Nimlang="nim">import sequtils, strformat
 
func mertensNumbers(max: int): seq[int] =
Line 1,977 ⟶ 2,837:
echo ""
echo &"M(n) is zero {zero} times for 1 ⩽ n ⩽ {Max}."
echo &"M(n) crosses zero {cross} times for 1 ⩽ n ⩽ {Max}."</langsyntaxhighlight>
 
{{out}}
Line 1,998 ⟶ 2,858:
Nearly the same as [[Square-free_integers#Pascal]]
Instead here marking all multiples, starting at factor 2, of a prime by incrementing the factor count.<BR> runtime ~log(n)*n
<langsyntaxhighlight lang="pascal">program Merten;
{$IFDEF FPC}
{$MODE DELPHI}
Line 2,222 ⟶ 3,082:
setlength(primes,0);
setlength(sieve,0);
end.</langsyntaxhighlight>
{{out}}
<pre>[1 to limit]
Line 2,284 ⟶ 3,144:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use utf8;
use strict;
use warnings;
Line 2,316 ⟶ 3,176:
(' 'x4 . sprintf "@{['%4d' x $show]}", @mertens[0..$show-1]) =~ s/((.){80})/$1\n/gr .
sprintf("\nEquals zero %3d times between 1 and $upto", scalar grep { ! $_ } @mertens) .
sprintf "\nCrosses zero%3d times between 1 and $upto", scalar grep { ! $mertens[$_-1] and $mertens[$_] } 1 .. @mertens;</langsyntaxhighlight>
{{out}}
<pre>Mertens sequence - First 199 terms:
Line 2,334 ⟶ 3,194:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
Based on the stackexchange link, short and sweet but not very fast: 1.4s just for the first 1000...
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<!--<lang Phix>(phixonline)-->
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (skip arg added to join_by)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">mcache</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Mertens</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080008080;">integerfor</span> <span style="color: #000000;">resm</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mcache</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000004080;">kinteger</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2mm</span> <span style="color: #0080800000FF;">to=</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do1</span>
<span style="color: #000000008080;">res</span> <span style="color: #0000FF;">-=for</span> <span style="color: #000000;">Mertensk</span><span style="color: #0000FF;">(=</span><span style="color: #7060A8000000;">floor2</span><span style="color: #0000FF;">(</span><span style="color: #000000008080;">nto</span><span style="color: #0000FF;">/</span><span style="color: #000000;">km</span> <span style="color: #0000FF008080;">))do</span>
<span style="color: #000000;">mm</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">mcache</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">/</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">mcache</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">mm</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">resmcache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080008080;">sequenceconstant</span> <span style="color: #000000;">sfirst</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF000000;">{99</span><span style="color: #0080000000FF;">",</span> <span .style="color: #000000;">perline</span> <span style="color: #0000FF;">}=</span> <span style="color: #000000;">10</span>
<span style="color: #000080;font-style:italic;">--constant first = 199, perline = 20 -- matches C/Go/etc
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">143</span> <span style="color: #008080;">do</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Mertens</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
--constant first = 143, perline = 12 -- matches wp</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">" ."</span><span style="color: #0000FF;">}&</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">first</span><span style="color: #0000FF;">),</span><span style="color: #000000;">Mertens</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First %d Mertens numbers:\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">first</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">perline</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">skip</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">zeroes</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">crosses</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
Line 2,357 ⟶ 3,226:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nMertens[1..1000] equals zero %d times and crosses zero %d times\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">zeroes</span><span style="color: #0000FF;">,</span><span style="color: #000000;">crosses</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
Matches the wp table:
<pre>
First 99 Mertens numbers:
. 1 0 -1 -1 -2 -1 -2 -2 -2 -1 -2
-2 -3. -2 1 -1 0 -1 -21 -2 -31 -32 -2 -1 -2
-21 -2 -12 -1 -13 -2 -31 -41 -4 -32 -2 -13
-13 -2 -1 0 0-2 -12 -2 -31 -31 -31 -2 -3
-3 -34 -34 -23 -2 -31 -3 -21 -2 -1 0 -1
-1 -20 -1 -12 -1 03 -13 -23 -2 -13 -23 -3
-3 -42 -32 -3 -3 -2 -32 -41 -4 -4 -30 -41
-41 -32 -21 -1 -1 -2 -20 -1 -12 0 -2 -1 2
-2 -3 1 -3 1-4 1 1 0-3 -13 -23 -2 -3 -2 -34
-34 -4 -53 -4 -4 -53 -62 -51 -51 -5 -4 -32
-32 -31 -21 -1 0 -1 -1 -1 2 -2 -2 - 1 -2 1 -3 1
-3 -2 -1 -1 -1 -2 -3 -4 -4 -3 -2 -1
 
Mertens[1..1000] equals zero 92 times and crosses zero 59 times
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">mertens: procedure options(main);
%replace MAX by 1000;
declare M(1:MAX) fixed binary(5);
declare (n, k) fixed binary(10);
declare (isZero, crossZero) fixed binary(8);
M(1) = 1;
do n = 2 to MAX;
M(n) = 1;
do k = 2 to n;
M(n) = M(n) - M(divide(n,k,10));
end;
end;
put skip list('The first 99 Mertens numbers are:');
put skip list(' ');
do n = 1 to 99;
put edit(M(n)) (F(3));
if mod(n,10) = 9 then put skip;
end;
isZero = 0;
crossZero = 0;
do n = 2 to MAX;
if M(n) = 0 then do;
isZero = isZero + 1;
if M(n-1) ^= 0 then
crossZero = crossZero + 1;
end;
end;
put skip list('Zeroes: ',isZero);
put skip list('Crossings:',crossZero);
end mertens;</syntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
 
Zeroes: 92
Crossings: 59</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">:- dynamic mertens_number_cache/2.
 
mertens_number(1, 1):- !.
Line 2,436 ⟶ 3,355:
count_zeros(1, 1000, Z, C),
writef('M(n) is zero %t times for 1 <= n <= 1000.\n', [Z]),
writef('M(n) crosses zero %t times for 1 <= n <= 1000.\n', [C]).</langsyntaxhighlight>
 
{{out}}
Line 2,456 ⟶ 3,375:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Dim M.i(1000)
 
M(1)=1
Line 2,469 ⟶ 3,388:
For n=1 To 99 : Print(RSet(Str(M(n)),4)) : If n%10=9 : PrintN("") : EndIf : Next
PrintN("M(N) is zero "+Str(z)+" times.") : PrintN("M(N) crosses zero "+Str(c)+" times.")
Input()</langsyntaxhighlight>
{{out}}
<pre>First 99 Mertens numbers:
Line 2,486 ⟶ 3,405:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def mertens(count):
"""Generate Mertens numbers"""
m = [None, 1]
Line 2,511 ⟶ 3,430:
crosses = sum(a!=0 and b==0 for a,b in zip(ms, ms[1:]))
print("M(N) equals zero {} times.".format(zeroes))
print("M(N) crosses zero {} times.".format(crosses))</langsyntaxhighlight>
 
{{out}}
Line 2,528 ⟶ 3,447:
M(N) equals zero 92 times.
M(N) crosses zero 59 times.</pre>
 
=={{header|Quackery}}==
 
<code>mobius</code> is defined at [[Möbius function#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ ' [ 0 ]
swap 1+ times
[ dup -1 peek
i^ 1+ mobius
+ join ]
behead drop ] is mertens ( n --> [ )
 
[ say " "
99 times
[ dup i^ peek
dup dup
-1 > if sp
abs 10 < if sp
echo
i^ 1+ 10 mod
9 = if cr ]
drop ] is grid ( [ --> )
 
[ 0 swap
witheach
[ 0 = + ] ] is zeroes ( [ --> n )
 
[ 0 0
rot witheach
[ dup 0 =
rot 0 !=
and
rot + swap ]
drop ] is crossings ( [ --> n )
 
1000 mertens
say "First 99 terms:"
cr
dup grid
cr
dup zeroes echo say " zeroes and "
crossings echo say " crossings"</syntaxhighlight>
 
{{out}}
 
<pre>First 99 terms:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
 
92 zeroes and 59 crossings</pre>
 
=={{header|Raku}}==
Line 2,534 ⟶ 3,511:
Mertens number is not defined for n == 0. Raku arrays are indexed from 0 so store a blank value at position zero to keep x and M(x) aligned.
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
sub μ (Int \n) {
Line 2,554 ⟶ 3,531:
printf "%4d: M(%d)\n", -$threshold, @mertens.first: * == -$threshold, :k;
printf "%4d: M(%d)\n", $threshold, @mertens.first: * == $threshold, :k;
}</langsyntaxhighlight>
{{out}}
<pre>Mertens sequence - First 199 terms:
Line 2,606 ⟶ 3,583:
 
The above "feature" was added to make the grid to be aligned with other solutions.
<langsyntaxhighlight lang="rexx">/*REXX pgm computes & shows a value grid of the Mertens function for a range of integers*/
parse arg LO HI grp eqZ xZ . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 0 /*Not specified? Then use the default.*/
Line 2,615 ⟶ 3,592:
call genP /*generate primes up to max √ HIHI */
call Franz LO, HI
if eqZ>0 then call Franz 1, -eqZ
if xZ>0 then call Franz -1, xZ
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Franz: parse arg a 1 oa,b 1 ob; @Mertens= ' The Mertens sequence from '
a= abs(a); b= abs(b); grid= oa>=0 & ob>=0 /*semaphore used to show a grid title. */
if grid then say center(@Mertens LO " ──► " HI" ", max(50, grp*3), '═') /*show title*/
Line 2,625 ⟶ 3,602:
zeros= 0 /*# of 0's found for Mertens function.*/
Xzero= 0 /*number of times that zero was crossed*/
$=; prev= /*$ holds output grid of GRP numbers. */
prev=
$= /*$ holds output grid of GRP numbers. */
do j=a to b; _= Mertens(j) /*process some numbers from LO ──► HI.*/
if _==0 then zeros= zeros + 1 /*Is Zero? Then bump the zeros counter*/
Line 2,632 ⟶ 3,608:
prev= _
if grid then $= $ right(_, 2) /*build grid if A & B are non─negative.*/
if words($)==grp then do; say substr($, 2); $= /*show grid if fully populated, */
end /* and nullify it for more #s. */
end /*j*/ /*for small grids, using wordCnt is OK.*/
 
Line 2,641 ⟶ 3,617:
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
Mertens: procedure expose @. !!. M.; parse arg n; /*obtainif aM.n\==. integer tothen bereturn tested for muM.*/n
if M.n\==.<1 then return M.n'∙'; m= 0 /*washandle computedspecial before?cases of Then return it.non─positive#*/
if n<1 then return '∙' /*handle special cases of non─positive#*/
m= 0 /*the sum of all the MU's (so far). */
do k=1 for n; m= m + mobius(k) /*sum the MU's up to N. */
end /*k*/ /* [↑] mobius function uses memoization*/
M.n= m; return m /*return the sum of all the MU's. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
mobius: procedure expose @. !!.; parse arg x 1 ox /*obtain aget integer to be tested for mu. */
if !!.x\==. then return !!.x /*X computed before? Return that value*/
if x<1 then return '∙'; mu= 0 /*handle special case of non-positive #*/
#= 0 do k=1; p= @.k; if p>x then leave /*start with(P) a mu > value of zero.X? Then we're done.*/
do k=1; if p=*p>x @.k then do; mu= mu+1; leave; end /* (P**2) > X? Bump # and /*get the Kth (pre─generated) prime.leave*/
if p>x//p==0 then leave do; mu= mu+1 /*primeX (P)divisible by P? > X? Bump mu Then we're donenumber. */
if p*p>x then do; #= #+1; leave /*prime (P**2 > X? Bump # and leave.*/
end
if x//p==0 then do; #= #+1 /*X divisible by P? Bump mu number. */
x= x % p /* Divide by prime. */
if x//p==0 then return 0 /*X÷by P? Then return zero*/
end
end /*k*/ /*# MU (below) is almost always small, <9*/
!!.ox= -1 ** #mu; return !!.ox /*raise -1 to the mu power, memoize it.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
genP: !@.1=2; @.2=3; @.3=5; M@.4=!7; @.5=11; hihi@.6= max(HI, eqZ, xZ) 13 /*initialize 2some arrayslow forprimes; memoization.# primes.*/
@!!.1=2.; @M.2=3; @!!.3=5; @.4 #=7 6; @ sq.5#=11; @.6=**2 13; nP=6 /*assign low primes; # primes " 2 arrays for memoization. */
do limj=nP@.#+4 untilby lim*lim>=hihi;2 endto max(HI, eqZ, xZ); parse var /*onlyj keep'' primes-1 up_ to the sqrt(HI)./*odd Ps from now on*/
if _==5 then iterate; if j//3==0 then iterate; if j//7==0 then iterate /*÷ 5 3 7*/
do j=@.nP+4 by 2 to hihi /*only find odd primes from here on. */
do k=37 while k*sq.k<=j /*divide by some generated odd primes. */
if j // @.k==0 then iterate j /*Is J divisible by P? Then not prime*/
end /*k*/ /* [↓] a prime (J) has been found. */
nP#= nP#+1; if nP<=HI then @.nP#=j; sq.j= j*j /*bump primeP count; assign primeP──►@.; to compute @. J**2*/
end /*j*/; return /*calculate the squares of some primes.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 2,695 ⟶ 3,666:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
def μ(n)
Line 2,713 ⟶ 3,684:
puts "\nThe Mertens function is zero #{ar.count(0)} times in the range (1..1000);"
puts "it crosses zero #{ar.each_cons(2).count{|m1, m2| m1 != 0 && m2 == 0}} times."
</syntaxhighlight>
</lang>
{{out}}
<pre> 1 0 -1 -1 -2 -1 -2 -2 -2 -1 -2 -2 -3 -2 -1 -1 -2 -2 -3
Line 2,729 ⟶ 3,700:
it crosses zero 59 times.
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program mertens;
m := [1] * 1000;
loop for n in [1..#m] do
m(n) -:= 0 +/[m(n div k) : k in [2..n]];
end loop;
 
print("The first 99 Mertens numbers:");
putchar(" ");
loop for n in [1..99] do
putchar(lpad(str m(n), 3));
if n mod 10=9 then print; end if;
end loop;
 
zero := #[n : n in [1..#m] | m(n) = 0];
cross := #[n : n in [1..#m] | m(n) = 0 and m(n-1) /= 0];
 
print("M(N) is zero " + str zero + " times.");
print("M(N) crosses zero " + str cross + " times.");
end program;</syntaxhighlight>
{{out}}
<pre>The first 99 Mertens numbers:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
M(N) is zero 92 times.
M(N) crosses zero 59 times.</pre>
 
=={{header|Sidef}}==
Built-in:
<langsyntaxhighlight lang="ruby">say mertens(123456789) #=> 1170
say mertens(1234567890) #=> 9163</langsyntaxhighlight>
 
Algorithm for computing M(n) in sublinear time:
 
<langsyntaxhighlight lang="ruby">func mertens(n) is cached {
 
var lookup_size = (2 * n.iroot(3)**2)
Line 2,771 ⟶ 3,777:
cache{n} = M
}(n)
}</langsyntaxhighlight>
 
Task:
<langsyntaxhighlight lang="ruby">with (200) {|n|
say "Mertens function in the range 1..#{n}:"
(1..n).map { mertens(_) }.slices(20).each {|line|
Line 2,785 ⟶ 3,791:
say (1..n->count_by { mertens(_)==0 }, " zeros")
say (1..n->count_by { mertens(_)==0 && mertens(_-1)!=0 }, " zero crossings")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,807 ⟶ 3,813:
=={{header|Swift}}==
{{trans|C}}
<langsyntaxhighlight lang="swift">import Foundation
 
func mertensNumbers(max: Int) -> [Int] {
Line 2,847 ⟶ 3,853:
}
print("M(n) is zero \(zero) times for 1 <= n <= \(max).")
print("M(n) crosses zero \(cross) times for 1 <= n <= \(max).")</langsyntaxhighlight>
 
{{out}}
Line 2,864 ⟶ 3,870:
M(n) is zero 92 times for 1 <= n <= 1000.
M(n) crosses zero 59 times for 1 <= n <= 1000.
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn mertens(t int) ([]int, int, int) {
mut to:=t
if to < 1 {
to = 1
}
mut merts := []int{len:to+1}
mut primes := [2]
mut sum := 0
mut zeros := 0
mut crosses := 0
for i := 1; i <= to; i++ {
mut j := i
mut cp := 0 // counts prime factors
mut spf := false // true if there is a square prime factor
for p in primes {
if p > j {
break
}
if j%p == 0 {
j /= p
cp++
}
if j%p == 0 {
spf = true
break
}
}
if cp == 0 && i > 2 {
cp = 1
primes << i
}
if !spf {
if cp%2 == 0 {
sum++
} else {
sum--
}
}
merts[i] = sum
if sum == 0 {
zeros++
if i > 1 && merts[i-1] != 0 {
crosses++
}
}
}
return merts, zeros, crosses
}
fn main() {
merts, zeros, crosses := mertens(1000)
println("Mertens sequence - First 199 terms:")
for i := 0; i < 200; i++ {
if i == 0 {
print(" ")
continue
}
if i%20 == 0 {
println('')
}
print(" ${merts[i]:2}")
}
println("\n\nEquals zero $zeros times between 1 and 1000")
println("\nCrosses zero $crosses times between 1 and 1000")
}</syntaxhighlight>
 
{{out}}
<pre>
Mertens sequence - First 199 terms:
1 0 -1 -1 -2 -1 -2 -2 -2 -1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2 -3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3 -3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1 -2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2 -2 -1 -1 0 1 2 2 1 1 1
1 0 -1 -2 -2 -3 -2 -3 -3 -4 -5 -4 -4 -5 -6 -5 -5 -5 -4 -3
-3 -3 -2 -1 -1 -1 -1 -2 -2 -1 -2 -3 -3 -2 -1 -1 -1 -2 -3 -4
-4 -3 -2 -1 -1 0 1 1 1 0 0 -1 -1 -1 -2 -1 -1 -2 -1 0
0 1 1 0 0 -1 0 -1 -1 -1 -2 -2 -2 -3 -4 -4 -4 -3 -2 -3
-3 -4 -5 -4 -4 -3 -4 -3 -3 -3 -4 -5 -5 -6 -5 -6 -6 -7 -7 -8
 
Equals zero 92 times between 1 and 1000
 
Crosses zero 59 times between 1 and 1000
</pre>
 
Line 2,869 ⟶ 3,962:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
 
var isSquareFree = Fn.new { |n|
Line 2,921 ⟶ 4,014:
prev = next
}
System.print("\nThe Mertens function crosses zero %(count) times in the range [1, 1000].")</langsyntaxhighlight>
 
{{out}}
Line 2,940 ⟶ 4,033:
 
The Mertens function crosses zero 59 times in the range [1, 1000].
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0">integer M ( 1+1000 );
integer K, Zero, Cross, N;
begin \compute values of the Mertens function
\Generate Mertens numbers
M( 1 ) := 1;
for N := 2 to 1000 do begin
M( N ) := 1;
for K := 2 to N do M( N ) := M( N ) - M( N / K )
end;
\Print table
Text(0, "The first 99 Mertens numbers are:^m^j");
Text(0, " " );
K := 9;
for N := 1 to 99 do begin
Format(3, 0);
RlOut(0, float(M(N)));
K := K - 1;
if K = 0 then begin
K := 10;
CrLf(0);
end
end;
\Calculate zeroes and crossings
Zero := 0;
Cross := 0;
for N :=2 to 1000 do begin
if M( N ) = 0 then begin
Zero := Zero + 1;
if M( N - 1 ) # 0 then Cross := Cross + 1
end
end;
Text(0, "M(N) is zero "); IntOut(0, Zero); Text(0, " times.^m^j" );
Text(0, "M(N) crosses zero "); IntOut(0, Cross); Text(0, " times.^m^j" );
end</syntaxhighlight>
{{out}}
<pre>
The first 99 Mertens numbers are:
1 0 -1 -1 -2 -1 -2 -2 -2
-1 -2 -2 -3 -2 -1 -1 -2 -2 -3
-3 -2 -1 -2 -2 -2 -1 -1 -1 -2
-3 -4 -4 -3 -2 -1 -1 -2 -1 0
0 -1 -2 -3 -3 -3 -2 -3 -3 -3
-3 -2 -2 -3 -3 -2 -2 -1 0 -1
-1 -2 -1 -1 -1 0 -1 -2 -2 -1
-2 -3 -3 -4 -3 -3 -3 -2 -3 -4
-4 -4 -3 -4 -4 -3 -2 -1 -1 -2
-2 -1 -1 0 1 2 2 1 1 1
M(N) is zero 92 times.
M(N) crosses zero 59 times.
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn mertensW(n){
[1..].tweak(fcn(n,pm){
pm.incN(mobius(n));
Line 2,967 ⟶ 4,113:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">mertensW().walk(199)
.pump(Console.println, T(Void.Read,19,False),
fcn{ vm.arglist.pump(String,"%3d".fmt) });
Line 2,976 ⟶ 4,122:
otm.reduce(fcn(s,m){ s + (m==0) },0) : println(_," zeros");
otm.reduce(fcn(p,m,rs){ rs.incN(m==0 and p!=0); m }.fp2( s:=Ref(0) ));
println(s.value," zero crossings");</langsyntaxhighlight>
{{out}}
<pre>
2,054

edits