Data update

This commit is contained in:
Ingy döt Net 2025-08-11 18:05:26 -07:00
parent 4d5544505c
commit 4924dd0264
3073 changed files with 55820 additions and 4408 deletions

View file

@ -0,0 +1,84 @@
using System;
namespace MobiusDemo
{
class Program
{
// -----------------------------------------------------------------
// Settings that are identical to the Java version
// -----------------------------------------------------------------
private const int MU_MAX = 1_000_000; // same upper bound
private static int[] MU = null; // will hold the sieve
static void Main(string[] args)
{
Console.WriteLine("First 199 terms of the möbius function are as follows:");
Console.Write(" ");
for (int n = 1; n < 200; n++)
{
Console.Write($"{MobiusFunction(n),2} ");
// linebreak after every 20 numbers exactly like the Java code
if ((n + 1) % 20 == 0)
Console.WriteLine();
}
}
// -----------------------------------------------------------------
// Compute μ(n) using the same sieve algorithm that the Java code
// uses. The first call builds the whole table up to MU_MAX.
// -----------------------------------------------------------------
private static int MobiusFunction(int n)
{
// If the sieve has already been built we can return the answer
// straight away.
if (MU != null)
return MU[n];
// -------------------------------------------------------------
// Build the sieve (once)
// -------------------------------------------------------------
MU = new int[MU_MAX + 1];
// initialise every entry with 1 Java did this explicitly
for (int i = 0; i <= MU_MAX; i++)
MU[i] = 1;
int sqrt = (int)Math.Sqrt(MU_MAX);
// first pass: multiply by -p for each prime factor p,
// and mark multiples of p² as zero.
for (int i = 2; i <= sqrt; i++)
{
if (MU[i] == 1) // i is still “prime”
{
// flip the sign for every multiple of i
for (int j = i; j <= MU_MAX; j += i)
MU[j] *= -i;
// any number that contains i² gets value 0
int i2 = i * i;
for (int j = i2; j <= MU_MAX; j += i2)
MU[j] = 0;
}
}
// second pass: reduce the encoded values to the final μ(n)
for (int i = 2; i <= MU_MAX; i++)
{
if (MU[i] == i) // only +i => μ = +1
MU[i] = 1;
else if (MU[i] == -i) // only -i => μ = -1
MU[i] = -1;
else if (MU[i] < 0) // product of an odd number of distinct primes
MU[i] = 1;
else if (MU[i] > 0) // product of an even number of distinct primes
MU[i] = -1;
// note: MU[i] == 0 stays 0 (square factor present)
}
return MU[n];
}
}
}

View file

@ -0,0 +1,39 @@
program moebius(output);
(* Moebius function *)
var
t, u: integer;
function moebius(n: integer): integer;
var
m, f: integer;
begin
m := 1;
if n <> 1 then
begin
f := 2;
repeat
if n mod (f * f) = 0 then
m := 0
else
begin
if n mod f = 0 then
begin
m := -m;
n := n div f
end;
f := f + 1
end
until (f > n) or (m = 0)
end;
moebius := m
end;
begin
for t := 0 to 9 do
begin
for u := 1 to 10 do
write(moebius(10 * t + u): 2, ' ');
writeln
end
end.

View file

@ -0,0 +1,35 @@
local int = require "int"
local fmt = require "fmt"
local function is_square_free(n)
local i = 2
local sq = i * i
while sq <= n do
if n % sq == 0 then return false end
i = (i > 2) ? i + 2 : i + 1
sq = i * i
end
return true
end
local function mu(n)
assert(n >= 1, "Argument must be a positive integer")
if n == 1 then return 1 end
local sq_free = is_square_free(n)
local factors = int.factors(n)
if sq_free and #factors % 2 == 0 then return 1 end
if sq_free then return -1 end
return 0
end
print("The first 199 Möbius numbers are:")
for i = 0, 9 do
for j = 0, 19 do
if i == 0 and j == 0 then
io.write(" ")
else
fmt.write("% 3d ", mu(i * 20 + j))
end
end
print()
end

View file

@ -0,0 +1,28 @@
# Moebius function
function Moebius([int]$N) {
[int]$m = 1
if ($N -ne 1) {
[int]$f = 2
do {
if ($N % ($f * $f) -eq 0) {
$m = 0
} else {
if ($N % $f -eq 0) {
$m = -$m;
$N = [math]::Floor($N / $f)
}
$f += 1
}
} while (($f -le $N) -and ($m -ne 0))
}
return $m
}
foreach ($t in 0..9) {
[string]$row = @()
foreach ($u in 1..10) {
$row += "{0,2} " -f $(Moebius(10 * $t + $u))
}
Write-Output $row
}

View file

@ -0,0 +1,28 @@
# Moebius function
Moebius <- function(n) {
if (n == 1)
return(1)
m <- 1
f <- 2
repeat {
if (n %% (f * f) == 0)
m <- 0
else {
if (n %% f == 0) {
m <- -m
n <- n %/% f
}
f <- f + 1
}
if (f > n || m == 0)
break
}
return(m)
}
mb <- matrix(0, 10, 10)
for (t in 0:9)
for (u in 1:10)
mb[t + 1, u] <- Moebius(10 * t + u)
mb

View file

@ -0,0 +1,42 @@
proc prime_factors {n} {
set d 1
set factors [list]
while {$n > 1 && $d < $n} {
incr d
set d_squared [expr {$d * $d}]
while {$n % $d == 0} {
lappend factors $d
set n [expr {$n / $d}]
}
}
return $factors
}
proc mobius {n} {
set p [prime_factors $n]
set unique_p [lsort -unique $p]
if {[llength $p] == [llength $unique_p]} {
if {[llength $p] % 2 == 0} {
return 1
} else {
return -1
}
} else {
return 0
}
}
set upto 199
set moebius_sequence [list]
for {set i 1} {$i <= $upto} {incr i} {
lappend moebius_sequence [mobius $i]
}
puts "Möbius sequence - First $upto terms:"
for {set i 0} {$i < $upto} {incr i} {
if {$i % 20 == 0 && $i != 0} {
puts ""
}
puts -nonewline [format "%4d" [lindex $moebius_sequence $i]]
}
puts ""

View file

@ -0,0 +1,84 @@
const std = @import("std");
const print = std.debug.print;
fn moebius(x_param: u64) i8 {
var x = x_param;
var prime_count: u32 = 0;
// Helper function to divide x by a factor and count it
// Returns true if we should return 0 (factor appears twice)
const divideXBy = struct {
fn call(x_ptr: *u64, factor: u64, prime_count_ptr: *u32) bool {
if (x_ptr.* % factor == 0) {
x_ptr.* /= factor;
prime_count_ptr.* += 1;
if (x_ptr.* % factor == 0) {
return true; // Return 0
}
}
return false;
}
}.call;
// Handle 2 and 3 separately
if (divideXBy(&x, 2, &prime_count)) return 0;
if (divideXBy(&x, 3, &prime_count)) return 0;
// Use a wheel sieve to check the remaining factors <= x
var i: u64 = 5;
const sqrt_x = isqrt(x);
while (i <= sqrt_x) : (i += 6) {
if (divideXBy(&x, i, &prime_count)) return 0;
if (divideXBy(&x, i + 2, &prime_count)) return 0;
}
// There can exist one prime factor larger than x,
// in that case we can check if x is still larger than one, and then count it.
if (x > 1) {
prime_count += 1;
}
if (prime_count % 2 == 0) {
return 1;
} else {
return -1;
}
}
/// Returns the largest integer smaller than or equal to `n`
fn isqrt(n: u64) u64 {
if (n <= 1) {
return n;
} else {
var x0: u64 = std.math.pow(u64, 2, @as(u32, @intFromFloat(@floor(@log2(@as(f64, @floatFromInt(n))) / 2.0))) + 1);
var x1: u64 = (x0 + n / x0) / 2;
while (x1 < x0) {
x0 = x1;
x1 = (x0 + n / x0) / 2;
}
return x0;
}
}
pub fn main() void {
const ROWS: u64 = 10;
const COLS: u64 = 20;
print("Values of the Möbius function, μ(x), for x between 0 and {}:\n", .{COLS * ROWS});
for (0..ROWS) |i| {
for (0..COLS + 1) |j| {
const x = COLS * i + j;
const mu = moebius(x);
if (mu >= 0) {
// Print an extra space if there's no minus sign in front of the output
// in order to align the numbers in a nice grid.
print(" ", .{});
}
print("{} ", .{mu});
}
print("\n" , .{});
}
const x = std.math.maxInt(u64);
print("\nμ({}) = {}\n", .{ x, moebius(x) });
}