Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -5,18 +5,20 @@ const std = @import("std");
const mem = std.mem;
pub fn main() !void {
var t0 = try std.time.Timer.start();
var t0: std.time.Timer = try .start();
// ------------------------------------------------------- stdout
const stdout = std.io.getStdOut().writer();
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
// ---------------------------------------------------- allocator
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var arena: std.heap.ArenaAllocator = .init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
// --------------------------------------------------------------
var counter = try Counter.init(allocator);
var counter: Counter = try .init(allocator);
defer counter.deinit();
var sisyphus = try SisyphusSequenceGenerator.init();
var sisyphus: SisyphusSequenceGenerator = try .init();
defer sisyphus.deinit();
var number: u64 = undefined;
@ -40,6 +42,8 @@ pub fn main() !void {
.{ n, number, sisyphus.prime },
);
}
try stdout.flush();
{
try stdout.writeAll("\nThese numbers under 250 do not occur in the first 100,000,000 terms:\n");
const missing = try counter.getMissing(allocator);
@ -52,6 +56,8 @@ pub fn main() !void {
}
try stdout.writeByte('\n');
}
try stdout.flush();
{
try stdout.writeAll("\nThese numbers under 250 occur the most in the first 100,000,000 terms:\n");
const most = try counter.getMost(allocator);
@ -64,6 +70,8 @@ pub fn main() !void {
}
try stdout.print(" all occur {d} times.\n", .{most.max});
}
try stdout.flush();
var count = counter.count; // Only need the count, not the found hashmap. Ditch counter.
while (true) {
number = try sisyphus.next();
@ -76,7 +84,10 @@ pub fn main() !void {
break;
}
}
try stdout.print("\nProcessed in {}\n", .{std.fmt.fmtDuration(t0.read())});
try stdout.writeByte('\n');
try stdout.flush();
std.log.info("processed in {D}", .{t0.read()});
}
const SisyphusSequenceGeneratorError = error{
@ -89,7 +100,7 @@ const SisyphusSequenceGenerator = struct {
prime: u64 = 0,
fn init() !SisyphusSequenceGenerator {
var si = SisyphusSequenceGenerator{};
var si: SisyphusSequenceGenerator = .{};
primesieve.primesieve_init(&si.it);
return si;
}
@ -114,7 +125,7 @@ const Counter = struct {
found: std.AutoHashMap(u64, usize),
count: usize = 0,
fn init(allocator: mem.Allocator) !Counter {
fn init(allocator: std.mem.Allocator) !Counter {
var found = std.AutoHashMap(u64, usize).init(allocator);
try found.ensureTotalCapacity(250);
for (1..250) |n|
@ -130,33 +141,33 @@ const Counter = struct {
self.found.getEntry(n).?.value_ptr.* += 1;
}
/// Caller owns returned slice memory.
fn getMissing(self: *const Counter, allocator: mem.Allocator) ![]u64 {
var missing_array = std.ArrayList(u64).init(allocator);
fn getMissing(self: *const Counter, allocator: std.mem.Allocator) ![]u64 {
var missing_array: std.ArrayList(u64) = .empty;
var it = self.found.iterator();
while (it.next()) |entry| {
if (entry.value_ptr.* == 0)
try missing_array.append(entry.key_ptr.*);
try missing_array.append(allocator, entry.key_ptr.*);
}
const missing = try missing_array.toOwnedSlice();
mem.sort(u64, missing, {}, std.sort.asc(u64));
const missing = try missing_array.toOwnedSlice(allocator);
std.mem.sortUnstable(u64, missing, {}, std.sort.asc(u64));
return missing;
}
/// Caller owns returned 'numbers' slice memory.
fn getMost(self: *const Counter, allocator: mem.Allocator) !struct { numbers: []u64, max: usize } {
fn getMost(self: *const Counter, allocator: std.mem.Allocator) !struct { numbers: []u64, max: usize } {
// Find the maximum count (there may be more than one at this value)
var value_it = self.found.valueIterator();
var max: usize = 0;
while (value_it.next()) |count|
max = @max(max, count.*);
var most_array = std.ArrayList(u64).init(allocator);
var most_array: std.ArrayList(u64) = .empty;
var it = self.found.iterator();
while (it.next()) |entry| {
if (entry.value_ptr.* == max)
try most_array.append(entry.key_ptr.*);
try most_array.append(allocator, entry.key_ptr.*);
}
const most = try most_array.toOwnedSlice();
mem.sort(u64, most, {}, std.sort.asc(u64));
const most = try most_array.toOwnedSlice(allocator);
std.mem.sortUnstable(u64, most, {}, std.sort.asc(u64));
return .{ .numbers = most, .max = max };
}
};