9 billion names of God the integer: Difference between revisions
Content added Content deleted
m (→{{header|FreeBASIC}}: syntaxhighlight) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 46: | Line 46: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=11l>V cache = [[BigInt(1)]] |
||
F cumu(n) |
F cumu(n) |
||
L(l) :cache.len .. n |
L(l) :cache.len .. n |
||
Line 99: | Line 99: | ||
V p = partitions(i) |
V p = partitions(i) |
||
I i C ns |
I i C ns |
||
print(‘#6: #.’.format(i, p))</ |
print(‘#6: #.’.format(i, p))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 124: | Line 124: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
< |
<syntaxhighlight lang=AArch64 Assembly> |
||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program integerName64.s */ |
/* program integerName64.s */ |
||
Line 290: | Line 290: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 304: | Line 304: | ||
{{libheader|Ada.Numerics.Big_Numbers.Big_Integers}} |
{{libheader|Ada.Numerics.Big_Numbers.Big_Integers}} |
||
< |
<syntaxhighlight lang=Ada>with Ada.Text_IO; |
||
with Ada.Numerics.Big_Numbers.Big_Integers; |
with Ada.Numerics.Big_Numbers.Big_Integers; |
||
Line 445: | Line 445: | ||
Triangle.Print; |
Triangle.Print; |
||
Row_Summer.Put_Sums; |
Row_Summer.Put_Sums; |
||
end Names_Of_God;</ |
end Names_Of_God;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 503: | Line 503: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
< |
<syntaxhighlight lang=ARM Assembly> |
||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program integerName.s */ |
/* program integerName.s */ |
||
Line 654: | Line 654: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 664: | Line 664: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang=AutoHotkey>SetBatchLines -1 |
||
InputBox, Enter_value, Enter the no. of lines sought |
InputBox, Enter_value, Enter the no. of lines sought |
||
Line 721: | Line 721: | ||
} |
} |
||
~Esc::ExitApp</ |
~Esc::ExitApp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
If user inputs 25, the result shall be: |
If user inputs 25, the result shall be: |
||
Line 756: | Line 756: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
#include <gmp.h> |
#include <gmp.h> |
||
Line 795: | Line 795: | ||
at++; |
at++; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 810: | Line 810: | ||
(this requires a System.Numerics registry reference) |
(this requires a System.Numerics registry reference) |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 907: | Line 907: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 1 |
<pre> 1 |
||
Line 948: | Line 948: | ||
===The Code=== |
===The Code=== |
||
see [[http://rosettacode.org/wiki/Talk:9_billion_names_of_God_the_integer#The_Green_Triangle The Green Triangle]]. |
see [[http://rosettacode.org/wiki/Talk:9_billion_names_of_God_the_integer#The_Green_Triangle The Green Triangle]]. |
||
< |
<syntaxhighlight lang=cpp> |
||
// Calculate hypotenuse n of OTT assuming only nothingness, unity, and hyp[n-1] if n>1 |
// Calculate hypotenuse n of OTT assuming only nothingness, unity, and hyp[n-1] if n>1 |
||
// Nigel Galloway, May 6th., 2013 |
// Nigel Galloway, May 6th., 2013 |
||
Line 957: | Line 957: | ||
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);} |
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=== |
===The Alpha and Omega, Beauty=== |
||
Before displaying the triangle the following code displays hyp as it is transformed by consequtive calls of G_hyp. |
Before displaying the triangle the following code displays hyp as it is transformed by consequtive calls of G_hyp. |
||
< |
<syntaxhighlight lang=cpp> |
||
#include <iostream> |
#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 972: | Line 972: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 996: | Line 996: | ||
===The One True Triangle, OTT=== |
===The One True Triangle, OTT=== |
||
The following will display OTT(25). |
The following will display OTT(25). |
||
< |
<syntaxhighlight lang=cpp> |
||
int main(){ |
int main(){ |
||
N = 25; |
N = 25; |
||
Line 1,021: | Line 1,021: | ||
std::cout << "1 1" << std::endl; |
std::cout << "1 1" << std::endl; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,053: | Line 1,053: | ||
===Values of Integer Partition Function=== |
===Values of Integer Partition Function=== |
||
Values of the Integer Partition function may be extracted as follows: |
Values of the Integer Partition function may be extracted as follows: |
||
< |
<syntaxhighlight lang=cpp> |
||
#include <iostream> |
#include <iostream> |
||
int main(){ |
int main(){ |
||
Line 1,065: | Line 1,065: | ||
std::cout << "G(123456) = " << r << std::endl; |
std::cout << "G(123456) = " << r << std::endl; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,076: | Line 1,076: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang=clojure>(defn nine-billion-names [row column] |
||
(cond (<= row 0) 0 |
(cond (<= row 0) 0 |
||
(<= column 0) 0 |
(<= column 0) 0 |
||
Line 1,094: | Line 1,094: | ||
(print-row x))) |
(print-row x))) |
||
(print-triangle 25)</ |
(print-triangle 25)</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang=lisp>(defun 9-billion-names (row column) |
||
(cond ((<= row 0) 0) |
(cond ((<= row 0) 0) |
||
((<= column 0) 0) |
((<= column 0) 0) |
||
Line 1,112: | Line 1,112: | ||
(9-billion-names-triangle 25) |
(9-billion-names-triangle 25) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
===Naive Solution=== |
===Naive Solution=== |
||
< |
<syntaxhighlight lang=ruby>def g(n, g) |
||
return 1 unless 1 < g && g < n-1 |
return 1 unless 1 < g && g < n-1 |
||
(2..g).reduce(1){ |res, q| res + (q > n-g ? 0 : g(n-g, q)) } |
(2..g).reduce(1){ |res, q| res + (q > n-g ? 0 : g(n-g, q)) } |
||
end |
end |
||
(1..25).each { |n| puts (1..n).map { |g| "%4s" % g(n, g) }.join }</ |
(1..25).each { |n| puts (1..n).map { |g| "%4s" % g(n, g) }.join }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,155: | Line 1,155: | ||
===Producing rows=== |
===Producing rows=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=d>import std.stdio, std.bigint, std.algorithm, std.range; |
||
auto cumu(in uint n) { |
auto cumu(in uint n) { |
||
Line 1,181: | Line 1,181: | ||
foreach (x; [23, 123, 1234]) |
foreach (x; [23, 123, 1234]) |
||
writeln(x, " ", x.cumu.back); |
writeln(x, " ", x.cumu.back); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Rows: |
<pre>Rows: |
||
Line 1,203: | Line 1,203: | ||
===Only partition functions=== |
===Only partition functions=== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=d>import std.stdio, std.bigint, std.algorithm; |
||
struct Names { |
struct Names { |
||
Line 1,253: | Line 1,253: | ||
writefln("%6d: %s", i, p); |
writefln("%6d: %s", i, p); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 23: 1255 |
<pre> 23: 1255 |
||
Line 1,276: | Line 1,276: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=Dart>import 'dart:math'; |
||
List<BigInt> partitions(int n) { |
List<BigInt> partitions(int n) { |
||
Line 1,309: | Line 1,309: | ||
print('$i: ${partitions(i)[i]}'); |
print('$i: ${partitions(i)[i]}'); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
In main: |
In main: |
||
< |
<syntaxhighlight lang=Dart> import 'package:DD1_NamesOfGod/DD1_NamesOfGod.dart' as names_of_god; |
||
main(List<String> arguments) { |
main(List<String> arguments) { |
||
names_of_god.printRows(min: 1, max: 11); |
names_of_god.printRows(min: 1, max: 11); |
||
names_of_god.printSums([23, 123, 1234, 12345]); |
names_of_god.printSums([23, 123, 1234, 12345]); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,340: | Line 1,340: | ||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang=Dyalect>var cache = [[1]] |
||
func namesOfGod(n) { |
func namesOfGod(n) { |
||
Line 1,368: | Line 1,368: | ||
for x in 1..25 { |
for x in 1..25 { |
||
print("\(x): \(row(x))") |
print("\(x): \(row(x))") |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,401: | Line 1,401: | ||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
Naive Solution |
Naive Solution |
||
< |
<syntaxhighlight lang=elixir>defmodule God do |
||
def g(n,g) when g == 1 or n < g, do: 1 |
def g(n,g) when g == 1 or n < g, do: 1 |
||
def g(n,g) do |
def g(n,g) do |
||
Line 1,412: | Line 1,412: | ||
Enum.each(1..25, fn n -> |
Enum.each(1..25, fn n -> |
||
IO.puts Enum.map(1..n, fn g -> "#{God.g(n,g)} " end) |
IO.puts Enum.map(1..n, fn g -> "#{God.g(n,g)} " end) |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,446: | Line 1,446: | ||
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. |
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> |
||
-module(triangle). |
-module(triangle). |
||
-export([start/1]). |
-export([start/1]). |
||
Line 1,468: | Line 1,468: | ||
formula(A1,B1)-> |
formula(A1,B1)-> |
||
formula(A1-1,B1-1)+formula(A1-B1,B1). |
formula(A1-1,B1-1)+formula(A1-B1,B1). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,502: | Line 1,502: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2020-01-23}} |
{{works with|Factor|0.99 2020-01-23}} |
||
< |
<syntaxhighlight lang=factor>USING: combinators io kernel math math.ranges memoize prettyprint |
||
sequences ; |
sequences ; |
||
Line 1,522: | Line 1,522: | ||
25 .triangle nl |
25 .triangle nl |
||
"Sums:" print { 23 123 1234 12345 } [ dup pprint bl G . ] each</ |
"Sums:" print { 23 123 1234 12345 } [ dup pprint bl G . ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,730: | Line 1,730: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=frink> |
||
class PartitionCount |
class PartitionCount |
||
{ |
{ |
||
Line 1,785: | Line 1,785: | ||
testRow[1234] |
testRow[1234] |
||
testRow[12345] |
testRow[12345] |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 1,822: | Line 1,822: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
The partition function is built-in. |
The partition function is built-in. |
||
< |
<syntaxhighlight lang=gap>PrintArray(List([1 .. 25], n -> List([1 .. n], k -> NrPartitions(n, k)))); |
||
[ [ 1 ], |
[ [ 1 ], |
||
Line 1,854: | Line 1,854: | ||
[ 1255, 2552338241, 156978797223733228787865722354959930, |
[ 1255, 2552338241, 156978797223733228787865722354959930, |
||
69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736 ]</ |
69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736 ]</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 1,907: | Line 1,907: | ||
fmt.Printf("%d %v\n", num, r[len(r)-1].Text(10)) |
fmt.Printf("%d %v\n", num, r[len(r)-1].Text(10)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,930: | Line 1,930: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang=groovy> |
||
def partitions(c) |
def partitions(c) |
||
{ |
{ |
||
Line 1,972: | Line 1,972: | ||
{partitions(i);} |
{partitions(i);} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,005: | Line 2,005: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=haskell>import Data.List (mapAccumL) |
||
cumu :: [[Integer]] |
cumu :: [[Integer]] |
||
Line 2,025: | Line 2,025: | ||
main = do |
main = do |
||
mapM_ print $ take 10 rows |
mapM_ print $ take 10 rows |
||
mapM_ (print.sums) [23, 123, 1234, 12345]</ |
mapM_ (print.sums) [23, 123, 1234, 12345]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,049: | Line 2,049: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=unicon>procedure main(A) |
||
n := integer(!A) | 10 |
n := integer(!A) | 10 |
||
every r := 2 to (n+1) do write(right(r-1,2),": ",showList(row(r))) |
every r := 2 to (n+1) do write(right(r-1,2),": ",showList(row(r))) |
||
Line 2,073: | Line 2,073: | ||
every (s := "[") ||:= (!A||", ") |
every (s := "[") ||:= (!A||", ") |
||
return s[1:-2]||"]" |
return s[1:-2]||"]" |
||
end</ |
end</syntaxhighlight> |
||
{{out}} (terminated without waiting for output of cumu(12345)): |
{{out}} (terminated without waiting for output of cumu(12345)): |
||
Line 2,098: | Line 2,098: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Recursive calculation of a row element: |
Recursive calculation of a row element: |
||
< |
<syntaxhighlight lang=j>T=: 0:`1:`(($:&<:+ - $: ])`0:@.(0=]))@.(1+*@-) M. "0</syntaxhighlight> |
||
Calculation of the triangle: |
Calculation of the triangle: |
||
< |
<syntaxhighlight lang=j>rows=: <@(#~0<])@({: T ])\@i.</syntaxhighlight> |
||
'''Show triangle''': |
'''Show triangle''': |
||
< |
<syntaxhighlight lang=j> ({.~1+1 i:~ '1'=])"1 ":> }.rows 1+10 |
||
1 |
1 |
||
1 1 |
1 1 |
||
Line 2,112: | Line 2,112: | ||
1 4 5 5 3 2 1 1 |
1 4 5 5 3 2 1 1 |
||
1 4 7 6 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 8 9 7 5 3 2 1 1</syntaxhighlight> |
||
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. |
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: |
Calculate row sums: |
||
< |
<syntaxhighlight lang=j>rowSums=: 3 :0"0 |
||
z=. (y+1){. 1x |
z=. (y+1){. 1x |
||
for_ks. <\1+i.y do. |
for_ks. <\1+i.y do. |
||
Line 2,127: | Line 2,127: | ||
z=. a n}z |
z=. a n}z |
||
end. |
end. |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> ({ [: rowSums >./) 3 23 123 1234 |
<pre> ({ [: rowSums >./) 3 23 123 1234 |
||
Line 2,135: | Line 2,135: | ||
Translation of [[9_billion_names_of_God_the_integer#Python|Python]] via [[9_billion_names_of_God_the_integer#D|D]] |
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}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang=java>import java.math.BigInteger; |
||
import java.util.*; |
import java.util.*; |
||
import static java.util.Arrays.asList; |
import static java.util.Arrays.asList; |
||
Line 2,175: | Line 2,175: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>Rows: |
<pre>Rows: |
||
Line 2,197: | Line 2,197: | ||
===Solution 1=== |
===Solution 1=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=JavaScript> |
||
(function () { |
(function () { |
||
var cache = [ |
var cache = [ |
||
Line 2,258: | Line 2,258: | ||
}); |
}); |
||
})() |
})() |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Solution 2=== |
===Solution 2=== |
||
Clean and straightforward solution |
Clean and straightforward solution |
||
< |
<syntaxhighlight lang=javascript> |
||
function genTriangle(n){ // O(n^3) time and O(n^2) space |
function genTriangle(n){ // O(n^3) time and O(n^2) space |
||
var a = new Array(n) |
var a = new Array(n) |
||
Line 2,300: | Line 2,300: | ||
console.log("G(" + x + ") = " + G(x)) |
console.log("G(" + x + ") = " + G(x)) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,336: | Line 2,336: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang=julia> |
||
using Combinatorics, StatsBase |
using Combinatorics, StatsBase |
||
Line 2,376: | Line 2,376: | ||
end |
end |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
[1] |
[1] |
||
[1, 1] |
[1, 1] |
||
Line 2,418: | Line 2,418: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Swift}} |
{{trans|Swift}} |
||
< |
<syntaxhighlight lang=scala>import java.lang.Math.min |
||
import java.math.BigInteger |
import java.math.BigInteger |
||
import java.util.ArrayList |
import java.util.ArrayList |
||
Line 2,452: | Line 2,452: | ||
System.out.printf("%s %s%n", it, c[c.size - 1]) |
System.out.printf("%s %s%n", it, c[c.size - 1]) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 2,490: | Line 2,490: | ||
=={{header|Lasso}}== |
=={{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". |
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". |
||
< |
<syntaxhighlight lang=Lasso>define cumu(n::integer) => { |
||
loop(-from=$cache->size,-to=#n+1) => { |
loop(-from=$cache->size,-to=#n+1) => { |
||
local(r = array(0), l = loop_count) |
local(r = array(0), l = loop_count) |
||
Line 2,520: | Line 2,520: | ||
cumu(#x+1)->last |
cumu(#x+1)->last |
||
'\r' |
'\r' |
||
^}</ |
^}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,557: | Line 2,557: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang=lua>function nog(n) |
||
local tri = {{1}} |
local tri = {{1}} |
||
for r = 2, n do |
for r = 2, n do |
||
Line 2,579: | Line 2,579: | ||
end |
end |
||
print("G(23) = " .. G(23)) |
print("G(23) = " .. G(23)) |
||
print("G(123) = " .. G(123))</ |
print("G(123) = " .. G(123))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1: 1 |
<pre>1: 1 |
||
Line 2,610: | Line 2,610: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang=maple>TriangleLine(n) := map(rhs, Statistics :- Tally(map(x -> x[-1], combinat:-partition(n)))): |
||
Triangle := proc(m) |
Triangle := proc(m) |
||
local i; |
local i; |
||
Line 2,617: | Line 2,617: | ||
end do |
end do |
||
end proc: |
end proc: |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,631: | Line 2,631: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=mathematica>Table[Last /@ Reverse@Tally[First /@ IntegerPartitions[n]], {n, 10}] // Grid</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 |
<pre>1 |
||
Line 2,645: | Line 2,645: | ||
Here I use the bulit-in function PartitionsP to calculate <math>P(n)</math>. |
Here I use the bulit-in function PartitionsP to calculate <math>P(n)</math>. |
||
< |
<syntaxhighlight lang=mathematica>PartitionsP /@ {23, 123, 1234, 12345}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{1255, 2552338241, 156978797223733228787865722354959930, 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736}</pre> |
<pre>{1255, 2552338241, 156978797223733228787865722354959930, 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736}</pre> |
||
< |
<syntaxhighlight lang=mathematica>DiscretePlot[PartitionsP[n], {n, 1, 999}, PlotRange -> All]</syntaxhighlight> |
||
[[File:9 billion names of God the integer Mathematica.png]] |
[[File:9 billion names of God the integer Mathematica.png]] |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang=maxima>for n thru 25 do print(makelist(length(integer_partitions(n-k,k)),k,1,n))$</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1] |
<pre>[1] |
||
Line 2,682: | Line 2,682: | ||
Using the built-in function to calculate <math>P(n)</math>: |
Using the built-in function to calculate <math>P(n)</math>: |
||
< |
<syntaxhighlight lang=maxima>makelist(num_partitions(n),n,[23,123,1234,12345]);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(%o1) [1255, 2552338241, 156978797223733228787865722354959930, 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736]</pre> |
<pre>(%o1) [1255, 2552338241, 156978797223733228787865722354959930, 69420357953926116819562977205209384460667673094671463620270321700806074195845953959951425306140971942519870679768681736]</pre> |
||
Line 2,688: | Line 2,688: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=nim>import bigints |
||
var cache = @[@[1.initBigInt]] |
var cache = @[@[1.initBigInt]] |
||
Line 2,713: | Line 2,713: | ||
for x in [23, 123, 1234, 12345]: |
for x in [23, 123, 1234, 12345]: |
||
let c = cumu(x) |
let c = cumu(x) |
||
echo x, " ", c[c.high]</ |
echo x, " ", c[c.high]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>@[1] |
<pre>@[1] |
||
Line 2,733: | Line 2,733: | ||
Faster version: |
Faster version: |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=nim>import bigints |
||
var p = @[1.initBigInt] |
var p = @[1.initBigInt] |
||
Line 2,765: | Line 2,765: | ||
let p = partitions(i) |
let p = partitions(i) |
||
if i in ns: |
if i in ns: |
||
echo i,": ",p</ |
echo i,": ",p</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>23: 1255 |
<pre>23: 1255 |
||
Line 2,773: | Line 2,773: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang=ocaml> |
||
let get, sum_unto = |
let get, sum_unto = |
||
let cache = ref [||] |
let cache = ref [||] |
||
Line 2,852: | Line 2,852: | ||
end |
end |
||
[23;123;1234;12345;123456] |
[23;123;1234;12345;123456] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,898: | Line 2,898: | ||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
< |
<syntaxhighlight lang=scheme> |
||
(define (nine-billion-names row column) |
(define (nine-billion-names row column) |
||
(cond |
(cond |
||
Line 2,923: | Line 2,923: | ||
(print-triangle 25) |
(print-triangle 25) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 2,955: | Line 2,955: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight 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(n)=for(k=1,n,print(row(k))); |
||
show(25) |
show(25) |
||
apply(numbpart, [23,123,1234,12345]) |
apply(numbpart, [23,123,1234,12345]) |
||
plot(x=1,999.9, numbpart(x\1))</ |
plot(x=1,999.9, numbpart(x\1))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1] |
<pre>[1] |
||
Line 3,017: | Line 3,017: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang=perl>use ntheory qw/:all/; |
||
sub triangle_row { |
sub triangle_row { |
||
Line 3,028: | Line 3,028: | ||
printf "%2d: %s\n", $_, join(" ",triangle_row($_)) for 1..25; |
printf "%2d: %s\n", $_, join(" ",triangle_row($_)) for 1..25; |
||
print "\n"; |
print "\n"; |
||
say "P($_) = ", partitions($_) for (23, 123, 1234, 12345);</ |
say "P($_) = ", partitions($_) for (23, 123, 1234, 12345);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[rows are the same as below] |
[rows are the same as below] |
||
Line 3,037: | Line 3,037: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang=perl> |
||
use strict; |
use strict; |
||
use warnings; |
use warnings; |
||
Line 3,078: | Line 3,078: | ||
print $i, "\n"; |
print $i, "\n"; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,117: | Line 3,117: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang=Phix>(phixonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo\rosetta\9billionnames.exw</span> |
<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> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
Line 3,150: | Line 3,150: | ||
<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> |
<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> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,182: | Line 3,182: | ||
{{trans|C}} |
{{trans|C}} |
||
{{libheader|Phix/mpfr}} |
{{libheader|Phix/mpfr}} |
||
<!--< |
<!--<syntaxhighlight lang=Phix>(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 3,214: | Line 3,214: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,226: | Line 3,226: | ||
=== Third and last, a simple plot === |
=== Third and last, a simple plot === |
||
{{libheader|Phix/pGUI}} |
{{libheader|Phix/pGUI}} |
||
<!--< |
<!--<syntaxhighlight lang=Phix>--> |
||
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span> |
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span> |
||
Line 3,258: | Line 3,258: | ||
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
{{trans|Yabasic}} |
{{trans|Yabasic}} |
||
< |
<syntaxhighlight lang=Phixmonti>/# Rosetta Code problem: http://rosettacode.org/wiki/9_billion_names_of_God_the_integer |
||
by Galileo, 05/2022 #/ |
by Galileo, 05/2022 #/ |
||
Line 3,291: | Line 3,291: | ||
enddef |
enddef |
||
20 nine_billion_names</ |
20 nine_billion_names</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 |
<pre> 1 |
||
Line 3,319: | Line 3,319: | ||
===The triangle, using constraint modelling=== |
===The triangle, using constraint modelling=== |
||
Using constraint modeling to generate all the partitions 1..25. |
Using constraint modeling to generate all the partitions 1..25. |
||
< |
<syntaxhighlight lang=Picat>import cp. |
||
main => |
main => |
||
Line 3,345: | Line 3,345: | ||
foreach(I in L) |
foreach(I in L) |
||
Map.put(I,Map.get(I,0)+1) |
Map.put(I,Map.get(I,0)+1) |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,378: | Line 3,378: | ||
===Number of partitions=== |
===Number of partitions=== |
||
This is the Picat solution of [http://rosettacode.org/wiki/Partition_function_P Partition_function_P]. |
This is the Picat solution of [http://rosettacode.org/wiki/Partition_function_P Partition_function_P]. |
||
< |
<syntaxhighlight lang=Picat>% Number of partitions |
||
go2 => |
go2 => |
||
foreach(N in [23,123,1234,12345,123456]) |
foreach(N in [23,123,1234,12345,123456]) |
||
Line 3,403: | Line 3,403: | ||
M := (K*(3*K+1)) // 2 |
M := (K*(3*K+1)) // 2 |
||
end, |
end, |
||
P = S.</ |
P = S.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,414: | Line 3,414: | ||
===Recursion=== |
===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). |
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 |
table |
||
pc(_,0) = 1. |
pc(_,0) = 1. |
||
pc(1,1) = 1. |
pc(1,1) = 1. |
||
pc(K,M) = cond(M < K, 0, pc(K, M-K) + pc(K + 1,M)).</ |
pc(K,M) = cond(M < K, 0, pc(K, M-K) + pc(K + 1,M)).</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=PicoLisp>(de row (N) |
||
(let C '((1)) |
(let C '((1)) |
||
(do N |
(do N |
||
Line 3,473: | Line 3,473: | ||
(println (sumr I)) ) ) |
(println (sumr I)) ) ) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,510: | Line 3,510: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=Pike>array cumu(int n) { |
||
array(array(int)) cache = ({({1})}); |
array(array(int)) cache = ({({1})}); |
||
Line 3,548: | Line 3,548: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} Not wait for "12345" output. |
{{out}} Not wait for "12345" output. |
||
<pre>rows: |
<pre>rows: |
||
Line 3,569: | Line 3,569: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang=purebasic> |
||
Define nMax.i=25, n.i, k.i |
Define nMax.i=25, n.i, k.i |
||
Dim pfx.s(1) |
Dim pfx.s(1) |
||
Line 3,638: | Line 3,638: | ||
PrintN(sum(12345,pfx())) |
PrintN(sum(12345,pfx())) |
||
Input() |
Input() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,674: | Line 3,674: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang=python>cache = [[1]] |
||
def cumu(n): |
def cumu(n): |
||
for l in range(len(cache), n+1): |
for l in range(len(cache), n+1): |
||
Line 3,692: | Line 3,692: | ||
print "\nsums:" |
print "\nsums:" |
||
for x in [23, 123, 1234, 12345]: print x, cumu(x)[-1]</ |
for x in [23, 123, 1234, 12345]: print x, cumu(x)[-1]</syntaxhighlight> |
||
{{out}} (I didn't actually wait long enough to see what the sum for 12345 is) |
{{out}} (I didn't actually wait long enough to see what the sum for 12345 is) |
||
<pre> |
<pre> |
||
Line 3,714: | Line 3,714: | ||
</pre> |
</pre> |
||
To calculate partition functions only: |
To calculate partition functions only: |
||
< |
<syntaxhighlight lang=python>def partitions(N): |
||
diffs,k,s = [],1,1 |
diffs,k,s = [],1,1 |
||
while k * (3*k-1) < 2*N: |
while k * (3*k-1) < 2*N: |
||
Line 3,731: | Line 3,731: | ||
p = partitions(12345) |
p = partitions(12345) |
||
for x in [23,123,1234,12345]: print x, p[x]</ |
for x in [23,123,1234,12345]: print x, p[x]</syntaxhighlight> |
||
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: |
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}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang=python>def partitions(n): |
||
partitions.p.append(0) |
partitions.p.append(0) |
||
Line 3,772: | Line 3,772: | ||
print "%6d: %s" % (i, p) |
print "%6d: %s" % (i, p) |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 23: 1255 |
<pre> 23: 1255 |
||
Line 3,780: | Line 3,780: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=racket>#lang racket |
||
(define (cdr-empty ls) (if (empty? ls) empty (cdr ls))) |
(define (cdr-empty ls) (if (empty? ls) empty (cdr ls))) |
||
Line 3,803: | Line 3,803: | ||
(newline) |
(newline) |
||
(map G '(23 123 1234))) |
(map G '(23 123 1234))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,840: | Line 3,840: | ||
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.) |
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=perl6>my @todo = $[1]; |
||
my @sums = 0; |
my @sums = 0; |
||
sub nextrow($n) { |
sub nextrow($n) { |
||
Line 3,873: | Line 3,873: | ||
for 23, 123, 1234, 12345 { |
for 23, 123, 1234, 12345 { |
||
put $_, "\t", @names-of-God[$_]; |
put $_, "\t", @names-of-God[$_]; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>rows: |
<pre>rows: |
||
Line 3,909: | Line 3,909: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang=Rebol> |
||
Red [] |
Red [] |
||
Line 3,955: | Line 3,955: | ||
probe names 1234 |
probe names 1234 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,024: | Line 4,024: | ||
</big> |
</big> |
||
which is derived from Euler's generating function. |
which is derived from Euler's generating function. |
||
< |
<syntaxhighlight lang=rexx>/*REXX program generates and displays a number triangle for partitions of a number. */ |
||
numeric digits 400 /*be able to handle larger numbers. */ |
numeric digits 400 /*be able to handle larger numbers. */ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
Line 4,081: | Line 4,081: | ||
else $= $ - x - y /* " " " " " " even.*/ |
else $= $ - x - y /* " " " " " " even.*/ |
||
end /*k*/ /* [↑] Euler's recursive func.*/ |
end /*k*/ /* [↑] Euler's recursive func.*/ |
||
@.n= $; return $ /*use memoization; return num.*/</ |
@.n= $; return $ /*use memoization; return num.*/</syntaxhighlight> |
||
{{out|output|text= when using the default input (of 25 rows):}} |
{{out|output|text= when using the default input (of 25 rows):}} |
||
<pre> |
<pre> |
||
Line 4,141: | Line 4,141: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
===Naive Solution=== |
===Naive Solution=== |
||
< |
<syntaxhighlight lang=ruby> |
||
# Generate IPF triangle |
# Generate IPF triangle |
||
# Nigel_Galloway: May 1st., 2013. |
# Nigel_Galloway: May 1st., 2013. |
||
Line 4,152: | Line 4,152: | ||
puts (1..n).map {|g| "%4s" % g(n,g)}.join |
puts (1..n).map {|g| "%4s" % g(n,g)}.join |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,183: | Line 4,183: | ||
===Full Solution=== |
===Full Solution=== |
||
< |
<syntaxhighlight lang=ruby> |
||
# Find large values of IPF |
# Find large values of IPF |
||
# Nigel_Galloway: May 1st., 2013. |
# Nigel_Galloway: May 1st., 2013. |
||
Line 4,214: | Line 4,214: | ||
n = 3 + @ipn1.inject(:+) + @ipn2.inject(:+) |
n = 3 + @ipn1.inject(:+) + @ipn2.inject(:+) |
||
puts "G(12345) = #{n}" |
puts "G(12345) = #{n}" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,225: | Line 4,225: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=rust>extern crate num; |
||
use std::cmp; |
use std::cmp; |
||
Line 4,269: | Line 4,269: | ||
println!("{}: {}", x, s); |
println!("{}: {}", x, s); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>rows: |
<pre>rows: |
||
Line 4,306: | Line 4,306: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Naive Solution=== |
===Naive Solution=== |
||
< |
<syntaxhighlight lang=scala> |
||
object Main { |
object Main { |
||
Line 4,343: | Line 4,343: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,386: | Line 4,386: | ||
===Full Solution=== |
===Full Solution=== |
||
< |
<syntaxhighlight lang=Scala>val cache = new Array[BigInt](15000) |
||
cache(0) = 1 |
cache(0) = 1 |
||
val cacheNaive = scala.collection.mutable.Map[Tuple2[Int, Int], BigInt]() |
val cacheNaive = scala.collection.mutable.Map[Tuple2[Int, Int], BigInt]() |
||
Line 4,434: | Line 4,434: | ||
println(quickPartitions(123)) |
println(quickPartitions(123)) |
||
println(quickPartitions(1234)) |
println(quickPartitions(1234)) |
||
println(quickPartitions(12345))</ |
println(quickPartitions(12345))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 |
<pre> 1 |
||
Line 4,465: | Line 4,465: | ||
=={{header|scheme}}== |
=={{header|scheme}}== |
||
< |
<syntaxhighlight lang=scheme>(define (f m n) |
||
(define (sigma g x y) |
(define (sigma g x y) |
||
(define (sum i) |
(define (sum i) |
||
Line 4,484: | Line 4,484: | ||
(cond ((< i x) (begin (display (line i)) (display "\n") (print-loop (+ i 1)) )))) |
(cond ((< i x) (begin (display (line i)) (display "\n") (print-loop (+ i 1)) )))) |
||
(print-loop 1)) |
(print-loop 1)) |
||
(print 25)</ |
(print 25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,515: | Line 4,515: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang=ruby>var cache = [[1]] |
||
func cumu (n) { |
func cumu (n) { |
||
Line 4,542: | Line 4,542: | ||
for i in [23, 123, 1234, 12345] { |
for i in [23, 123, 1234, 12345] { |
||
"%2s : %4s\n".printf(i, cumu(i)[-1]) |
"%2s : %4s\n".printf(i, cumu(i)[-1]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,571: | Line 4,571: | ||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
< |
<syntaxhighlight lang=spl>'print triangle |
||
> n, 1..25 |
> n, 1..25 |
||
k = 50-n*2 |
k = 50-n*2 |
||
Line 4,608: | Line 4,608: | ||
< |
< |
||
<= p[n+1] |
<= p[n+1] |
||
.</ |
.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,644: | Line 4,644: | ||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang=stata>mata |
||
function part(n) { |
function part(n) { |
||
a = J(n,n,.) |
a = J(n,n,.) |
||
Line 4,653: | Line 4,653: | ||
return(a) |
return(a) |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
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 [[oeis:A008284|OEIS A008284]] and row sums with [[oeis:A000041|OEIS A000041]]. |
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 [[oeis:A008284|OEIS A008284]] and row sums with [[oeis:A000041|OEIS A000041]]. |
||
Line 4,683: | Line 4,683: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=Swift>var cache = [[1]] |
||
func namesOfGod(n:Int) -> [Int] { |
func namesOfGod(n:Int) -> [Int] { |
||
for l in cache.count...n { |
for l in cache.count...n { |
||
Line 4,716: | Line 4,716: | ||
var numInt = array[array.count - 1] |
var numInt = array[array.count - 1] |
||
println("\(x): \(numInt)") |
println("\(x): \(numInt)") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,755: | Line 4,755: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=tcl>set cache 1 |
||
proc cumu {n} { |
proc cumu {n} { |
||
global cache |
global cache |
||
Line 4,784: | Line 4,784: | ||
foreach x {23 123 1234 12345} { |
foreach x {23 123 1234 12345} { |
||
puts "${x}: [lindex [cumu $x] end]" |
puts "${x}: [lindex [cumu $x] end]" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,833: | Line 4,833: | ||
Print |
Print |
||
Return</ |
Return</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 1 |
<pre> 1 |
||
Line 4,853: | Line 4,853: | ||
0 OK, 0:30 </pre> |
0 OK, 0:30 </pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang=vb>Public Sub nine_billion_names() |
||
Dim p(25, 25) As Long |
Dim p(25, 25) As Long |
||
p(1, 1) = 1 |
p(1, 1) = 1 |
||
Line 4,868: | Line 4,868: | ||
Debug.Print |
Debug.Print |
||
Next i |
Next i |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre> 1 |
<pre> 1 |
||
1 1 |
1 1 |
||
Line 4,891: | Line 4,891: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang=vlang>import math.big |
||
fn int_min(a int, b int) int { |
fn int_min(a int, b int) int { |
||
Line 4,936: | Line 4,936: | ||
println("$num ${r[r.len-1]}") |
println("$num ${r[r.len-1]}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,962: | Line 4,962: | ||
{{libheader|Wren-big}} |
{{libheader|Wren-big}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang=ecmascript>import "/big" for BigInt |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 4,994: | Line 4,994: | ||
[23, 123, 1234, 12345].each { |i| |
[23, 123, 1234, 12345].each { |i| |
||
Fmt.print("$5s: $s", i, cumu.call(i)[-1]) |
Fmt.print("$5s: $s", i, cumu.call(i)[-1]) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,034: | Line 5,034: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|VBA}} |
{{trans|VBA}} |
||
< |
<syntaxhighlight lang=Yabasic>clear screen |
||
Sub nine_billion_names(rows) |
Sub nine_billion_names(rows) |
||
Line 5,054: | Line 5,054: | ||
End Sub |
End Sub |
||
nine_billion_names(20)</ |
nine_billion_names(20)</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|C}} |
{{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. |
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. |
||
< |
<syntaxhighlight lang=zkl>var [const] BN=Import.lib("zklBigNum"); |
||
const N=0d100_000; |
const N=0d100_000; |
||
Line 5,075: | Line 5,075: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang=zkl>idx:=T(23, 123, 1234, 12345, 20000, 30000, 40000, 50000, N); |
||
p[0].set(1); |
p[0].set(1); |
||
Line 5,082: | Line 5,082: | ||
(1).pump(i,Void,calc.fp1(p)); // for n in [1..i] do calc(n,p) |
(1).pump(i,Void,calc.fp1(p)); // for n in [1..i] do calc(n,p) |
||
"%2d:\t%d".fmt(i,p[i]).println(); |
"%2d:\t%d".fmt(i,p[i]).println(); |
||
}</ |
}</syntaxhighlight> |
||
The .fp/.fp1 methods create a closure, fixing the first or second parameter. |
The .fp/.fp1 methods create a closure, fixing the first or second parameter. |
||
{{out}} |
{{out}} |