9 billion names of God the integer: Difference between revisions

Implementation for R
(add task to arm assembly raspberry pi)
(Implementation for R)
 
(41 intermediate revisions by 19 users not shown)
Line 1:
{{task}}
 
This task is a variation of the [[wp:The Nine Billion Names of God#Plot_summary|short story by Arthur C. Clarke]].
Line 37 ⟶ 38:
 
If your environment is able, plot &nbsp; <math>P(n)</math> &nbsp; against &nbsp; <math>n</math> &nbsp; for &nbsp; <math>n=1\ldots 999</math>.
 
;Related tasks
* [[Partition function P]]
<br><br>
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V cache = [[BigInt(1)]]
F cumu(n)
L(l) :cache.len .. n
V r = [BigInt(0)]
L(x) 1 .. l
r.append(r.last + :cache[l - x][min(x, l - x)])
:cache.append(r)
R :cache[n]
 
F row(n)
V r = cumu(n)
R (0 .< n).map(i -> @r[i + 1] - @r[i])
 
print(‘rows:’)
L(x) 1..10
print(‘#2:’.format(x)‘ ’row(x))
 
print("\nsums:")
 
V pp = [BigInt(1)]
 
F partitions(n)
:pp.append(BigInt(0))
 
L(k) 1 .. n
V d = n - k * (3 * k - 1) I/ 2
I d < 0
L.break
 
I k [&] 1 != 0
:pp[n] += :pp[d]
E
:pp[n] -= :pp[d]
 
d -= k
I d < 0
L.break
 
I k [&] 1 != 0
:pp[n] += :pp[d]
E
:pp[n] -= :pp[d]
 
R :pp.last
 
V ns = Set([23, 123, 1234, 12345])
V max_ns = max(ns)
 
L(i) 1 .. max_ns
I i > max_ns
L.break
V p = partitions(i)
I i C ns
print(‘#6: #.’.format(i, p))</syntaxhighlight>
 
{{out}}
<pre>
rows:
1: [1]
2: [1, 1]
3: [1, 1, 1]
4: [1, 2, 1, 1]
5: [1, 2, 2, 1, 1]
6: [1, 3, 3, 2, 1, 1]
7: [1, 3, 4, 3, 2, 1, 1]
8: [1, 4, 5, 5, 3, 2, 1, 1]
9: [1, 4, 7, 6, 5, 3, 2, 1, 1]
10: [1, 5, 8, 9, 7, 5, 3, 2, 1, 1]
 
sums:
23: 1255
123: 2552338241
1234: 156978797223733228787865722354959930
12345: 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736
</pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program integerName64.s */
 
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ MAXI, 524
 
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "Total : @ pour @ \n"
szMessError: .asciz "Number too large !!.\n"
szCarriageReturn: .asciz "\n"
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
tbNames: .skip 8 * MAXI
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
mov x0,#5
bl functionG
mov x0,#23
bl functionG
 
mov x0,#123
bl functionG
mov x0,#524
bl functionG
mov x0,#1234
bl functionG
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrtbNames: .quad tbNames
qAdrsZoneConv: .quad sZoneConv
/******************************************************************/
/* compute function G */
/******************************************************************/
/* x0 contains N */
functionG:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
stp x4,x5,[sp,-16]! // save registres
cmp x0,#MAXI + 1
bge 2f
mov x3,x0
mov x2,#1
1: // loop compute every item
mov x0,x2
bl computeNumber
add x2,x2,#1
cmp x2,x3
ble 1b
ldr x1,qAdrsZoneConv // result display
bl conversion10 // call decimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
mov x4,x0
mov x0,x3
ldr x1,qAdrsZoneConv // result display
bl conversion10 // call decimal conversion
mov x0,x4
ldr x1,qAdrsZoneConv // insert conversion in message
bl strInsertAtCharInc
bl affichageMess
mov x0,#0
b 100f
2:
ldr x0,qAdrszMessError
bl affichageMess
mov x0,#-1
100:
ldp x4,x5,[sp],16 // restaur des 2 registres
ldp x2,x3,[sp],16 // restaur des 2 registres
ldp x1,lr,[sp],16 // restaur des 2 registres
ret
qAdrszMessError: .quad szMessError
/******************************************************************/
/* random door test strategy */
/******************************************************************/
/* x0 contains N */
computeNumber:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
stp x4,x5,[sp,-16]! // save registres
stp x6,x7,[sp,-16]! // save registres
ldr x6,qAdrtbNames // table address
mov x1,#1
str x1,[x6] // init item 0
mov x1,#0
str x1,[x6,x0,lsl #3] // init item N
mov x2,#1 // indice
1:
add x3,x2,x2, lsl #1
sub x4,x3,#1
mul x4,x2,x4
lsr x4,x4,#1
subs x3,x0,x4 // compute new indice
blt 90f
tst x2,#1 // indice owen ?
beq 2f
ldr x4,[x6,x3,lsl #3]
ldr x5,[x6,x0,lsl #3]
add x5,x5,x4 // addition
str x5,[x6,x0,lsl #3]
b 3f
2: // else substrac
ldr x4,[x6,x3,lsl #3]
ldr x5,[x6,x0,lsl #3]
sub x5,x5,x4
str x5,[x6,x0,lsl #3]
3:
subs x3,x3,x2 // compute new indice
blt 90f
tst x2,#1 // owen ?
beq 4f
ldr x4,[x6,x3,lsl #3]
ldr x5,[x6,x0,lsl #3]
add x5,x5,x4
str x5,[x6,x0,lsl #3]
b 5f
4:
ldr x4,[x6,x3,lsl #3]
ldr x5,[x6,x0,lsl #3]
sub x5,x5,x4
str x5,[x6,x0,lsl #3]
5:
add x2,x2,#1
cmp x2,x0
ble 1b
90:
ldr x0,[x6,x0,lsl #3] // return last item of table
100:
ldp x6,x7,[sp],16 // restaur des 2 registres
ldp x4,x5,[sp],16 // restaur des 2 registres
ldp x2,x3,[sp],16 // restaur des 2 registres
ldp x1,lr,[sp],16 // restaur des 2 registres
ret
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{out}}
<pre>
Total : 7 pour 5
Total : 1255 pour 23
Total : 2552338241 pour 123
Total : 18324532310194337942 pour 524
Number too large !!.
</pre>
 
=={{header|Ada}}==
Line 43 ⟶ 303:
{{libheader|Ada.Numerics.Big_Numbers.Big_Integers}}
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Numerics.Big_Numbers.Big_Integers;
 
Line 184 ⟶ 444:
Triangle.Print;
Row_Summer.Put_Sums;
end Names_Of_God;</langsyntaxhighlight>
 
{{out}}
Line 242 ⟶ 502:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program integerName.s */
Line 393 ⟶ 653:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 403 ⟶ 663:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SetBatchLines -1
 
InputBox, Enter_value, Enter the no. of lines sought
Line 460 ⟶ 720:
}
 
~Esc::ExitApp</langsyntaxhighlight>
{{out}}
If user inputs 25, the result shall be:
Line 495 ⟶ 755:
 
If we forgo the rows and only want to calculate <math>P(n)</math>, using the recurrence relation <math>P_n = \sum_{k=1}^n (-1)^{k+1} \Big(P_{n-k(3k-1)/2} + P_{n-k(3k+1)/2}\Big)</math> is a better way. This requires <math>O(n^2)</math> storage for caching instead the <math>O(n^3)</math>-ish for storing all the rows.
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <gmp.h>
 
Line 534 ⟶ 794:
at++;
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 549 ⟶ 809:
(this requires a System.Numerics registry reference)
 
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 646 ⟶ 906:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 1
Line 687 ⟶ 947:
===The Code===
see [[http://rosettacode.org/wiki/Talk:9_billion_names_of_God_the_integer#The_Green_Triangle The Green Triangle]].
<langsyntaxhighlight lang="cpp">
// Calculate hypotenuse n of OTT assuming only nothingness, unity, and hyp[n-1] if n>1
// Nigel Galloway, May 6th., 2013
Line 696 ⟶ 956:
void G_hyp(const int n){for(int i=0;i<N-2*n-1;i++) n==1?hyp[n-1+i]=1+G(i+n+1,n+1):hyp[n-1+i]+=G(i+n+1,n+1);}
}
</syntaxhighlight>
</lang>
 
===The Alpha and Omega, Beauty===
Before displaying the triangle the following code displays hyp as it is transformed by consequtive calls of G_hyp.
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <iomanip>
Line 711 ⟶ 971:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 735 ⟶ 995:
===The One True Triangle, OTT===
The following will display OTT(25).
<langsyntaxhighlight lang="cpp">
int main(){
N = 25;
Line 760 ⟶ 1,020:
std::cout << "1 1" << std::endl;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 792 ⟶ 1,052:
===Values of Integer Partition Function===
Values of the Integer Partition function may be extracted as follows:
<langsyntaxhighlight lang="cpp">
#include <iostream>
int main(){
Line 804 ⟶ 1,064:
std::cout << "G(123456) = " << r << std::endl;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 815 ⟶ 1,075:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn nine-billion-names [row column]
(cond (<= row 0) 0
(<= column 0) 0
Line 833 ⟶ 1,093:
(print-row x)))
 
(print-triangle 25)</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun 9-billion-names (row column)
(cond ((<= row 0) 0)
((<= column 0) 0)
Line 851 ⟶ 1,111:
 
(9-billion-names-triangle 25)
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
{{trans|Ruby}}
===Naive Solution===
<langsyntaxhighlight lang="ruby">def g(n, g)
return 1 unless 1 < g && g < n-1
(2..g).reduce(1){ |res, q| res + (q > n-g ? 0 : g(n-g, q)) }
end
(1..25).each { |n| puts (1..n).map { |g| "%4s" % g(n, g) }.join }</langsyntaxhighlight>
{{out}}
<pre>
Line 894 ⟶ 1,154:
===Producing rows===
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.bigint, std.algorithm, std.range;
 
auto cumu(in uint n) {
Line 920 ⟶ 1,180:
foreach (x; [23, 123, 1234])
writeln(x, " ", x.cumu.back);
}</langsyntaxhighlight>
{{out}}
<pre>Rows:
Line 942 ⟶ 1,202:
===Only partition functions===
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.bigint, std.algorithm;
 
struct Names {
Line 992 ⟶ 1,252:
writefln("%6d: %s", i, p);
}
}</langsyntaxhighlight>
{{out}}
<pre> 23: 1255
Line 1,015 ⟶ 1,275:
{{trans|Python}}
 
<langsyntaxhighlight Dartlang="dart">import 'dart:math';
 
List<BigInt> partitions(int n) {
Line 1,048 ⟶ 1,308:
print('$i: ${partitions(i)[i]}');
}
}</langsyntaxhighlight>
 
In main:
<langsyntaxhighlight Dartlang="dart"> import 'package:DD1_NamesOfGod/DD1_NamesOfGod.dart' as names_of_god;
 
main(List<String> arguments) {
names_of_god.printRows(min: 1, max: 11);
names_of_god.printSums([23, 123, 1234, 12345]);
}</langsyntaxhighlight>
 
{{out}}
Line 1,079 ⟶ 1,339:
=={{header|Dyalect}}==
 
<langsyntaxhighlight Dyalectlang="dyalect">var cache = [[1]]
 
func namesOfGod(n) {
for l in cache.lenLength()..n {
var r = [0]
if l == 1 {
r.addAdd(r[r.lenLength() - 1] + cache[0][0])
} else {
for x in 1..l {
r.addAdd(r[r.lenLength() - 1] + cache[l - x][min(x, l-x)])
}
}
cache.addAdd(r)
}
return cache[n]
}
 
func row(n) {
constlet r = namesOfGod(n)
var returnArrayxs = []
for i in 0..(<n - 1) {
returnArrayxs.addAdd(r[i + 1] - r[i])
}
return returnArrayxs
}
 
for x in 1..25 {
print("\(x): \(row(x))")
}</langsyntaxhighlight>
 
Output:
Line 1,140 ⟶ 1,400:
{{trans|Ruby}}
Naive Solution
<langsyntaxhighlight lang="elixir">defmodule God do
def g(n,g) when g == 1 or n < g, do: 1
def g(n,g) do
Line 1,151 ⟶ 1,411:
Enum.each(1..25, fn n ->
IO.puts Enum.map(1..n, fn g -> "#{God.g(n,g)} " end)
end)</langsyntaxhighlight>
 
{{out}}
Line 1,185 ⟶ 1,445:
 
Step 1: Print the pyramid for a smallish number of names. The P function is implement as described on [http://mathworld.wolfram.com/PartitionFunctionP.html partition function], (see 59 on that page). This is slow for N > 100, but works fine for the example: 10.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module(triangle).
-export([start/1]).
Line 1,207 ⟶ 1,467:
formula(A1,B1)->
formula(A1-1,B1-1)+formula(A1-B1,B1).
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,241 ⟶ 1,501:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: combinators io kernel math math.ranges memoize prettyprint
sequences ;
 
Line 1,261 ⟶ 1,521:
 
25 .triangle nl
"Sums:" print { 23 123 1234 12345 } [ dup pprint bl G . ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,342 ⟶ 1,602:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' version 03-11-2016
' compile with: fbc -s console
 
Line 1,379 ⟶ 1,639:
' ------=< MAIN >=------
 
Dim As ULong n, k, max = 25 ' with max > 479416 the numbers become
Dim As ULongInt p(max, max) ' to big for a 64bit unsigned integer
 
Line 1,430 ⟶ 1,690:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> 1
Line 1,469 ⟶ 1,729:
This demonstrates using a class that memoizes results to improve efficiency and reduce later calculation. It verifies its results against Frink's built-in and much more memory-and-space-efficient partitionCount function which uses Euler's pentagonal method for counting partitions.
 
<langsyntaxhighlight lang="frink">
class PartitionCount
{
Line 1,524 ⟶ 1,784:
testRow[1234]
testRow[12345]
</syntaxhighlight>
</lang>
 
<pre>
Line 1,559 ⟶ 1,819:
</pre>
 
== {{header|GAP}} ==
The partition function is built-in.
<langsyntaxhighlight lang="gap">PrintArray(List([1 .. 25], n -> List([1 .. n], k -> NrPartitions(n, k))));
 
[ [ 1 ],
Line 1,593 ⟶ 1,853:
 
[ 1255, 2552338241, 156978797223733228787865722354959930,
69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736 ]</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,646 ⟶ 1,906:
fmt.Printf("%d %v\n", num, r[len(r)-1].Text(10))
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,669 ⟶ 1,929:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">
def partitions(c)
{
Line 1,711 ⟶ 1,971:
{partitions(i);}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,744 ⟶ 2,004:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (mapAccumL)
 
cumu :: [[Integer]]
Line 1,764 ⟶ 2,024:
main = do
mapM_ print $ take 10 rows
mapM_ (print.sums) [23, 123, 1234, 12345]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,788 ⟶ 2,048:
{{trans|Python}}
 
<langsyntaxhighlight lang="unicon">procedure main(A)
n := integer(!A) | 10
every r := 2 to (n+1) do write(right(r-1,2),": ",showList(row(r)))
Line 1,812 ⟶ 2,072:
every (s := "[") ||:= (!A||", ")
return s[1:-2]||"]"
end</langsyntaxhighlight>
 
{{out}} (terminated without waiting for output of cumu(12345)):
Line 1,837 ⟶ 2,097:
=={{header|J}}==
Recursive calculation of a row element:
<langsyntaxhighlight lang="j">T=: 0:`1:`(($:&<:+ - $: ])`0:@.(0=]))@.(1+*@-) M. "0</langsyntaxhighlight>
Calculation of the triangle:
<langsyntaxhighlight lang="j">rows=: <@(#~0<])@({: T ])\@i.</langsyntaxhighlight>
'''Show triangle''':
<langsyntaxhighlight lang="j"> ({.~1+1 i:~ '1'=])"1 ":> }.rows 1+10
1
1 1
Line 1,851 ⟶ 2,111:
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</langsyntaxhighlight>
 
Note that we've gone to extra work, here, in this '''show triangle''' example, to keep columns aligned when we have multi-digit values. But then we limited the result to one digit values because that is prettier.
 
Calculate row sums:
<langsyntaxhighlight lang="j">rowSums=: 3 :0"0
z=. (y+1){. 1x
for_ks. <\1+i.y do.
Line 1,866 ⟶ 2,126:
z=. a n}z
end.
)</langsyntaxhighlight>
{{out}}
<pre> ({ [: rowSums >./) 3 23 123 1234
Line 1,874 ⟶ 2,134:
Translation of [[9_billion_names_of_God_the_integer#Python|Python]] via [[9_billion_names_of_God_the_integer#D|D]]
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.math.BigInteger;
import java.util.*;
import static java.util.Arrays.asList;
Line 1,914 ⟶ 2,174:
}
}
}</langsyntaxhighlight>
 
<pre>Rows:
Line 1,934 ⟶ 2,194:
 
=={{header|JavaScript}}==
===Solution 1===
{{trans|Python}}
<syntaxhighlight lang="javascript">
<lang JavaScript>
(function () {
var cache = [
Line 1,988 ⟶ 2,249:
}
 
// PL clearly this was not tested:
console.log("Sums")[23, 123, 1234, 12345].foreach(function (a) {
// console.log("Sums")[23, 123, 1234, 12345].foreach(function (a) {
console.log("Sums");
[23, 123, 1234, 12345].forEach(function (a) {
var s = cumu(a);
console.log(a, s[s.length - 1]);
});
})()
</syntaxhighlight>
</lang>
 
===Solution 2===
Clean and straightforward solution
<syntaxhighlight lang="javascript">
function genTriangle(n){ // O(n^3) time and O(n^2) space
var a = new Array(n)
for (let i = 0; i < n; i++){
a[i] = new Array(i+1)
for (let j = 0; j < i; j++){
a[i][j] = 0
let s = i-j-1, k = Math.min(s, j)
while (k >= 0) a[i][j] += a[s][k--]
}
a[i][i] = 1
}
return a.map(x => x.join(" ")).join("\n")
}
 
function G(n){ // At least O(n^2) time and O(n) space
var a = new Array(n+1)
a[0] = 1n
for (let i = 1; i <= n; i++){
a[i] = 0n
for (let k = 1, s = 1; s <= i;){
a[i] += (k & 1 ? a[i-s]:-a[i-s])
k > 0 ? (s += k, k = -k):(k = -k+1, s = k*(3*k-1)/2)
}
}
return a[n]
}
 
console.log(genTriangle(25))
console.log("")
 
for (const x of [23, 123, 1234, 12345]){
console.log("G(" + x + ") = " + G(x))
}
</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
 
G(23) = 1255
G(123) = 2552338241
G(1234) = 156978797223733228787865722354959930
G(12345) = 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736
</pre>
 
=={{header|jq}}==
''Adapted from [[#Wren|Wren]]''
 
'''Works with gojq, the Go implementation of jq, and with fq.'''
 
This entry uses the same algorithm as [[#Wren|Wren]], but on my 16GM
RAM machine, wren version 0.4.0 runs out of memory computing P(12345),
so that goal has been excluded here.
 
The integer arithmetic supported by the C implementation of jq lacks the precision
required for computing P(1234) accurately, so the output shown below is based on
a run of gojq.
 
The values shown in the output agree with those obtained using the programs
at [[Partition_function_P#jq]].
<syntaxhighlight lang=jq>
def cumu:
. as $n
| reduce range(1; $n+1) as $l ( {cache: [[1]]};
.r = [0]
| reduce range(1; $l+1) as $x (.;
.min = $l - $x
| if ($x < .min) then .min = $x else . end
| .r = .r + [.r[-1] + .cache[$l - $x][.min] ] )
| .cache = .cache + [.r] )
| .cache[$n] ;
 
def row:
cumu as $r
| reduce range(0; .) as $i ([]; . + [$r[$i+1] - $r[$i]] );
 
def task:
"Rows:",
(range(1; 26) | [ ., row]),
"\nSums:",
( (23, 123, 1234) # 12345 is a stretch for memory even using wren
| [., cumu[-1]] ) ;
</syntaxhighlight>
 
'''Invocation''': gojq -n -rcf 9-billion.jq
{{output}}
<pre>
Rows:
[1,[1]]
[2,[1,1]]
[3,[1,1,1]]
[4,[1,2,1,1]]
[5,[1,2,2,1,1]]
[6,[1,3,3,2,1,1]]
[7,[1,3,4,3,2,1,1]]
[8,[1,4,5,5,3,2,1,1]]
[9,[1,4,7,6,5,3,2,1,1]]
[10,[1,5,8,9,7,5,3,2,1,1]]
[11,[1,5,10,11,10,7,5,3,2,1,1]]
[12,[1,6,12,15,13,11,7,5,3,2,1,1]]
[13,[1,6,14,18,18,14,11,7,5,3,2,1,1]]
[14,[1,7,16,23,23,20,15,11,7,5,3,2,1,1]]
[15,[1,7,19,27,30,26,21,15,11,7,5,3,2,1,1]]
[16,[1,8,21,34,37,35,28,22,15,11,7,5,3,2,1,1]]
[17,[1,8,24,39,47,44,38,29,22,15,11,7,5,3,2,1,1]]
[18,[1,9,27,47,57,58,49,40,30,22,15,11,7,5,3,2,1,1]]
[19,[1,9,30,54,70,71,65,52,41,30,22,15,11,7,5,3,2,1,1]]
[20,[1,10,33,64,84,90,82,70,54,42,30,22,15,11,7,5,3,2,1,1]]
[21,[1,10,37,72,101,110,105,89,73,55,42,30,22,15,11,7,5,3,2,1,1]]
[22,[1,11,40,84,119,136,131,116,94,75,56,42,30,22,15,11,7,5,3,2,1,1]]
[23,[1,11,44,94,141,163,164,146,123,97,76,56,42,30,22,15,11,7,5,3,2,1,1]]
[24,[1,12,48,108,164,199,201,186,157,128,99,77,56,42,30,22,15,11,7,5,3,2,1,1]]
[25,[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]]
 
Sums:
[23,1255]
[123,2552338241]
[1234,156978797223733228787865722354959930]
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
using Combinatorics, StatsBase
 
Line 2,036 ⟶ 2,451:
end
 
</langsyntaxhighlight> {{output}} <pre>
[1]
[1, 1]
Line 2,078 ⟶ 2,493:
=={{header|Kotlin}}==
{{trans|Swift}}
<langsyntaxhighlight lang="scala">import java.lang.Math.min
import java.math.BigInteger
import java.util.ArrayList
Line 2,112 ⟶ 2,527:
System.out.printf("%s %s%n", it, c[c.size - 1])
}
}</langsyntaxhighlight>
 
<pre>
Line 2,150 ⟶ 2,565:
=={{header|Lasso}}==
This code is derived from the Python solution, as an illustration of the difference in array behaviour (indexes, syntax), and loop and query expression as alternative syntax to "for".
<langsyntaxhighlight Lassolang="lasso">define cumu(n::integer) => {
loop(-from=$cache->size,-to=#n+1) => {
local(r = array(0), l = loop_count)
Line 2,180 ⟶ 2,595:
cumu(#x+1)->last
'\r'
^}</langsyntaxhighlight>
 
{{out}}
Line 2,217 ⟶ 2,632:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function nog(n)
local tri = {{1}}
for r = 2, n do
Line 2,239 ⟶ 2,654:
end
print("G(23) = " .. G(23))
print("G(123) = " .. G(123))</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 2,270 ⟶ 2,685:
 
=={{header|Maple}}==
<langsyntaxhighlight lang="maple">TriangleLine(n) := map(rhs, Statistics :- Tally(map(x -> x[-1], combinat:-partition(n)))):
Triangle := proc(m)
local i;
Line 2,277 ⟶ 2,692:
end do
end proc:
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,291 ⟶ 2,706:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">Table[Last /@ Reverse@Tally[First /@ IntegerPartitions[n]], {n, 10}] // Grid</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,305 ⟶ 2,720:
 
Here I use the bulit-in function PartitionsP to calculate <math>P(n)</math>.
<langsyntaxhighlight lang="mathematica">PartitionsP /@ {23, 123, 1234, 12345}</langsyntaxhighlight>
{{out}}
<pre>{1255, 2552338241, 156978797223733228787865722354959930, 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736}</pre>
 
<langsyntaxhighlight lang="mathematica">DiscretePlot[PartitionsP[n], {n, 1, 999}, PlotRange -> All]</langsyntaxhighlight>
[[File:9 billion names of God the integer Mathematica.png]]
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">for n thru 25 do print(makelist(length(integer_partitions(n-k,k)),k,1,n))$</langsyntaxhighlight>
{{out}}
<pre>[1]
Line 2,342 ⟶ 2,757:
 
Using the built-in function to calculate <math>P(n)</math>:
<langsyntaxhighlight lang="maxima">makelist(num_partitions(n),n,[23,123,1234,12345]);</langsyntaxhighlight>
{{out}}
<pre>(%o1) [1255, 2552338241, 156978797223733228787865722354959930, 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736]</pre>
Line 2,348 ⟶ 2,763:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import bigints
 
var cache = @[@[1.initBigInt]]
Line 2,373 ⟶ 2,788:
for x in [23, 123, 1234, 12345]:
let c = cumu(x)
echo x, " ", c[c.high]</langsyntaxhighlight>
{{out}}
<pre>@[1]
Line 2,393 ⟶ 2,808:
Faster version:
{{trans|C}}
<langsyntaxhighlight lang="nim">import bigints
 
var p = @[1.initBigInt]
Line 2,425 ⟶ 2,840:
let p = partitions(i)
if i in ns:
echo i,": ",p</langsyntaxhighlight>
{{out}}
<pre>23: 1255
Line 2,433 ⟶ 2,848:
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">
let get, sum_unto =
let cache = ref [||]
Line 2,512 ⟶ 2,927:
end
[23;123;1234;12345;123456]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,558 ⟶ 2,973:
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(define (nine-billion-names row column)
(cond
Line 2,583 ⟶ 2,998:
(print-triangle 25)
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,615 ⟶ 3,030:
=={{header|PARI/GP}}==
 
<langsyntaxhighlight lang="parigp">row(n)=my(v=vector(n)); forpart(i=n,v[i[#i]]++); v;
show(n)=for(k=1,n,print(row(k)));
show(25)
apply(numbpart, [23,123,1234,12345])
plot(x=1,999.9, numbpart(x\1))</langsyntaxhighlight>
{{out}}
<pre>[1]
Line 2,677 ⟶ 3,092:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/:all/;
 
sub triangle_row {
Line 2,688 ⟶ 3,103:
printf "%2d: %s\n", $_, join(" ",triangle_row($_)) for 1..25;
print "\n";
say "P($_) = ", partitions($_) for (23, 123, 1234, 12345);</langsyntaxhighlight>
{{out}}
[rows are the same as below]
Line 2,697 ⟶ 3,112:
 
{{trans|Raku}}
<langsyntaxhighlight lang="perl">
use strict;
use warnings;
Line 2,738 ⟶ 3,153:
print $i, "\n";
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,777 ⟶ 3,192:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- demo\rosetta\9billionnames.exw
<span style="color: #000080;font-style:italic;">-- demo\rosetta\9billionnames.exw</span>
 
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence cache = {{1}}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cache</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span>
function cumu(integer n)
<span style="color: #008080;">function</span> <span style="color: #000000;">cumu</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
sequence r
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span>
for l=length(cache) to n do
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cache</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
r = {0}
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}</span>
for x=1 to l do
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
r = append(r,r[-1]+cache[l-x+1][min(x,l-x)+1])
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
cache = append(cache,r)
<span style="color: #000000;">cache</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cache</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return cache[n]
<span style="color: #008080;">return</span> <span style="color: #000000;">cache</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
function row(integer n)
<span style="color: #008080;">function</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
sequence r = cumu(n+1)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cumu</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
sequence res = repeat(0,n)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
for i=1 to n do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
res[i] = r[i+1]-r[i]
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]-</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
for i=1 to 25 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">25</span> <span style="color: #008080;">do</span>
puts(1,repeat(' ',50-2*i))
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
sequence r = row(i)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">row</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
for j=1 to i do
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
printf(1,"%4d",r[j])
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
puts(1,"\n")
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,840 ⟶ 3,257:
{{trans|C}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>include mpfr.e
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
sequence p
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span>
procedure calc(integer n)
n += 1
<span style="color: #008080;">procedure</span> <span style="color: #000000;">calc</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
for k=1 to n-1 do
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
integer d = n - k * (3 * k - 1) / 2;
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
if d<1 then exit end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">3</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">/</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">;</span>
if and_bits(k,1) then mpz_add(p[n],p[n],p[d])
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else mpz_sub(p[n],p[n],p[d]) end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span>
d -= k;
<span style="color: #008080;">else</span> <span style="color: #7060A8;">mpz_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if d<1 then exit end if
<span style="color: #000000;">d</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">;</span>
if and_bits(k,1) then mpz_add(p[n],p[n],p[d])
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;"><</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else mpz_sub(p[n],p[n],p[d]) end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #008080;">else</span> <span style="color: #7060A8;">mpz_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
constant cx = {23, 123, 1234, 12345}
puts(1,"sums:\n")
<span style="color: #008080;">constant</span> <span style="color: #000000;">cx</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">123</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1234</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12345</span><span style="color: #0000FF;">}</span>
integer at = 1
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"sums:\n"</span><span style="color: #0000FF;">)</span>
p = mpz_inits(cx[$]+1)
<span style="color: #004080;">integer</span> <span style="color: #000000;">at</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
mpz_set_si(p[1],1)
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">[$]+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
for i=1 to cx[$] do
<span style="color: #7060A8;">mpz_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
calc(i)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">[$]</span> <span style="color: #008080;">do</span>
if i=cx[at] then
<span style="color: #000000;">calc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
printf(1,"%2d:%s\n",{i,mpz_get_str(p[i+1])})
<span style="color: #008080;">if</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">[</span><span style="color: #000000;">at</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
at += 1
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d:%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])})</span>
end if
<span style="color: #000000;">at</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{Out}}
<pre>
Line 2,881 ⟶ 3,301:
=== Third and last, a simple plot ===
{{libheader|Phix/pGUI}}
<!--<syntaxhighlight lang="phix">-->
<lang Phix>include pGUI.e
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
IupOpen()
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
IupControlsOpen()
<span style="color: #7060A8;">IupControlsOpen</span><span style="color: #0000FF;">()</span>
Ihandle plot = IupPlot("MENUITEMPROPERTIES=Yes, SIZE=640x320")
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">plot</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupPlot</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"MENUITEMPROPERTIES=Yes, SIZE=640x320"</span><span style="color: #0000FF;">)</span>
IupSetAttribute(plot, "TITLE", "9 Billion Names");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"9 Billion Names"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "TITLEFONTSIZE", "10");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLEFONTSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"10"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "TITLEFONTSTYLE", "ITALIC");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLEFONTSTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ITALIC"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "GRIDLINESTYLE", "DOTTED");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"GRIDLINESTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DOTTED"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "GRID", "YES");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"GRID"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"YES"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_XLABEL", "x");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_XLABEL"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"x"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_YLABEL", "G(x)");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YLABEL"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"G(x)"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_XFONTSTYLE", "ITALIC");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_XFONTSTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ITALIC"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_YFONTSTYLE", "ITALIC");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YFONTSTYLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ITALIC"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_XSCALE", "LOG10");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_XSCALE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"LOG10"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_YSCALE", "LOG10");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YSCALE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"LOG10"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_YTICKSIZEAUTO", "NO");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YTICKSIZEAUTO"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"NO"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_YTICKMAJORSIZE", "8");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YTICKMAJORSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"8"</span><span style="color: #0000FF;">);</span>
IupSetAttribute(plot, "AXS_YTICKMINORSIZE", "0");
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"AXS_YTICKMINORSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0"</span><span style="color: #0000FF;">);</span>
IupPlotBegin(plot)
<span style="color: #7060A8;">IupPlotBegin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">)</span>
for x=1 to 999 do
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">999</span> <span style="color: #008080;">do</span>
IupPlotAdd(plot, x, sum(row(x))) -- (row() from part 1)
<span style="color: #7060A8;">IupPlotAdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">row</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)))</span> <span style="color: #000080;font-style:italic;">-- (row() from part 1)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
{} = IupPlotEnd(plot)
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupPlotEnd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">)</span>
Ihandle dlg = IupDialog(plot)
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">plot</span><span style="color: #0000FF;">)</span>
IupCloseOnEscape(dlg)
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"9 Billion Names"</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg, "TITLE", "9 Billion Names")
<span style="color: #7060A8;">IupMap</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
IupMap(dlg)
<span style="color: #7060A8;">IupShowXY</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #004600;">IUP_CENTER</span><span style="color: #0000FF;">,</span><span style="color: #004600;">IUP_CENTER</span><span style="color: #0000FF;">)</span>
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
IupMainLoop()
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
IupClose()</lang>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|Phixmonti}}==
{{trans|Yabasic}}
<syntaxhighlight lang="phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/9_billion_names_of_God_the_integer
by Galileo, 05/2022 #/
 
include ..\Utilitys.pmt
 
cls
def nine_billion_names >ps
0 ( tps dup ) dim
1 ( 1 1 ) sset
( 2 tps ) for var i
( 1 i ) for var j
( i 1 - j 1 - ) sget >ps ( i j - j ) sget ps> + ( i j ) sset
endfor
endfor
( 1 tps ) for var i
tps 2 * i 2 * 2 - - >ps
( 1 i ) for var j
( i j ) sget tostr len nip 1 swap - tps j 4 * + + i locate ( i j ) sget print
endfor
nl
ps> drop
endfor
ps> drop drop
enddef
20 nine_billion_names</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
 
=== Press any key to exit ===</pre>
 
=={{header|Picat}}==
===The triangle, using constraint modelling===
Using constraint modeling to generate all the partitions 1..25.
<syntaxhighlight lang="picat">import cp.
 
main =>
foreach(N in 1..25)
P = integer_partition(N).reverse,
G = P.map(sort_down).map(first).counts.to_list.sort.map(second),
println(G=G.sum)
end,
println("Num partitions == sum of rows:"),
println([partition1(N) : N in 1..25]).
 
% Get all partitions
integer_partition(N) = find_all(X,integer_partition(N,X)).
integer_partition(N,X) =>
member(Len,1..N),
X = new_list(Len),
X :: 1..N,
increasing(X),
sum(X) #= N,
solve($[split],X).
 
% Counts the occurrences of the elements in L
counts(L) = Map =>
Map = new_map(),
foreach(I in L)
Map.put(I,Map.get(I,0)+1)
end.</syntaxhighlight>
 
{{out}}
<pre>[1] = 1
[1,1] = 2
[1,1,1] = 3
[1,2,1,1] = 5
[1,2,2,1,1] = 7
[1,3,3,2,1,1] = 11
[1,3,4,3,2,1,1] = 15
[1,4,5,5,3,2,1,1] = 22
[1,4,7,6,5,3,2,1,1] = 30
[1,5,8,9,7,5,3,2,1,1] = 42
[1,5,10,11,10,7,5,3,2,1,1] = 56
[1,6,12,15,13,11,7,5,3,2,1,1] = 77
[1,6,14,18,18,14,11,7,5,3,2,1,1] = 101
[1,7,16,23,23,20,15,11,7,5,3,2,1,1] = 135
[1,7,19,27,30,26,21,15,11,7,5,3,2,1,1] = 176
[1,8,21,34,37,35,28,22,15,11,7,5,3,2,1,1] = 231
[1,8,24,39,47,44,38,29,22,15,11,7,5,3,2,1,1] = 297
[1,9,27,47,57,58,49,40,30,22,15,11,7,5,3,2,1,1] = 385
[1,9,30,54,70,71,65,52,41,30,22,15,11,7,5,3,2,1,1] = 490
[1,10,33,64,84,90,82,70,54,42,30,22,15,11,7,5,3,2,1,1] = 627
[1,10,37,72,101,110,105,89,73,55,42,30,22,15,11,7,5,3,2,1,1] = 792
[1,11,40,84,119,136,131,116,94,75,56,42,30,22,15,11,7,5,3,2,1,1] = 1002
[1,11,44,94,141,163,164,146,123,97,76,56,42,30,22,15,11,7,5,3,2,1,1] = 1255
[1,12,48,108,164,199,201,186,157,128,99,77,56,42,30,22,15,11,7,5,3,2,1,1] = 1575
[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] = 1958
Num partitions == sum of rows:
[1,2,3,5,7,11,15,22,30,42,56,77,101,135,176,231,297,385,490,627,792,1002,1255,1575,1958]</pre>
 
===Number of partitions===
This is the Picat solution of [http://rosettacode.org/wiki/Partition_function_P Partition_function_P].
<syntaxhighlight lang="picat">% Number of partitions
go2 =>
foreach(N in [23,123,1234,12345,123456])
println(N=partition1(N))
end,
nl.
 
table
partition1(0) = 1.
partition1(N) = P =>
S = 0,
K = 1,
M = (K*(3*K-1)) // 2,
while (M <= N)
S := S - ((-1)**K)*partition1(N-M),
K := K + 1,
M := (K*(3*K-1)) // 2
end,
K := 1,
M := (K*(3*K+1)) // 2,
while (M <= N)
S := S - ((-1)**K)*partition1(N-M),
K := K + 1,
M := (K*(3*K+1)) // 2
end,
P = S.</syntaxhighlight>
 
{{out}}
<pre>23 = 1255
123 = 2552338241
1234 = 156978797223733228787865722354959930
12345 = 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736
123456 = 30817659578536496678545317146533980855296613274507139217608776782063054452191537379312358383342446230621170608408020911309259407611257151683372221925128388387168451943800027128045369650890220060901494540459081545445020808726917371699102825508039173543836338081612528477859613355349851184591540231790254269948278726548570660145691076819912972162262902150886818986555127204165221706149989</pre>
 
===Recursion===
Here is a port of the Haskell code from [http://oeis.org/A000041 oeis.org/A000041]. Though for 12345 it's too slow (and eats much RAM).
<syntaxhighlight lang="picat">pc(N) = pc(1,N).
table
pc(_,0) = 1.
pc(1,1) = 1.
pc(K,M) = cond(M < K, 0, pc(K, M-K) + pc(K + 1,M)).</syntaxhighlight>
 
=={{header|PicoLisp}}==
{{trans|Python}}
<langsyntaxhighlight PicoLisplang="picolisp">(de row (N)
(let C '((1))
(do N
Line 2,965 ⟶ 3,548:
(println (sumr I)) ) )
 
(bye)</langsyntaxhighlight>
 
{{out}}
Line 2,998 ⟶ 3,581:
69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736
0.626 sec</pre>
 
=={{header|Pike}}==
{{trans|Python}}
 
<syntaxhighlight lang="pike">array cumu(int n) {
array(array(int)) cache = ({({1})});
 
for(int l = sizeof(cache); l < n + 1; l++) {
array(int) r = ({0});
for(int x = 1; x < l + 1; x++) {
r = Array.push(r, r[-1] + cache[l - x][min(x, l-x)]);
}
cache = Array.push(cache, r);
}
return cache[n];
}
 
array row(int n) {
array r = cumu(n);
array res = ({});
for (int i = 0; i < n; i++) {
res = Array.push(res, r[i+1] - r[i]);
}
return res;
}
 
int main() {
write("rows:\n");
for(int x = 1; x < 11; x++) {
write("%2d: ", x);
for(int i = 0; i < sizeof(row(x)); i++) {
write((string)row(x)[i] + " ");
}
write("\n");
}
 
array(int) sum_n = ({23, 123, 1234, 12345});
write("\nsums:\n");
for (int x = 0; x < sizeof(sum_n); x++) {
write((string)sum_n[x] + " " + (string)cumu(sum_n[x])[-1] + "\n");
}
return 0;
}</syntaxhighlight>
{{out}} Not wait for "12345" output.
<pre>rows:
1: 1
2: 1 1
3: 1 1 1
4: 1 2 1 1
5: 1 2 2 1 1
6: 1 3 3 2 1 1
7: 1 3 4 3 2 1 1
8: 1 4 5 5 3 2 1 1
9: 1 4 7 6 5 3 2 1 1
10: 1 5 8 9 7 5 3 2 1 1
 
sums:
23 1255
123 2552338241
1234 156978797223733228787865722354959930
^C</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">
Define nMax.i=25, n.i, k.i
Dim pfx.s(1)
Line 3,069 ⟶ 3,713:
PrintN(sum(12345,pfx()))
Input()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,105 ⟶ 3,749:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">cache = [[1]]
def cumu(n):
for l in range(len(cache), n+1):
Line 3,123 ⟶ 3,767:
 
print "\nsums:"
for x in [23, 123, 1234, 12345]: print x, cumu(x)[-1]</langsyntaxhighlight>
{{out}} (I didn't actually wait long enough to see what the sum for 12345 is)
<pre>
Line 3,145 ⟶ 3,789:
</pre>
To calculate partition functions only:
<langsyntaxhighlight lang="python">def partitions(N):
diffs,k,s = [],1,1
while k * (3*k-1) < 2*N:
Line 3,162 ⟶ 3,806:
 
p = partitions(12345)
for x in [23,123,1234,12345]: print x, p[x]</langsyntaxhighlight>
 
This version uses only a fraction of the memory and of the running time, compared to the first one that has to generate all the rows:
{{trans|C}}
<langsyntaxhighlight lang="python">def partitions(n):
partitions.p.append(0)
 
Line 3,203 ⟶ 3,847:
print "%6d: %s" % (i, p)
 
main()</langsyntaxhighlight>
{{out}}
<pre> 23: 1255
Line 3,209 ⟶ 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}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (cdr-empty ls) (if (empty? ls) empty (cdr ls)))
Line 3,234 ⟶ 3,924:
(newline)
(map G '(23 123 1234)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,271 ⟶ 3,961:
To save a bunch of memory, this algorithm throws away all the numbers that it knows it's not going to use again, on the assumption that the function will only be called with increasing values of $n. (It could easily be made to recalculate if it notices a regression.)
 
<syntaxhighlight lang="raku" perl6line>my @todo = $[1];
my @sums = 0;
sub nextrow($n) {
for +@todo .. $n -> $l {
@sums[$l] = 0;
print $l,"\r" if $l < $n;
my $r = [];
for reverse ^$l -> $x {
Line 3,293 ⟶ 3,981:
 
say "rows:";
say .fmt('%2d'), ": ", nextrow($_)[] for 1..1025;
 
 
my @names-of-God = 1, { partition-sum ++$ } … *;
my @names-of-God-adder = lazy [\+] flat 1, ( (1 .. *) Z (1 .. *).map: * × 2 + 1 );
sub partition-sum ($n) {
sum @names-of-God[$n X- @names-of-God-adder[^(@names-of-God-adder.first: * > $n, :k)]]
Z× (flat (1, 1, -1, -1) xx *)
}
 
say "\nsums:";
for 23, 123, 1234, 12345 {
sayput $_, "\t", @names-of-God[+] nextrow($_)[];
}</langsyntaxhighlight>
{{out}}
<pre>rows:
1: [1]
2: [1 1]
3: [1 1 1]
4: [1 2 1 1]
5: [1 2 2 1 1]
6: [1 3 3 2 1 1]
7: [1 3 4 3 2 1 1]
8: [1 4 5 5 3 2 1 1]
9: [1 4 7 6 5 3 2 1 1]
10: [1 5 8 9 7 5 3 2 1 1]
11: [1 5 10 11 10 7 5 3 2 1 1]
12: [1 6 12 15 13 11 7 5 3 2 1 1]
13: [1 6 14 18 18 14 11 7 5 3 2 1 1]
14: [1 7 16 23 23 20 15 11 7 5 3 2 1 1]
15: [1 7 19 27 30 26 21 15 11 7 5 3 2 1 1]
16: [1 8 21 34 37 35 28 22 15 11 7 5 3 2 1 1]
17: [1 8 24 39 47 44 38 29 22 15 11 7 5 3 2 1 1]
18: [1 9 27 47 57 58 49 40 30 22 15 11 7 5 3 2 1 1]
19: [1 9 30 54 70 71 65 52 41 30 22 15 11 7 5 3 2 1 1]
20: [1 10 33 64 84 90 82 70 54 42 30 22 15 11 7 5 3 2 1 1]
21: [1 10 37 72 101 110 105 89 73 55 42 30 22 15 11 7 5 3 2 1 1]
22: [1 11 40 84 119 136 131 116 94 75 56 42 30 22 15 11 7 5 3 2 1 1]
23: [1 11 44 94 141 163 164 146 123 97 76 56 42 30 22 15 11 7 5 3 2 1 1]
24: [1 12 48 108 164 199 201 186 157 128 99 77 56 42 30 22 15 11 7 5 3 2 1 1]
25: [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]
 
sums:
Line 3,318 ⟶ 4,028:
1234 156978797223733228787865722354959930
12345 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736</pre>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">
Red []
 
context [
sum-part: function [nums [block!] count [integer!]][
out: 0.0
loop count [
out: out + nums/1
if empty? nums: next nums [break]
]
out
]
nums: make map! [1 [1] 2 [1 1]]
sums: make map! [1 1 2 2]
set 'names function [row /show /all][
if row < 1 [cause-error 'user 'message "Argument needs to be >= 1"]
if show [
unless nums/:row [names row]
repeat i row [either all [probe reduce [i nums/:i sums/:i]][print nums/:i]]
]
either sums/:row [sums/:row][
out: clear []
half: to integer! row / 2
if row - 1 > last: length? nums [
repeat i row - last - 1 [names last + i]
]
repeat col row - 1 [
either col = (half + 1) [
append out at nums/(row - 1) half
break
][
append out sum-part nums/(row - col) col
]
]
also sums/:row: sum nums/:row: copy out clear out
]
]
]
 
print "rows: ^/"
names/show 25
print "^/sums: ^/"
probe names 23
probe names 123
probe names 1234
 
</syntaxhighlight>
 
{{out}}
<pre>
rows:
 
1
1 1
1.0 1 1
1.0 2.0 1 1
1.0 2.0 2.0 1 1
1.0 3.0 3.0 2.0 1 1
1.0 3.0 4.0 3.0 2.0 1 1
1.0 4.0 5.0 5.0 3.0 2.0 1 1
1.0 4.0 7.0 6.0 5.0 3.0 2.0 1 1
1.0 5.0 8.0 9.0 7.0 5.0 3.0 2.0 1 1
1.0 5.0 10.0 11.0 10.0 7.0 5.0 3.0 2.0 1 1
1.0 6.0 12.0 15.0 13.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 6.0 14.0 18.0 18.0 14.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 7.0 16.0 23.0 23.0 20.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 7.0 19.0 27.0 30.0 26.0 21.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 8.0 21.0 34.0 37.0 35.0 28.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 8.0 24.0 39.0 47.0 44.0 38.0 29.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 9.0 27.0 47.0 57.0 58.0 49.0 40.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 9.0 30.0 54.0 70.0 71.0 65.0 52.0 41.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 10.0 33.0 64.0 84.0 90.0 82.0 70.0 54.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 10.0 37.0 72.0 101.0 110.0 105.0 89.0 73.0 55.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 11.0 40.0 84.0 119.0 136.0 131.0 116.0 94.0 75.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 11.0 44.0 94.0 141.0 163.0 164.0 146.0 123.0 97.0 76.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 12.0 48.0 108.0 164.0 199.0 201.0 186.0 157.0 128.0 99.0 77.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
1.0 12.0 52.0 120.0 192.0 235.0 248.0 230.0 201.0 164.0 131.0 100.0 77.0 56.0 42.0 30.0 22.0 15.0 11.0 7.0 5.0 3.0 2.0 1 1
 
sums:
 
1255.0
2552338241.0
1.5697879722373306e35
</pre>
 
=={{header|REXX}}==
Line 3,349 ⟶ 4,145:
</big>
which is derived from Euler's generating function.
<langsyntaxhighlight lang="rexx">/*REXX program generates and displays a number triangle for partitions of a number. */
numeric digits 400 /*be able to handle larger numbers. */
parse arg N . /*obtain optional argument from the CL.*/
Line 3,406 ⟶ 4,202:
else $= $ - x - y /* " " " " " " even.*/
end /*k*/ /* [↑] Euler's recursive func.*/
@.n= $; return $ /*use memoization; return num.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input &nbsp; (of 25 rows):}}
<pre>
Line 3,466 ⟶ 4,262:
=={{header|Ruby}}==
===Naive Solution===
<langsyntaxhighlight lang="ruby">
# Generate IPF triangle
# Nigel_Galloway: May 1st., 2013.
Line 3,477 ⟶ 4,273:
puts (1..n).map {|g| "%4s" % g(n,g)}.join
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,508 ⟶ 4,304:
 
===Full Solution===
<langsyntaxhighlight lang="ruby">
# Find large values of IPF
# Nigel_Galloway: May 1st., 2013.
Line 3,539 ⟶ 4,335:
n = 3 + @ipn1.inject(:+) + @ipn2.inject(:+)
puts "G(12345) = #{n}"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,550 ⟶ 4,346:
=={{header|Rust}}==
{{trans|Python}}
<langsyntaxhighlight lang="rust">extern crate num;
 
use std::cmp;
Line 3,594 ⟶ 4,390:
println!("{}: {}", x, s);
}
}</langsyntaxhighlight>
{{out}}
<pre>rows:
Line 3,631 ⟶ 4,427:
=={{header|Scala}}==
===Naive Solution===
<langsyntaxhighlight lang="scala">
object Main {
 
Line 3,668 ⟶ 4,464:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,711 ⟶ 4,507:
 
===Full Solution===
<langsyntaxhighlight Scalalang="scala">val cache = new Array[BigInt](15000)
cache(0) = 1
val cacheNaive = scala.collection.mutable.Map[Tuple2[Int, Int], BigInt]()
Line 3,759 ⟶ 4,555:
println(quickPartitions(123))
println(quickPartitions(1234))
println(quickPartitions(12345))</langsyntaxhighlight>
{{out}}
<pre> 1
Line 3,790 ⟶ 4,586:
 
=={{header|scheme}}==
<langsyntaxhighlight lang="scheme">(define (f m n)
(define (sigma g x y)
(define (sum i)
Line 3,809 ⟶ 4,605:
(cond ((< i x) (begin (display (line i)) (display "\n") (print-loop (+ i 1)) ))))
(print-loop 1))
(print 25)</langsyntaxhighlight>
 
{{out}}
Line 3,840 ⟶ 4,636:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var cache = [[1]]
 
func cumu (n) {
Line 3,867 ⟶ 4,663:
for i in [23, 123, 1234, 12345] {
"%2s : %4s\n".printf(i, cumu(i)[-1])
}</langsyntaxhighlight>
 
{{out}}
Line 3,896 ⟶ 4,692:
 
=={{header|SPL}}==
<langsyntaxhighlight lang="spl">'print triangle
> n, 1..25
k = 50-n*2
Line 3,933 ⟶ 4,729:
<
<= p[n+1]
.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,969 ⟶ 4,765:
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">mata
function part(n) {
a = J(n,n,.)
Line 3,978 ⟶ 4,774:
return(a)
}
end</langsyntaxhighlight>
 
The result is shown for n=10 to keep it small. Due to computations being done in floating point, the result is exact up to n=299, and suffers rounding for larger values of n. Compare the array with [http://[oeis.org/:A008284 |OEIS A008284]] and row sums with [http://[oeis.org/:A000041 |OEIS A000041]].
 
'''Output'''
Line 4,008 ⟶ 4,804:
=={{header|Swift}}==
{{trans|Python}}
<langsyntaxhighlight Swiftlang="swift">var cache = [[1]]
func namesOfGod(n:Int) -> [Int] {
for l in cache.count...n {
Line 4,041 ⟶ 4,837:
var numInt = array[array.count - 1]
println("\(x): \(numInt)")
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,080 ⟶ 4,876:
=={{header|Tcl}}==
{{trans|Python}}
<langsyntaxhighlight lang="tcl">set cache 1
proc cumu {n} {
global cache
Line 4,109 ⟶ 4,905:
foreach x {23 123 1234 12345} {
puts "${x}: [lindex [cumu $x] end]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,132 ⟶ 4,928:
<small>(I killed the run when it started to take a significant proportion of my system's memory.)</small>
 
=={{header|uBasic/4tH}}==
{{trans|VBA}}
Since uBasic/4tH features a single array of 256 elements, level "15" is the best that can be achieved.
<syntaxhighlight lang="text">Proc _NineBillionNames(15)
End
 
_NineBillionNames
Param (1)
Local (3)
@(1*a@ + 1) = 1
For b@ = 2 To a@
For c@ = 1 To b@
@(b@*a@ + c@) = @((b@ - 1)*a@ + (c@ - 1)) + @((b@ - c@)*a@ + c@)
Next
Next
For b@ = 1 To a@
d@ = a@ * 2 - 2 * b@ - 2
For c@ = 1 To b@
Print Tab(d@ + c@ * 4 + (1 - Len(Str(@(b@*a@ + c@))))); @(b@*a@ + c@);
Next
Next
Print
Return</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
 
0 OK, 0:30 </pre>
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Sub nine_billion_names()
Dim p(25, 25) As Long
p(1, 1) = 1
Line 4,148 ⟶ 4,989:
Debug.Print
Next i
End Sub</langsyntaxhighlight>{{out}}
<pre> 1
1 1
Line 4,168 ⟶ 5,009:
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</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import math.big
 
fn int_min(a int, b int) int {
if a < b {
return a
} else {
return b
}
}
 
fn cumu (mut cache [][]big.Integer, n int) []big.Integer {
for y := cache.len; y <= n; y++ {
mut row := [big.zero_int]
for x := 1; x <= y; x++ {
cache_value := cache[y-x][int_min(x, y-x)]
row << row[row.len-1] + cache_value
}
cache << row
}
return cache[n]
}
fn main() {
mut cache := [[big.one_int]]
row := fn[mut cache](n int) {
e := cumu(mut cache, n)
for i := 0; i < n; i++ {
print(" ${e[i+1]-e[i]} ")
}
println('')
}
println("rows:")
for x := 1; x < 11; x++ {
row(x)
}
println('')
println("sums:")
for num in [23, 123, 1234, 12345] {
r := cumu(mut cache, num)
println("$num ${r[r.len-1]}")
}
}</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
 
sums:
23 1255
123 2552338241
1234 156978797223733228787865722354959930
12345 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736
</pre>
 
=={{header|Wren}}==
Line 4,173 ⟶ 5,083:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./fmt" for Fmt
 
var cache = [[BigInt.one]]
Line 4,205 ⟶ 5,115:
[23, 123, 1234, 12345].each { |i|
Fmt.print("$5s: $s", i, cumu.call(i)[-1])
}</langsyntaxhighlight>
 
{{out}}
Line 4,245 ⟶ 5,155:
=={{header|Yabasic}}==
{{trans|VBA}}
<langsyntaxhighlight Yabasiclang="yabasic">clear screen
 
Sub nine_billion_names(rows)
Line 4,265 ⟶ 5,175:
End Sub
 
nine_billion_names(20)</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|C}}
Takes its time getting to 100,000 but it does. Uses the GMP big int library. Does the big int math in place to avoid garbage creation.
<langsyntaxhighlight lang="zkl">var [const] BN=Import.lib("zklBigNum");
const N=0d100_000;
Line 4,286 ⟶ 5,196:
}
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">idx:=T(23, 123, 1234, 12345, 20000, 30000, 40000, 50000, N);
p[0].set(1);
 
Line 4,293 ⟶ 5,203:
(1).pump(i,Void,calc.fp1(p)); // for n in [1..i] do calc(n,p)
"%2d:\t%d".fmt(i,p[i]).println();
}</langsyntaxhighlight>
The .fp/.fp1 methods create a closure, fixing the first or second parameter.
{{out}}
Line 4,304 ⟶ 5,214:
100000: 27493510569775696512677516320986352688173429315980054758203125984302147328114964173055050741660736621590157844774296248940493063070200461792764493033510116079342457190155718943509725312466108452006369558934464248716828789832182345009262853831404597021307130674510624419227311238999702284408609370935531629697851569569892196108480158600569421098519
</pre>
{{omit from|6502 Assembly|Good luck doing this with only 64K of addressable memory}}
{{omit from|Z80 Assembly|See above}}
3

edits