Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
83
Task/Sisyphus-sequence/FreeBASIC/sisyphus-sequence.basic
Normal file
83
Task/Sisyphus-sequence/FreeBASIC/sisyphus-sequence.basic
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
'#include "isprime.bas"
|
||||
|
||||
Function getNthPrime(n As Integer) As Longint
|
||||
If n <= 0 Then Return 0
|
||||
|
||||
Dim As Integer cnt = 0
|
||||
Dim As Longint num = 1
|
||||
|
||||
While cnt < n
|
||||
num += 1
|
||||
If isPrime(num) Then cnt += 1
|
||||
Wend
|
||||
|
||||
Return num
|
||||
End Function
|
||||
|
||||
Function findMax(arr() As Integer) As Integer
|
||||
Dim As Integer maxVal = arr(0)
|
||||
For i As Integer = 1 To Ubound(arr)
|
||||
If arr(i) > maxVal Then maxVal = arr(i)
|
||||
Next
|
||||
Return maxVal
|
||||
End Function
|
||||
|
||||
' Main program
|
||||
Const As Longint limit = 1e6
|
||||
Dim As Double sisyphus(100)
|
||||
Dim As Integer under250(250)
|
||||
Dim As Integer i, m, np = 0
|
||||
Dim As Longint specific = 1000
|
||||
Dim As Longint cnt = 1
|
||||
Dim As Double nextVal = 1
|
||||
|
||||
sisyphus(0) = 1
|
||||
under250(1) = 1
|
||||
|
||||
Do
|
||||
If (nextVal Mod 2) = 0 Then
|
||||
nextVal /= 2
|
||||
Else
|
||||
np += 1
|
||||
nextVal += getNthPrime(np)
|
||||
End If
|
||||
|
||||
If nextVal <= 250 Then under250(nextVal) += 1
|
||||
|
||||
cnt += 1
|
||||
If cnt <= 100 Then
|
||||
sisyphus(cnt-1) = nextVal
|
||||
If cnt = 100 Then
|
||||
Print "The first 100 members of the Sisyphus sequence are:"
|
||||
For i = 0 To 99
|
||||
Print Using "####"; sisyphus(i);
|
||||
If (i + 1) Mod 10 = 0 Then Print
|
||||
Next
|
||||
Print
|
||||
End If
|
||||
Elseif cnt = specific Then
|
||||
Print Using "###,###,###,###"; cnt;
|
||||
Print "th member is: ";
|
||||
Print Using "###,###,###,###"; nextVal;
|
||||
Print " and highest prime needed: ";
|
||||
Print Using "###,###,###"; getNthPrime(np)
|
||||
|
||||
If cnt = limit Then
|
||||
m = findMax(under250())
|
||||
Print !"\nNumbers under 250 that do not occur in first "; cnt; " terms:"
|
||||
For i = 1 To 250
|
||||
If under250(i) = 0 Then Print i; " ";
|
||||
Next
|
||||
Print
|
||||
Print !"\nNumbers under 250 that occur the most in first "; cnt; " terms:"
|
||||
For i = 1 To 250
|
||||
If under250(i) = m Then Print i; " ";
|
||||
Next
|
||||
Print " all occur "; m; " times."
|
||||
Exit Do
|
||||
End If
|
||||
specific *= 10
|
||||
End If
|
||||
Loop
|
||||
|
||||
Sleep
|
||||
162
Task/Sisyphus-sequence/Zig/sisyphus-sequence.zig
Normal file
162
Task/Sisyphus-sequence/Zig/sisyphus-sequence.zig
Normal file
|
|
@ -0,0 +1,162 @@
|
|||
const primesieve = @cImport({
|
||||
@cInclude("primesieve.h");
|
||||
});
|
||||
const std = @import("std");
|
||||
const mem = std.mem;
|
||||
|
||||
pub fn main() !void {
|
||||
var t0 = try std.time.Timer.start();
|
||||
// ------------------------------------------------------- stdout
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
// ---------------------------------------------------- allocator
|
||||
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
// --------------------------------------------------------------
|
||||
var counter = try Counter.init(allocator);
|
||||
defer counter.deinit();
|
||||
|
||||
var sisyphus = try SisyphusSequenceGenerator.init();
|
||||
defer sisyphus.deinit();
|
||||
|
||||
var number: u64 = undefined;
|
||||
for (0..10) |_| {
|
||||
for (0..10) |_| {
|
||||
number = try sisyphus.next();
|
||||
try counter.add(number);
|
||||
try stdout.print(" {d:3}", .{number});
|
||||
}
|
||||
try stdout.writeByte('\n');
|
||||
}
|
||||
try stdout.writeByte('\n');
|
||||
|
||||
for ([_]usize{ 1_000, 10_000, 100_000, 1_000_000, 10_000_000, 100_000_000 }) |n| {
|
||||
while (counter.count != n) {
|
||||
number = try sisyphus.next();
|
||||
try counter.add(number);
|
||||
}
|
||||
try stdout.print(
|
||||
"{d:10}th member is {d:10} and highest prime needed is {d:9}\n",
|
||||
.{ n, number, sisyphus.prime },
|
||||
);
|
||||
}
|
||||
{
|
||||
try stdout.writeAll("\nThese numbers under 250 do not occur in the first 100,000,000 terms:\n");
|
||||
const missing = try counter.getMissing(allocator);
|
||||
defer allocator.free(missing);
|
||||
|
||||
var sep: []const u8 = "";
|
||||
for (missing) |n| {
|
||||
try stdout.print("{s}{d}", .{ sep, n });
|
||||
sep = ", ";
|
||||
}
|
||||
try stdout.writeByte('\n');
|
||||
}
|
||||
{
|
||||
try stdout.writeAll("\nThese numbers under 250 occur the most in the first 100,000,000 terms:\n");
|
||||
const most = try counter.getMost(allocator);
|
||||
defer allocator.free(most.numbers);
|
||||
|
||||
var sep: []const u8 = "";
|
||||
for (most.numbers) |n| {
|
||||
try stdout.print("{s}{d}", .{ sep, n });
|
||||
sep = ", ";
|
||||
}
|
||||
try stdout.print(" all occur {d} times.\n", .{most.max});
|
||||
}
|
||||
var count = counter.count; // Only need the count, not the found hashmap. Ditch counter.
|
||||
while (true) {
|
||||
number = try sisyphus.next();
|
||||
count += 1;
|
||||
if (number == 36) {
|
||||
try stdout.print(
|
||||
"\nMember {d} is {d} and highest prime needed is {d}\n",
|
||||
.{ count, number, sisyphus.prime },
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
try stdout.print("\nProcessed in {}\n", .{std.fmt.fmtDuration(t0.read())});
|
||||
}
|
||||
|
||||
const SisyphusSequenceGeneratorError = error{
|
||||
PrimeSieveError,
|
||||
};
|
||||
|
||||
const SisyphusSequenceGenerator = struct {
|
||||
it: primesieve.primesieve_iterator = undefined,
|
||||
next_: u64 = 1,
|
||||
prime: u64 = 0,
|
||||
|
||||
fn init() !SisyphusSequenceGenerator {
|
||||
var si = SisyphusSequenceGenerator{};
|
||||
primesieve.primesieve_init(&si.it);
|
||||
return si;
|
||||
}
|
||||
fn deinit(self: *SisyphusSequenceGenerator) void {
|
||||
primesieve.primesieve_free_iterator(&self.it);
|
||||
}
|
||||
fn next(self: *SisyphusSequenceGenerator) SisyphusSequenceGeneratorError!u64 {
|
||||
const n = self.next_;
|
||||
if (self.next_ % 2 == 0) {
|
||||
self.next_ /= 2;
|
||||
} else {
|
||||
self.prime = primesieve.primesieve_next_prime(&self.it);
|
||||
if (self.it.is_error != 0 or self.prime == primesieve.PRIMESIEVE_ERROR)
|
||||
return SisyphusSequenceGeneratorError.PrimeSieveError;
|
||||
self.next_ += self.prime;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
||||
const Counter = struct {
|
||||
found: std.AutoHashMap(u64, usize),
|
||||
count: usize = 0,
|
||||
|
||||
fn init(allocator: mem.Allocator) !Counter {
|
||||
var found = std.AutoHashMap(u64, usize).init(allocator);
|
||||
try found.ensureTotalCapacity(250);
|
||||
for (1..250) |n|
|
||||
try found.put(@as(u64, n), 0);
|
||||
return Counter{ .found = found };
|
||||
}
|
||||
fn deinit(self: *Counter) void {
|
||||
self.found.deinit();
|
||||
}
|
||||
fn add(self: *Counter, n: u64) !void {
|
||||
self.count += 1;
|
||||
if (n < 250)
|
||||
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);
|
||||
var it = self.found.iterator();
|
||||
while (it.next()) |entry| {
|
||||
if (entry.value_ptr.* == 0)
|
||||
try missing_array.append(entry.key_ptr.*);
|
||||
}
|
||||
const missing = try missing_array.toOwnedSlice();
|
||||
mem.sort(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 } {
|
||||
// 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 it = self.found.iterator();
|
||||
while (it.next()) |entry| {
|
||||
if (entry.value_ptr.* == max)
|
||||
try most_array.append(entry.key_ptr.*);
|
||||
}
|
||||
const most = try most_array.toOwnedSlice();
|
||||
mem.sort(u64, most, {}, std.sort.asc(u64));
|
||||
return .{ .numbers = most, .max = max };
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue