Jump to content

9 billion names of God the integer: Difference between revisions

Add new solution in Zig
(Implementation for R)
(Add new solution in Zig)
Line 5,176:
 
nine_billion_names(20)</syntaxhighlight>
 
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
const print = std.debug.print;
const bigint = std.math.big.int.Managed;
const eql = std.mem.eql;
const Array = std.ArrayList;
const Array1 = Array(bigint);
const Array2 = Array(Array1);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
 
fn cumu(cache: *Array2, n: usize) !*Array1 {
var y = cache.items.len;
while (cache.items.len <= n) : (y += 1) {
var roww = Array1.init(allocator);
try roww.append(try bigint.init(allocator));
for (1..y + 1) |x| {
var cache_value = try cache.items[y - x].items[@min(x, y - x)].clone();
try cache_value.add(&cache_value, &roww.getLast());
try roww.append(cache_value);
}
try cache.append(roww);
}
return &(cache.items[n]);
}
 
fn row(cache: *Array2, n: usize) !void {
const e = try cumu(cache, n);
var v = try bigint.init(allocator);
for (0..n) |i| {
try v.sub(&e.items[i + 1], &e.items[i]);
print(" {} ", .{v});
}
v.deinit();
print("\n", .{});
}
 
pub fn main() !void {
var cache = Array2.init(allocator);
defer {
cache.deinit();
}
defer {
while (cache.popOrNull()) |l| {
l.deinit();
}
}
var cache0 = Array1.init(allocator);
var v = try bigint.init(allocator);
try v.set(1);
try cache0.append(v);
try cache.append(cache0);
 
print("rows:\n", .{});
for (1..1000) |x| {
try row(&cache, x);
}
 
print("\nsums:\n", .{});
for ([_]usize{ 23, 123, 999 }) |num| {
const r = try cumu(&cache, num);
print("{d: >4} {d}\n", .{ num, r.getLast() });
}
}
</syntaxhighlight>
{{out}}
<pre>
rows:
1
1 1
1 1 1
1 2 1 1
1 2 2 1 1
1 3 3 2 1 1
1 3 4 3 2 1 1
1 4 5 5 3 2 1 1
1 4 7 6 5 3 2 1 1
1 5 8 9 7 5 3 2 1 1
1 5 10 11 10 7 5 3 2 1 1
1 6 12 15 13 11 7 5 3 2 1 1
1 6 14 18 18 14 11 7 5 3 2 1 1
1 7 16 23 23 20 15 11 7 5 3 2 1 1
1 7 19 27 30 26 21 15 11 7 5 3 2 1 1
1 8 21 34 37 35 28 22 15 11 7 5 3 2 1 1
1 8 24 39 47 44 38 29 22 15 11 7 5 3 2 1 1
1 9 27 47 57 58 49 40 30 22 15 11 7 5 3 2 1 1
1 9 30 54 70 71 65 52 41 30 22 15 11 7 5 3 2 1 1
1 10 33 64 84 90 82 70 54 42 30 22 15 11 7 5 3 2 1 1
1 10 37 72 101 110 105 89 73 55 42 30 22 15 11 7 5 3 2 1 1
1 11 40 84 119 136 131 116 94 75 56 42 30 22 15 11 7 5 3 2 1 1
1 11 44 94 141 163 164 146 123 97 76 56 42 30 22 15 11 7 5 3 2 1 1
1 12 48 108 164 199 201 186 157 128 99 77 56 42 30 22 15 11 7 5 3 2 1 1
1 12 52 120 192 235 248 230 201 164 131 100 77 56 42 30 22 15 11 7 5 3 2 1 1
</pre>
 
=={{header|zkl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.