Möbius function: Difference between revisions

Added various BASIC dialects (Gambas, MSX Basic, PureBasic and XBasic)
m (Correct fact in rust code comment)
(Added various BASIC dialects (Gambas, MSX Basic, PureBasic and XBasic))
 
(20 intermediate revisions by 7 users not shown)
Line 120:
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|Amazing Hopper}}==
{{Trans|Python}}
<syntaxhighlight lang="c">
#include <basico.h>
 
#proto cálculodeMobius(_X_)
#synon _cálculodeMobius calcularMobius
 
algoritmo
imprimir ("Mobius numbers from 1..199\n")
i=0, s=1
iterar grupo( ++i, #(i<=199), calcular Mobius (i), \
solo si (#( iszero(s%20) ), NL;s=0 ), imprimir, ++s )
saltar
terminar
 
subrutinas
 
cálculo de Mobius (n)
si( #(n==0) ) ; tomar '" "'
sino si( #(n==1) ); tomar '" 1"'
sino; p=0
iterar para (i=1, #(i<=n+1), ++i)
si ( #( iszero(n%i) && isprime(i)) )
cuando ( #( iszero(n%(i*i)) ) ){
tomar '" 0"'; ir a (herejía) /* ¡! */
} ++p
fin si
siguiente
tomar si ( es impar(p), " -1", " 1" )
fin si
 
/* ¡Dios! ¡Purifica esta mierda! ----+ */
/* | */
herejía: /* <----------------------+ */
retornar
</syntaxhighlight>
{{out}}
<pre>
Mobius numbers from 1..199
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>
 
Line 325 ⟶ 378:
end</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|Chipmunk Basic}}===
Line 455 ⟶ 506:
0 1 -1 0 1 1 1 0 -1 0 1 0 1 1 1 0 -1 0 0 0
</pre>
 
==={{header|Gambas}}===
{{trans|FreeBASIC}}
{{works with|Windows}}
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim outstr As String = " . "
 
For i As Integer = 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
End
 
Function mobius(n As Integer) As Integer
 
If n = 1 Then Return 1
For d As Integer = 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
Return -1
 
End Function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|GW-BASIC}}===
Line 522 ⟶ 606:
230 RETURN
</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">Procedure.i mobius(n)
If n = 1:
ProcedureReturn 1
EndIf
For d = 2 To Int(Sqr(n))
If Mod(n, d) = 0:
If Mod(n, d * d) = 0:
ProcedureReturn 0
EndIf
ProcedureReturn -mobius(n / d)
EndIf
Next d
ProcedureReturn -1
EndProcedure
 
OpenConsole()
outstr$ = " . "
For i = 1 To 200
If mobius(i) >= 0:
outstr$ = outstr$ + " "
EndIf
outstr$ = outstr$ + Str(mobius(i)) + " "
If Mod(i, 10) = 9:
PrintN(outstr$)
outstr$ = ""
EndIf
Next i
 
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Tiny BASIC}}===
Line 545 ⟶ 668:
101 PRINT "1"
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">PROGRAM "Möbius function"
VERSION "0.0000"
IMPORT "xma"
 
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION mobius (n)
 
FUNCTION Entry ()
outstr$ = " . "
FOR i = 1 TO 200
IF mobius(i) >= 0 THEN outstr$ = outstr$
outstr$ = outstr$ + STR$(mobius(i)) + " "
IF i MOD 10 = 9 THEN
PRINT outstr$
outstr$ = ""
END IF
NEXT i
END FUNCTION
 
FUNCTION mobius (n)
IF n = 1 THEN RETURN 1
FOR d = 2 TO INT(SQRT(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
END PROGRAM</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Yabasic}}===
Line 910 ⟶ 1,070:
0 -1 -1 1 0 1 -1 1 0 0 -1 -1 0 -1 1 -1 0 -1 0 -1
</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight lang=easylang>
mu_max = 100000
sqroot = floor sqrt mu_max
#
for i to mu_max
mu[] &= 1
.
for i = 2 to sqroot
if mu[i] = 1
for j = i step i to mu_max
mu[j] *= -i
.
for j = i * i step i * i to mu_max
mu[j] = 0
.
.
.
for i = 2 to mu_max
if mu[i] = i
mu[i] = 1
elif mu[i] = -i
mu[i] = -1
elif mu[i] < 0
mu[i] = 1
elif mu[i] > 0
mu[i] = -1
.
.
numfmt 0 3
for i = 1 to 100
write mu[i]
if i mod 10 = 0
print ""
.
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,152 ⟶ 1,351:
 
=={{header|J}}==
Implementation:
<syntaxhighlight lang="j">mu=: */@:-@~:@q:</syntaxhighlight>
 
Explanation: <code>q: n</code> gives the list of prime factors of n. (This is an empty list for the number 1, is <code>2 2 5 5</code> for the number 100, and is <code>2 2 2 3 5</code> for the number 120.)
Implementation:
 
In this context <code>~:</code> replaces each prime factor either by 1, if it is its first occurrence, or by 0, if it is a repetition (e.g. <code>2 2 5 5</code> → <code>1 0 1 0</code>). Then, <code>-</code> simply negates this list (e.g. <code>1 0 1 0</code> → <code>_1 0 _1 0</code>), and finally <code>*/</code> multiplies all list elements to get the desired result.
<syntaxhighlight lang="j">mu=: ({{*/1-y>1}} * _1 ^ 2|+/)@q:~&_</syntaxhighlight>
 
Explanation: _ q: n gives the list of exponents of prime factors of n. (This is an empty list for the number 1, is 2 0 2 for the number 100 and is 3 1 1 for the number 120.)
 
In this context +/ is the sum of that list, 2 | +/ is 1 if the sum is odd and 0 if the sum is even. _1^0 is 1 and _1^1 is _1. And, {{*/1-y>1}} returns zero if any exponent is at least 2 in magnitude.
 
Task example:
<syntaxhighlight lang="j"> mu >: i. 10 20
 
<syntaxhighlight lang="j"> mu 1+i.10 20
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
Line 1,654 ⟶ 1,850:
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 0</pre>
 
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
{
for(i = 1, 99,
print1(moebius(i) " ");
if(i % 10 == 0, print("\n"););
);
}
</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
</pre>
 
 
=={{header|Pascal}}==
Line 2,125 ⟶ 2,356:
'''NEXT'''
≫ '''END'''
≫ ''''<span style="color:blue">MU'''</span>' STO
|
'''<span style="color:blue">MU'''</span> ''( n -- µ(n) )''
if n = 1 then return 1
// default return value put in stack
Line 2,141 ⟶ 2,372:
|}
 
{{in}}
≪ 1 100 '''FOR''' li "" li DUP 19 + '''FOR''' j "-0+" j <span style="color:blue">MU</span> 2 + DUP SUB + '''NEXT''' 20 '''STEP''' ≫ EVAL
<pre>
 
≪ 1 100 FOR li "" li DUP 19 + FOR j "-0+" j MU 2 + DUP SUB + NEXT 20 STEP ≫ EVAL
</pre>
{{out}}
<pre>
Line 2,153 ⟶ 2,383:
1: "0+-0+++0-0+0+++0-000"
</pre>
{{works with|HP|49/50}}
Improvement of an implementation found on the [https://www.hpmuseum.org/forum/thread-10330-post-93200.html#pid93200 MoHPC forum]
« '''CASE'''
DUP 1 ≤ '''THEN END'''
FACTOR DUP TYPE 9 ≠ '''THEN''' DROP -1 '''END'''
DUP →STR "^" POS '''THEN''' DROP 0 '''END'''
SIZE 1 + 2 / 1 SWAP 2 MOD { NEG } IFT
'''END'''
» '<span style="color:blue">MU</span>' STO
« 1 100 '''FOR''' j
j 20 MOD 1 == "" IFT
"-0+" j <span style="color:blue">MU</span> 2 + DUP SUB +
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
Same output.
 
=={{header|Ruby}}==
Line 2,222 ⟶ 2,468:
}
 
/// ComputesReturns the largest integer squaresmaller rootthan ofor equal to `n√n` through a binary search.
///
/// This is the integer `i` such that `i^2 <= n < (i + 1)^2`.
const fn isqrt(n: u64) -> u64 {
//if Specialn case<= to1 avoid overflow{
if n == u64::MAX {n
} else {
return 4_294_967_296;
let mut x0 = u64::pow(2, n.ilog2() / 2 + 1);
}
let mut x1 = (x0 + n / x0) / 2;
 
let mut left = 0;while x1 < x0 {
let mut right = n + 1 x0 = x1;
x1 = (x0 + n / x0) / 2;
 
while left != right - 1 {
let mid = left + (right - left) / 2;
if mid as u128 * mid as u128 <= n as u128 {
left = mid;
} else {
right = mid;
}
x0
}
 
left
}
 
Line 2,286 ⟶ 2,523:
μ(18446744073709551615) = -1
</pre>
 
=={{header|Sidef}}==
Built-in:
Line 2,323 ⟶ 2,561:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
 
var isSquareFree = Fn.new { |n|
Line 2,351 ⟶ 2,589:
System.write(" ")
} else {
SystemFmt.write("%(Fmt.dm(3$ 3d ", mu.call(i*20 + j))) ")
}
}
2,127

edits