9 billion names of God the integer: Difference between revisions
Content added Content deleted
(Add new solution in Zig) |
|||
Line 5,271: | Line 5,271: | ||
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 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 |
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> |
</pre> |
||