Möbius function: Difference between revisions
Content added Content deleted
(Added Easylang) |
(→J: simplify) |
||
Line 1,245: | Line 1,245: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Implementation: |
Implementation: |
||
<syntaxhighlight lang="j">mu=: |
<syntaxhighlight lang="j">mu=: */@:-@~:@q:</syntaxhighlight> |
||
Explanation: <code> |
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.) |
||
In this context <code> |
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. |
||
Task example: |
Task example: |
||
<syntaxhighlight lang="j"> mu |
<syntaxhighlight lang="j"> mu >: 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 _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 0 0 _1 _1 _1 0 1 1 1 0 _1 1 1 0 |