Möbius function: Difference between revisions

m
syntax highlighting fixup automation
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 32:
=={{header|ALGOL 68}}==
{{Trans|C}}
<langsyntaxhighlight lang="algol68">BEGIN
# show the first 199 values of the moebius function #
INT sq root = 1 000;
Line 58:
IF ( i + 1 ) MOD 20 = 0 THEN print( ( newline ) ) FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 76:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">mobius: function [n][
if n=0 -> return ""
if n=1 -> return 1
Line 87:
 
loop split.every:20 map 0..199 => mobius 'a ->
print map a => [pad to :string & 3]</langsyntaxhighlight>
 
{{out}}
Line 103:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">loop 100
result .= SubStr(" " Möbius(A_Index), -1) . (Mod(A_Index, 10) ? " " : "`n")
MsgBox, 262144, , % result
Line 148:
ans.push(Format("{:d}", n))
return ans
}</langsyntaxhighlight>
{{out}}
<pre> 1 -1 -1 0 -1 1 -1 0 0 1
Line 162:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f MOBIUS_FUNCTION.AWK
# converted from Java
Line 209:
return(MU[n])
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 228:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight BASIC256lang="basic256">function mobius(n)
if n = 1 then return 1
for d = 2 to int(sqr(n))
Line 248:
end if
next i
end</langsyntaxhighlight>
{{out}}
<pre>
Line 257:
=={{header|C}}==
{{trans|Java}}
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Line 311:
free(mu);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>First 199 terms of the m÷bius function are as follows:
Line 327:
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 381:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>First 199 terms of the m÷bius function are as follows:
Line 397:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.math;
import std.stdio;
 
Line 452:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>First 199 terms of the m├╢bius function are as follows:
Line 468:
=={{header|F_Sharp|F#}}==
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]]
<langsyntaxhighlight lang="fsharp">
// Möbius function. Nigel Galloway: January 31st., 2021
let fN g=let n=primes32()
Line 478:
let mobius=seq{yield 1; yield! Seq.initInfinite((+)2>>fN)}
mobius|>Seq.take 500|>Seq.chunkBySize 25|>Seq.iter(fun n->Array.iter(printf "%3d") n;printfn "")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 506:
The <code>mobius</code> word exists in the <code>math.extras</code> vocabulary. See the implementation [https://docs.factorcode.org/content/word-mobius%2Cmath.extras.html here].
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io math.extras math.ranges sequences ;
 
"First 199 terms of the Möbius sequence:" print
199 [1,b] [ mobius ] map " " prefix 20 group
[ [ "%3s" printf ] each nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 528:
=={{header|Fortran}}==
{{Trans|C}}
<langsyntaxhighlight lang="fortran">
program moebius
use iso_fortran_env, only: output_unit
Line 570:
end do
end program moebius
</syntaxhighlight>
</lang>
 
{{out}}
Line 588:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function mobius( n as uinteger ) as integer
if n = 1 then return 1
for d as uinteger = 2 to int(sqr(n))
Line 607:
outstr = ""
end if
next i</langsyntaxhighlight>
{{out}}
<pre>
Line 633:
 
=={{header|FutureBasic}}==
<langsyntaxhighlight lang="futurebasic">local fn IsPrime( n as long ) as BOOL
BOOL result = YES
long i
Line 678:
next
 
HandleEvents</langsyntaxhighlight>
{{output}}
<pre>
Line 690:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 745:
fmt.Printf(" % d", mobs[i])
}
}</langsyntaxhighlight>
 
{{out}}
Line 763:
 
=={{header|GW-BASIC}}==
<langsyntaxhighlight lang="gwbasic">10 FOR T = 0 TO 9
20 FOR U = 1 TO 10
30 N = 10*T + U
Line 781:
170 M = -M
180 N = N/F
190 RETURN</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate)
import Data.List.Split (chunksOf)
import Data.Vector.Unboxed (toList)
Line 812:
putStrLn ("μ(n) for 1 ≤ n ≤ " ++ show n ++ ":\n") >>
putStrLn (showMoebiusBlock cols $ moebiusBlock n)
_ -> hPutStrLn stderr $ "Usage: " ++ prog ++ " num-columns maximum-number"</langsyntaxhighlight>
 
{{out}}
Line 834:
Implementation:
 
<langsyntaxhighlight Jlang="j">mu=: ({{*/1-y>1}} * _1 ^ 2|+/)@q:~&_</langsyntaxhighlight>
 
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.)
Line 842:
Task example:
 
<langsyntaxhighlight Jlang="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 852:
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 0</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class MöbiusFunction {
 
Line 915:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 939:
====Using a Sieve====
'''Adapted from [[#C|C]]'''
<langsyntaxhighlight lang="jq"># Input: a non-negative integer, $n
# Output: an array of size $n + 1 such that the nth-mobius number is .[$n]
# i.e. $n|mobius_array[-1]
Line 963:
 
# For one-off computations:
def mu($n): $n | mobius_array[-1];</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="jq">def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
Line 977:
| join(" ") ) ;
 
task</langsyntaxhighlight>
{{out}}
<pre>
Line 995:
Note that the following solution to the task at hand (computing a range of Mobius numbers is inefficient as it does not cache the primes array.
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># relatively_prime(previous) tests whether the input integer is prime
# relative to the primes in the array "previous":
def relatively_prime(previous):
Line 1,048:
else . as $in
| pf( [ primes( (1+$in) | sqrt | floor) ] )
end;</langsyntaxhighlight>
'''Mu'''
<langsyntaxhighlight lang="jq">def isSquareFree:
. as $n
| 2
Line 1,070:
else 0
end
end;</langsyntaxhighlight>
'''The Task'''
<langsyntaxhighlight lang="jq">def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
Line 1,083:
| join(" ") ) ;
 
task</langsyntaxhighlight>
{{out}}
As above.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
# modified from reinermartin's PR at https://github.com/JuliaMath/Primes.jl/pull/70/files
Line 1,102:
print(lpad(μ(n), 3), n % 20 == 19 ? "\n" : "")
end
</langsyntaxhighlight>{{out}}
<pre>
First 199 terms of the Möbius sequence:
Line 1,119:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import kotlin.math.sqrt
 
fun main() {
Line 1,176:
}
return MU!![n]
}</langsyntaxhighlight>
{{out}}
<pre>First 199 terms of the möbius function are as follows:
Line 1,192:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function buildArray(size, value)
local tbl = {}
for i=1, size do
Line 1,236:
print()
end
end</langsyntaxhighlight>
{{out}}
<pre>First 199 terms of the mobius function are as follows:
Line 1,251:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">Grid[Partition[MoebiusMu[Range[99]], UpTo[10]]]</langsyntaxhighlight>
{{out}}
<pre>1 -1 -1 0 -1 1 -1 0 0 1
Line 1,267:
Uses the prime decomposition method from https://rosettacode.org/wiki/Prime_decomposition#Nim
 
<langsyntaxhighlight Nimlang="nim">import std/[math, sequtils, strformat]
 
func getStep(n: int): int {.inline.} =
Line 1,320:
if i mod 20 == 0:
echo "" # print newline
</syntaxhighlight>
</lang>
{{out}}
<pre>The first 199 möbius numbers are:
Line 1,338:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use utf8;
use strict;
use warnings;
Line 1,361:
 
say "Möbius sequence - First $upto terms:\n" .
(' 'x4 . sprintf "@{['%4d' x $upto]}", @möebius) =~ s/((.){80})/$1\n/gr;</langsyntaxhighlight>
{{out}}
<pre>Möbius sequence - First 199 terms:
Line 1,376:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">Moebius</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,390:
<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;">199</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;">Moebius</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>
<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;">20</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,415:
 
<langsyntaxhighlight lang="python">
# Python Program to evaluate
# Mobius def M(N) = 1 if N = 1
Line 1,483:
if i % 20 == 0: print()
# This code is contributed by
# Manish Shaw(manishshaw1)</langsyntaxhighlight>
{{out}}
<pre>Mobius numbers from 1..99:
Line 1,495:
The idea is based on efficient program to print all prime factors of a given number. The interesting thing is, we do not need inner while loop here because if a number divides more than once, we can immediately return 0.
<langsyntaxhighlight Pythonlang="python"># Python Program to evaluate
# Mobius def M(N) = 1 if N = 1
# M(N) = 0 if any prime factor
Line 1,558:
print(f"{mobius(i):>4}", end = '')
# This code is contributed by
# Manish Shaw(manishshaw1)</langsyntaxhighlight>
{{out}}
<pre>Mobius numbers from 1..99:
Line 1,573:
Möbius number is not defined for n == 0. Raku arrays are indexed from 0 so store a blank value at position zero to keep n and μ(n) aligned.
 
<syntaxhighlight lang="raku" perl6line>use Prime::Factor;
 
sub μ (Int \n) {
Line 1,585:
# The Task
put "Möbius sequence - First 199 terms:\n",
@möbius[^200]».fmt('%3s').batch(20).join: "\n";</langsyntaxhighlight>
{{out}}
<pre>Möbius sequence - First 199 terms:
Line 1,610:
 
The function to computer some prime numbers is a bit of an overkill, but the goal was to keep it general &nbsp;(in case of larger/higher ranges for a Möbius sequence).
<langsyntaxhighlight lang="rexx">/*REXX pgm computes & shows a value grid of the Möbius function for a range of integers.*/
parse arg LO HI grp . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 0 /*Not specified? Then use the default.*/
Line 1,655:
end /*k*/ /* [↓] a prime (J) has been found. */
nP= nP+1; if nP<=HI then @.nP= j /*bump prime count; assign prime to @.*/
end /*j*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 1,675:
=={{header|Ring}}==
{{Trans|FreeBASIC}}
<langsyntaxhighlight lang="ring">
mobStr = " . "
 
Line 1,706:
next
return -1
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,732:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'prime'
 
def μ(n)
Line 1,742:
([" "] + (1..199).map{|n|"%2s" % μ(n)}).each_slice(20){|line| puts line.join(" ") }
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,760:
Built-in:
 
<langsyntaxhighlight lang="ruby">say moebius(53) #=> -1
say moebius(54) #=> 0
say moebius(55) #=> 1</langsyntaxhighlight>
 
Simple implementation:
<langsyntaxhighlight lang="ruby">func μ(n) {
var f = n.factor_exp.map { .tail }
f.any { _ > 1 } ? 0 : ((-1)**f.sum)
Line 1,775:
say line.map { '%2s' % _ }.join(' ')
})
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,794:
Tiny BASIC is not suited for printing tables, so this is limited to prompting for a single number and calculating its Mobius number.
 
<langsyntaxhighlight lang="tinybasic"> PRINT "Enter an integer"
INPUT N
IF N < 0 THEN LET N = -N
Line 1,813:
END
101 PRINT "1"
END</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/math" for Int
 
Line 1,850:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,868:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func Mobius(N);
int N, Cnt, F, K;
[Cnt:= 0;
Line 1,890:
if rem(N/20) = 19 then CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 1,908:
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="yabasic">outstr$ = " . "
for i = 1 to 200
if mobius(i) >= 0 then outstr$ = outstr$ + " " : fi
Line 1,928:
next d
return -1
end sub</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn mobius(n){
pf:=primeFactors(n);
sq:=pf.filter1('wrap(f){ (n % (f*f))==0 }); // False if square free
Line 1,949:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">[1..199].apply(mobius)
.pump(Console.println, T(Void.Read,19,False),
fcn{ vm.arglist.pump(String,"%3d".fmt) });</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits