Möbius function: Difference between revisions
Content added Content deleted
(Möbius function in Applesoft BASIC and Chipmunk Basic) |
(RPL: add section) |
||
Line 1,966: | Line 1,966: | ||
0 -1 -1 1 0 1 -1 1 0 0 |
0 -1 -1 1 0 1 -1 1 0 0 |
||
-1 -1 0 -1 1 -1 0 -1 0 -1 |
-1 -1 0 -1 1 -1 0 -1 0 -1 |
||
</pre> |
|||
=={{header|RPL}}== |
|||
{{trans|FreeBASIC}} |
|||
RPL does not allow that a function returns something whilst in the middle of a branch, so we have to play here with index saturation and flag use to mimic the short returns that many other languages accept. |
|||
{{works with|Halcyon Calc|4.2.7}} |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ |
|||
'''IF''' DUP 1 ≠ '''THEN''' → n |
|||
≪ -1 |
|||
2 n √ '''FOR''' d |
|||
'''IF''' n d MOD NOT '''THEN''' |
|||
1 CF '''IF''' n d SQ MOD NOT '''THEN''' |
|||
n 'd' STO DROP 0 1 SF '''END''' |
|||
'''IF''' 1 FC? '''THEN''' |
|||
DROP n d / n 'd' STO '''MU''' NEG '''END''' |
|||
'''END''' |
|||
'''NEXT''' |
|||
≫ '''END''' |
|||
≫ ''''MU'''' STO |
|||
| |
|||
'''MU''' ''( n -- µ(n) )'' |
|||
if n = 1 then return 1 |
|||
// default return value put in stack |
|||
for d as uinteger = 2 to int(sqr(n)) |
|||
if n mod d = 0 then |
|||
if n mod (d*d) = 0 then |
|||
return 0 // and set flag |
|||
// if flag not set, |
|||
return -mobius(n/d) |
|||
end if |
|||
next d |
|||
|} |
|||
{{in}} |
|||
<pre> |
|||
≪ 1 100 FOR li "" li DUP 19 + FOR j "-0+" j MU 2 + DUP SUB + NEXT 20 STEP ≫ EVAL |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
5: "+--0-+-00+-0-++0-0-0" |
|||
4: "++-00+00---0+++0-++0" |
|||
3: "---00+-000+0-0+0++-0" |
|||
2: "-+00+--0+--0-+00+--0" |
|||
1: "0+-0+++0-0+0+++0-000" |
|||
</pre> |
</pre> |
||