9 billion names of God the integer: Difference between revisions

Content deleted Content added
Peak (talk | contribs)
 
(4 intermediate revisions by 3 users not shown)
Line 3,853:
1234: 156978797223733228787865722354959930
12345: 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736</pre>
 
=={{header|R}}==
<syntaxhighlight lang="racket">
library(partitions)
library(stringi)
 
get_row <- function(x) unname(table(parts(x)[1,]))
 
center_string <- function(s,pad_len=80) stri_pad_both(s,(pad_len - length(s))," ")
for (i in 1:25) cat(center_string(stri_c(get_row(i),collapse = " "),80),"\n")
 
cat("The sum of G(25) is:", sum(get_row(25)),"\n")
 
</syntaxhighlight>
{{out}}
<pre>
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
 
The sum of G(25) is: 1958
 
</pre>
 
=={{header|Racket}}==
Line 5,037 ⟶ 5,083:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
var cache = [[BigInt.one]]
Line 5,130 ⟶ 5,176:
 
nine_billion_names(20)</syntaxhighlight>
 
 
=={{header|Zig}}==
===Standard triangle solution===
<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>
===Here is faster version===
<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);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
 
fn calc ( n:usize, p:*Array1) !void {
for ( 1..n+1 ) |k| {
var d:i64 = @intCast(n);
d -= @intCast(k*(3*k - 1)/2);
inline for ( 0..2 ) |_| {
if ( d < 0 ) return;
if (k&1>0) try p.items[n].add ( &p.items[n], &p.items[@intCast(d)] )
else try p.items[n].sub ( &p.items[n], &p.items[@intCast(d)] );
d -= @intCast(k);
}
}
}
 
fn main() !void {
const s = [_]usize{ 23, 123, 1234, 12345, 123456 };
var p = Array1.init ( allocator );
try p.append ( try bigint.initSet ( allocator, 1 ) );
var i:usize=1;
for ( s )|m|{
while (i<=m):(i+=1){
try p.append( try bigint.init(allocator) );
try calc( i, &p );
}
print("P({d}) = {d}\n", .{m,p.items[m]} );
}
}
</syntaxhighlight>
{{out}}
<pre>
P(23) = 1255
P(123) = 2552338241
P(1234) = 156978797223733228787865722354959930
P(12345) = 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736
P(123456) = 30817659578536496678545317146533980855296613274507139217608776782063054452191537379312358383342446230621170608408020911309259407611257151683372221925128388387168451943800027128045369650890220060901494540459081545445020808726917371699102825508039173543836338081612528477859613355349851184591540231790254269948278726548570660145691076819912972162262902150886818986555127204165221706149989
</pre>
 
=={{header|zkl}}==