Mertens function: Difference between revisions

m
(Mertens function in various BASIC dialents (BASIC256, Run BASIC, True BASIC, XBasic and Yabasic))
 
(10 intermediate revisions by 9 users not shown)
Line 77:
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>
 
Line 1,631 ⟶ 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#}}==
Line 1,908 ⟶ 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>
 
Line 2,970 ⟶ 3,194:
 
=={{header|Phix}}==
Based on the stackexchange link, short and sweet but not very fast: 1.4s just for the first 1000...
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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,995 ⟶ 3,228:
<!--</syntaxhighlight>-->
{{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
Line 3,216 ⟶ 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 3,411 ⟶ 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}}==
Line 3,548 ⟶ 3,872:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn mertens(t int) ([]int, int, int) {
mut to:=t
if to < 1 {
Line 3,638 ⟶ 3,962:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
 
var isSquareFree = Fn.new { |n|
Line 3,709 ⟶ 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>
 
2,054

edits