How to resolve the algorithm 9 billion names of God the integer step by step in the Pike programming language
How to resolve the algorithm 9 billion names of God the integer step by step in the Pike programming language
Table of Contents
Problem Statement
This task is a variation of the short story by Arthur C. Clarke. (Solvers should be aware of the consequences of completing this task.) In detail, to specify what is meant by a “name”:
Display the first 25 rows of a number triangle which begins: Where row
n
{\displaystyle n}
corresponds to integer
n
{\displaystyle n}
, and each column
C
{\displaystyle C}
in row
m
{\displaystyle m}
from left to right corresponds to the number of names beginning with
C
{\displaystyle C}
. A function
G ( n )
{\displaystyle G(n)}
should return the sum of the
n
{\displaystyle n}
-th row. Demonstrate this function by displaying:
G ( 23 )
{\displaystyle G(23)}
,
G ( 123 )
{\displaystyle G(123)}
,
G ( 1234 )
{\displaystyle G(1234)}
, and
G ( 12345 )
{\displaystyle G(12345)}
.
Optionally note that the sum of the
n
{\displaystyle n}
-th row
P ( n )
{\displaystyle P(n)}
is the integer partition function. Demonstrate this is equivalent to
G ( n )
{\displaystyle G(n)}
by displaying:
P ( 23 )
{\displaystyle P(23)}
,
P ( 123 )
{\displaystyle P(123)}
,
P ( 1234 )
{\displaystyle P(1234)}
, and
P ( 12345 )
{\displaystyle P(12345)}
.
If your environment is able, plot
P ( n )
{\displaystyle P(n)}
against
n
{\displaystyle n}
for
n
1 … 999
{\displaystyle n=1\ldots 999}
.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 9 billion names of God the integer step by step in the Pike programming language
Source code in the pike programming language
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;
}
You may also check:How to resolve the algorithm Cramer's rule step by step in the Factor programming language
You may also check:How to resolve the algorithm Nested function step by step in the Perl programming language
You may also check:How to resolve the algorithm Dragon curve step by step in the Perl programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm URL encoding step by step in the V (Vlang) programming language