Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,25 @@
# 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]
# For example, the first mobius number could be evaluated by 1|mobius_array[-1].
def mobius_array:
. as $n
| ($n|sqrt) as $sqrt
| reduce range(2; 1 + $sqrt) as $i ([range(0; $n + 1) | 1];
if .[$i] == 1
then # for each factor found, swap + and -
reduce range($i; $n + 1; $i) as $j (.; .[$j] *= -$i)
| ($i*$i) as $isq # square factor = 0
| reduce range($isq; $n + 1; $isq) as $j (.; .[$j] = 0 )
else .
end )
| reduce range(2; 1 + $n) as $i (.;
if .[$i] == $i then .[$i] = 1
elif .[$i] == -$i then .[$i] = -1
elif .[$i] < 0 then .[$i] = 1
elif .[$i] > 0 then .[$i] = -1
else .[$i] = 0 # avoid "-0"
end);
# For one-off computations:
def mu($n): $n | mobius_array[-1];

View file

@ -0,0 +1,13 @@
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
def task:
def pp: if . >=0 then " \(.)" else tostring end;
(199 | mobius_array) as $mu
| "The first 199 Möbius numbers are:",
([" ", (range(1; 200) | $mu[.] | pp )]
| nwise(20)
| join(" ") ) ;
task

View file

@ -0,0 +1,54 @@
# relatively_prime(previous) tests whether the input integer is prime
# relative to the primes in the array "previous":
def relatively_prime(previous):
. as $in
| (previous|length) as $plen
# state: [found, ix]
| [false, 0]
| until( .[0] or .[1] >= $plen;
[ ($in % previous[.[1]]) == 0, .[1] + 1] )
| .[0] | not ;
# Emit a stream in increasing order of all primes (from 2 onwards)
# that are less than or equal to mx:
def primes(mx):
# The helper function, next, has arity 0 for tail recursion optimization;
# it expects its input to be the array of previously found primes:
def next:
. as $previous
| ($previous | .[length-1]) as $last
| if ($last >= mx) then empty
else ((2 + $last)
| until( relatively_prime($previous) ; . + 2)) as $nextp
| if $nextp <= mx
then $nextp, (( $previous + [$nextp] ) | next)
else empty
end
end;
if mx <= 1 then empty
elif mx == 2 then 2
else (2, 3, ([2,3] | next))
end ;
# Return an array of the distinct prime factors of . in increasing order
def prime_factors:
# Return an array of prime factors of . given that "primes"
# is an array of relevant primes:
def pf($primes):
if . <= 1 then []
else . as $in
| if ($in | relatively_prime($primes)) then [$in]
else reduce $primes[] as $p
([];
if ($in % $p) != 0 then .
else . + [$p] + (($in / $p) | pf($primes))
end)
end
| unique
end;
if . <= 1 then []
else . as $in
| pf( [ primes( (1+$in) | sqrt | floor) ] )
end;

View file

@ -0,0 +1,21 @@
def isSquareFree:
. as $n
| 2
| until ( (. * . > $n) or . == 0;
if ($n % (.*.) == 0) then 0 # i.e. stop
elif . > 2 then . + 2
else . + 1
end )
| . != 0;
def mu:
. as $n
| if . < 1 then "Argument to mu must be a positive integer" | error
elif . == 1 then 1
else if isSquareFree
then if ((prime_factors|length) % 2 == 0) then 1
else -1
end
else 0
end
end;

View file

@ -0,0 +1,12 @@
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
def task:
def pp: if . >=0 then " \(.)" else tostring end;
"The first 199 Möbius numbers are:",
([" ", (range(1; 200) | mu | pp )]
| nwise(20)
| join(" ") ) ;
task