Möbius function: Difference between revisions

Dialects of BASIC moved to the BASIC section.
m (syntax highlighting fixup automation)
(Dialects of BASIC moved to the BASIC section.)
Line 225:
</pre>
 
=={{header|BASIC}}==
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">function mobius(n)
Line 254 ⟶ 255:
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">function mobius( n as uinteger ) as integer
if n = 1 then return 1
for d as uinteger = 2 to int(sqr(n))
if n mod d = 0 then
if n mod (d*d) = 0 then return 0
return -mobius(n/d)
end if
next d
return -1
end function
 
dim as string outstr = " . "
for i as uinteger = 1 to 200
if mobius(i)>=0 then outstr += " "
outstr += str(mobius(i))+" "
if i mod 10 = 9 then
print outstr
outstr = ""
end if
next i</syntaxhighlight>
{{out}}
<pre>
. 1 -1 -1 0 -1 1 -1 0 0
1 -1 0 -1 1 1 0 -1 0 -1
0 1 1 -1 0 0 1 0 0 -1
-1 -1 0 1 1 1 0 -1 1 1
0 -1 -1 -1 0 0 1 -1 0 0
0 1 0 -1 0 1 0 1 1 -1
0 -1 1 0 0 1 -1 -1 0 1
-1 -1 0 -1 1 0 0 1 -1 -1
0 0 1 -1 0 1 1 1 0 -1
0 1 0 1 1 1 0 -1 0 0
0 -1 -1 -1 0 -1 1 -1 0 -1
-1 1 0 -1 -1 1 0 0 1 1
0 0 1 1 0 0 0 -1 0 1
-1 -1 0 1 1 0 0 -1 -1 -1
0 1 1 1 0 1 1 0 0 -1
0 -1 0 0 -1 1 0 -1 1 1
0 1 0 -1 0 -1 1 -1 0 0
-1 0 0 -1 -1 0 0 1 1 -1
0 -1 -1 1 0 1 -1 1 0 0
-1 -1 0 -1 1 -1 0 -1 0 -1</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">local fn IsPrime( n as long ) as BOOL
BOOL result = YES
long i
if ( n < 2 ) then result = NO : exit fn
for i = 2 to n + 1
if ( i * i <= n ) and ( n mod i == 0 )
result = NO : exit fn
end if
next
end fn = result
 
local fn Mobius( n as long ) as long
long i, p = 0, result = 0
if ( n == 1 ) then result = 1 : exit fn
for i = 1 to n + 1
if ( n mod i == 0 ) and ( fn IsPrime( i ) == YES )
if ( n mod ( i * i ) == 0 )
result = 0 : exit fn
else
p++
end if
end if
next
if( p mod 2 != 0 )
result = -1
else
result = 1
end if
end fn = result
 
window 1, @"Möbius function", (0,0,600,300)
 
printf @"First 100 terms of Mobius sequence:"
 
long i
for i = 1 to 100
printf @"%2ld\t", fn Mobius(i)
if ( i mod 20 == 0 ) then print
next
 
HandleEvents</syntaxhighlight>
{{output}}
<pre>
First 100 terms of Mobius sequence:
1 -1 -1 0 -1 1 -1 0 0 1 -1 0 -1 1 1 0 -1 0 -1 0
1 1 -1 0 0 1 0 0 -1 -1 -1 0 1 1 1 0 -1 1 1 0
-1 -1 -1 0 0 1 -1 0 0 0 1 0 -1 0 1 0 1 1 -1 0
-1 1 0 0 1 -1 -1 0 1 -1 -1 0 -1 1 0 0 1 -1 -1 0
0 1 -1 0 1 1 1 0 -1 0 1 0 1 1 1 0 -1 0 0 0
</pre>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
<syntaxhighlight lang="gwbasic">10 FOR T = 0 TO 9
20 FOR U = 1 TO 10
30 N = 10*T + U
40 GOSUB 100
50 PRINT USING "## ";M;
60 NEXT U
70 PRINT
80 NEXT T
90 END
100 IF N = 1 THEN M = 1 : RETURN
110 M = 1 : F = 2
120 IF N MOD (F*F) = 0 THEN M = 0 : RETURN
130 IF N MOD F = 0 THEN GOSUB 170
140 F = F + 1
150 IF F <= N THEN GOTO 120
160 RETURN
170 M = -M
180 N = N/F
190 RETURN</syntaxhighlight>
{{out}}
<pre>
1 -1 -1 0 -1 1 -1 0 0 1
-1 0 -1 1 1 0 -1 0 -1 0
1 1 -1 0 0 1 0 0 -1 -1
-1 0 1 1 1 0 -1 1 1 0
-1 -1 -1 0 0 1 -1 0 0 0
1 0 -1 0 1 0 1 1 -1 0
-1 1 0 0 1 -1 -1 0 1 -1
-1 0 -1 1 0 0 1 -1 -1 0
0 1 -1 0 1 1 1 0 -1 0
1 0 1 1 1 0 -1 0 0 0
</pre>
 
==={{header|Tiny BASIC}}===
Tiny BASIC is not suited for printing tables, so this is limited to prompting for a single number and calculating its Mobius number.
<syntaxhighlight lang="tinybasic"> PRINT "Enter an integer"
INPUT N
IF N < 0 THEN LET N = -N
IF N < 2 THEN GOTO 100 + N
LET C = 1
LET F = 2
10 IF ((N/F)/F)*F*F = N THEN GOTO 100
IF (N/F)*F = N THEN GOTO 30
20 LET F = F + 1
IF F<=N THEN GOTO 10
GOTO 100 + C
30 LET N = N / F
LET C = -C
GOTO 20
99 PRINT "-1"
END
100 PRINT "0"
END
101 PRINT "1"
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">outstr$ = " . "
for i = 1 to 200
if mobius(i) >= 0 then outstr$ = outstr$ + " " : fi
outstr$ = outstr$ + str$(mobius(i)) + " "
if mod(i, 10) = 9 then
print outstr$
outstr$ = ""
end if
next i
end
 
sub mobius(n)
if n = 1 then return 1 : fi
for d = 2 to int(sqr(n))
if mod(n, d) = 0 then
if mod(n, (d*d)) = 0 then return 0 : fi
return -mobius(n/d)
end if
next d
return -1
end sub</syntaxhighlight>
 
=={{header|C}}==
Line 571 ⟶ 751:
end program moebius
</syntaxhighlight>
 
{{out}}
<pre>
Line 585 ⟶ 764:
0 1 0 -1 0 -1 1 -1 0 0 -1 0 0 -1 -1 0 0 1 1 -1
0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function mobius( n as uinteger ) as integer
if n = 1 then return 1
for d as uinteger = 2 to int(sqr(n))
if n mod d = 0 then
if n mod (d*d) = 0 then return 0
return -mobius(n/d)
end if
next d
return -1
end function
 
dim as string outstr = " . "
for i as uinteger = 1 to 200
if mobius(i)>=0 then outstr += " "
outstr += str(mobius(i))+" "
if i mod 10 = 9 then
print outstr
outstr = ""
end if
next i</syntaxhighlight>
{{out}}
<pre>
. 1 -1 -1 0 -1 1 -1 0 0
1 -1 0 -1 1 1 0 -1 0 -1
0 1 1 -1 0 0 1 0 0 -1
-1 -1 0 1 1 1 0 -1 1 1
0 -1 -1 -1 0 0 1 -1 0 0
0 1 0 -1 0 1 0 1 1 -1
0 -1 1 0 0 1 -1 -1 0 1
-1 -1 0 -1 1 0 0 1 -1 -1
0 0 1 -1 0 1 1 1 0 -1
0 1 0 1 1 1 0 -1 0 0
0 -1 -1 -1 0 -1 1 -1 0 -1
-1 1 0 -1 -1 1 0 0 1 1
0 0 1 1 0 0 0 -1 0 1
-1 -1 0 1 1 0 0 -1 -1 -1
0 1 1 1 0 1 1 0 0 -1
0 -1 0 0 -1 1 0 -1 1 1
0 1 0 -1 0 -1 1 -1 0 0
-1 0 0 -1 -1 0 0 1 1 -1
0 -1 -1 1 0 1 -1 1 0 0
-1 -1 0 -1 1 -1 0 -1 0 -1</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">local fn IsPrime( n as long ) as BOOL
BOOL result = YES
long i
if ( n < 2 ) then result = NO : exit fn
for i = 2 to n + 1
if ( i * i <= n ) and ( n mod i == 0 )
result = NO : exit fn
end if
next
end fn = result
 
local fn Mobius( n as long ) as long
long i, p = 0, result = 0
if ( n == 1 ) then result = 1 : exit fn
for i = 1 to n + 1
if ( n mod i == 0 ) and ( fn IsPrime( i ) == YES )
if ( n mod ( i * i ) == 0 )
result = 0 : exit fn
else
p++
end if
end if
next
if( p mod 2 != 0 )
result = -1
else
result = 1
end if
end fn = result
 
window 1, @"Möbius function", (0,0,600,300)
 
printf @"First 100 terms of Mobius sequence:"
 
long i
for i = 1 to 100
printf @"%2ld\t", fn Mobius(i)
if ( i mod 20 == 0 ) then print
next
 
HandleEvents</syntaxhighlight>
{{output}}
<pre>
First 100 terms of Mobius sequence:
1 -1 -1 0 -1 1 -1 0 0 1 -1 0 -1 1 1 0 -1 0 -1 0
1 1 -1 0 0 1 0 0 -1 -1 -1 0 1 1 1 0 -1 1 1 0
-1 -1 -1 0 0 1 -1 0 0 0 1 0 -1 0 1 0 1 1 -1 0
-1 1 0 0 1 -1 -1 0 1 -1 -1 0 -1 1 0 0 1 -1 -1 0
0 1 -1 0 1 1 1 0 -1 0 1 0 1 1 1 0 -1 0 0 0
</pre>
 
Line 761 ⟶ 838:
0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1
</pre>
 
=={{header|GW-BASIC}}==
<syntaxhighlight lang="gwbasic">10 FOR T = 0 TO 9
20 FOR U = 1 TO 10
30 N = 10*T + U
40 GOSUB 100
50 PRINT USING "## ";M;
60 NEXT U
70 PRINT
80 NEXT T
90 END
100 IF N = 1 THEN M = 1 : RETURN
110 M = 1 : F = 2
120 IF N MOD (F*F) = 0 THEN M = 0 : RETURN
130 IF N MOD F = 0 THEN GOSUB 170
140 F = F + 1
150 IF F <= N THEN GOTO 120
160 RETURN
170 M = -M
180 N = N/F
190 RETURN</syntaxhighlight>
 
=={{header|Haskell}}==
Line 1,790 ⟶ 1,846:
0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1
</pre>
 
=={{header|Tiny BASIC}}==
Tiny BASIC is not suited for printing tables, so this is limited to prompting for a single number and calculating its Mobius number.
 
<syntaxhighlight lang="tinybasic"> PRINT "Enter an integer"
INPUT N
IF N < 0 THEN LET N = -N
IF N < 2 THEN GOTO 100 + N
LET C = 1
LET F = 2
10 IF ((N/F)/F)*F*F = N THEN GOTO 100
IF (N/F)*F = N THEN GOTO 30
20 LET F = F + 1
IF F<=N THEN GOTO 10
GOTO 100 + C
30 LET N = N / F
LET C = -C
GOTO 20
99 PRINT "-1"
END
100 PRINT "0"
END
101 PRINT "1"
END</syntaxhighlight>
 
=={{header|Wren}}==
Line 1,905 ⟶ 1,937:
0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">outstr$ = " . "
for i = 1 to 200
if mobius(i) >= 0 then outstr$ = outstr$ + " " : fi
outstr$ = outstr$ + str$(mobius(i)) + " "
if mod(i, 10) = 9 then
print outstr$
outstr$ = ""
end if
next i
end
 
sub mobius(n)
if n = 1 then return 1 : fi
for d = 2 to int(sqr(n))
if mod(n, d) = 0 then
if mod(n, (d*d)) = 0 then return 0 : fi
return -mobius(n/d)
end if
next d
return -1
end sub</syntaxhighlight>
 
=={{header|zkl}}==
512

edits