Paraffins: Difference between revisions

34,101 bytes added ,  5 months ago
m
m (included the full name for the OEIS entry.)
m (→‎{{header|Wren}}: Minor tidy)
 
(17 intermediate revisions by 9 users not shown)
Line 6:
 
;Task:
Enumerate, without repetitions and in order of increasing size, all possible paraffin molecules (also known as [[wp:alkane|alkane]]s).
 
 
Paraffins are built up using only carbon atoms, which has four bonds, and hydrogen, which has one bond.   All bonds for each atom must be used, so it is easiest to think of an alkane as linked carbon atoms forming the "backbone" structure, with adding hydrogen atoms linking the remaining unused bonds.
 
In a paraffin, one is allowed neither double bonds (two bonds between the same pair of atoms), nor cycles of linked carbons. &nbsp; So all paraffins with &nbsp; '''n''' &nbsp; carbon atoms share the empirical formula &nbsp; &nbsp; <big>C<sub>n</sub>H<sub>2n+2</sub></big>
 
But for all &nbsp; '''n''' ≥ 4 &nbsp; there are several distinct molecules ("isomers") with the same formula but different structures.
Line 17:
The number of isomers rises rather rapidly when &nbsp; '''n''' &nbsp; increases.
 
In counting isomers it should be borne in mind that the four bond positions on a given carbon atom can be freely interchanged and bonds rotated (including 3-D "out of the paper" rotations when it's being observed on a flat diagram), &nbsp; so rotations or re-orientations of parts of the molecule (without breaking bonds) do not give different isomers. &nbsp; So what seem at first to be different molecules may in fact turn out to be different orientations of the same molecule.
 
 
Line 35:
 
The sequence of those results is visible in the OEIS entry: &nbsp;
::: &nbsp; [[oeis:A00602|A00602: number of n-node unrooted quartic trees; number of n-carbon alkanes C(n)H(2n+2) ignoring stereoisomers]].
 
The sequence is (the index starts from zero, and represents the number of carbon atoms):
Line 50:
A flat 1D representation, with arrays or lists is enough, for instance:
 
<langsyntaxhighlight haskelllang="text">*Main> all_paraffins 1
[CCP H H H H]
*Main> all_paraffins 2
[BCP (C H H H) (C H H H)]
*Main> all_paraffins 3
[CCP H H (C H H H) (C H H H)]
*Main> all_paraffins 4
[BCP (C H H (C H H H)) [BCP (C H H (C H H H)),CCP H (C H H H) (C H H H)),
CCP H (C H H H) (C H H H) (C H H H)]
(C H H H)]
*Main> all_paraffins 5
[CCP H H (C H H (C H H H)) (C H H (C H H H)),CCP H (C H H H)
(C H H H) (C H CCP H (C H H H)),CCP (C H H H) (C H H H) (C H H H)),
CCP (C H H H) (C H H H) (C H H H) (C H H H)]
(C H H H)]
*Main> all_paraffins 6
[BCP (C H H (C H H (C H H H))) (C H H (C H H (C H H H))),BCP
BCP (C H H (C H H (C H H H))) (C H (C H H H) (C H H H)),BCP (C H
BCP (C H (C H H H) (C H H H)) (C H (C H H H) (C H H H)),CCP H (C H H H)
(C H CCP H (C H H H)) (C H H (C H H H)),CCP (C H H H) (C H H H)),
CCP (C H H H) (C H H H) (C H H H) (C H H (C H H H))]</langsyntaxhighlight>
 
Showing a basic 2D ASCII-art representation of the paraffins is better; for instance (molecule names aren't necessary):
<presyntaxhighlight lang="text"> Methane methane ethane Ethane propane Propane Isobutaneisobutane
H H H H H H H H H
| | | | | | | | | │ │ │
H - C - H H - C - C - H H - C - C - C - H H - C - C - C - H
| | | | | | | | | │ │ │
H H H H H H H | H
|
H - C - H
|
H</presyntaxhighlight>
 
 
;Links:
* &nbsp; A paper that explains the problem and its solution in a functional language:
http://www.cs.wright.edu/~tkprasad/courses/cs776/paraffins-turner.pdf
 
* &nbsp; A Haskell implementation:
https://github.com/ghc/nofib/blob/master/imaginary/paraffins/Main.hs
 
* &nbsp; A Scheme implementation:
http://www.ccs.neu.edu/home/will/Twobit/src/paraffins.scm
 
* &nbsp; A Fortress implementation: &nbsp; &nbsp; &nbsp; &nbsp; (this site has been closed)
http://java.net/projects/projectfortress/sources/sources/content/ProjectFortress/demos/turnersParaffins0.fss?rev=3005
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V nMax = 250
V nBranches = 4
V rooted = [BigInt(0)] * (nMax + 1)
V unrooted = [BigInt(0)] * (nMax + 1)
rooted[0] = BigInt(1)
rooted[1] = BigInt(1)
unrooted[0] = BigInt(1)
unrooted[1] = BigInt(1)
 
F choose(m, k)
I k == 1
R m
V result = m
L(i) 1 .< k
result = result * (m + i) I/ (i + 1)
R result
 
F tree(br, n, l, sum, cnt)
V s = 0
L(b) br + 1 .. :nBranches
s = sum + (b - br) * n
I s > :nMax {R}
 
V c = choose(:rooted[n], b - br) * cnt
 
I l * 2 < s {:unrooted[s] += c}
I b == :nBranches {R}
:rooted[s] += c
L(m) (n - 1 .< 0).step(-1)
tree(b, m, l, s, c)
 
F bicenter(s)
I (s [&] 1) == 0
:unrooted[s] += :rooted[s I/ 2] * (:rooted[s I/ 2] + 1) I/ 2
 
L(n) 1 .. nMax
tree(0, n, n, 1, BigInt(1))
bicenter(n)
print(n‘: ’unrooted[n])</syntaxhighlight>
 
{{out}}
<pre>
1: 1
2: 1
3: 1
4: 2
5: 3
6: 5
7: 9
8: 18
9: 35
10: 75
...
244: 34576004768296889785887066794910718730985852896707505707076422305798138880427343561380451664648670542260
245: 96356944442415066997623733664230869603611716312377642117711752082444867783045964116385053282421589905891
246: 268540209617944059776303921971316267806082307732727328919911735252505071315985779867844056236080213000010
247: 748434113260252449609376828666343341456610378512725586135955779939163320693444008584504390382026391947130
248: 2086006351917005252913566124773054331962205157167696706926185063169623907656246841866717933958839366769700
249: 5814271898167303040368103945830220447130073898083466852225709084407144308593691069932064987528870826155297
250: 16206624309085062837751018464745815688226709117091506494175397665527493805947344857313038875654104100026504
</pre>
 
=={{header|C}}==
Can't show the tree shapes; count only.
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define MAX_N 33 /* max number of tree nodes */
Line 214 ⟶ 277:
 
return 0;
}</langsyntaxhighlight>Same idea, with GMP, and done somewhat differently:<langsyntaxhighlight lang="c">#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>
Line 319 ⟶ 382:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iostream>
#include <vector>
 
const int32_t MAX_TREE_NODES = 52;
const int32_t MAX_BRANCHES = 4;
 
std::vector<uint64_t> rooted(MAX_TREE_NODES + 1, 0);
std::vector<uint64_t> unrooted(MAX_TREE_NODES + 1,0);
std::vector<uint64_t> count(MAX_BRANCHES, 0);
 
void tree(const int32_t& branches, const int32_t& radius, const int32_t& combinations,
const int32_t& previous_nodes, const uint64_t& branches_count) {
int32_t nodes = previous_nodes;
for ( int32_t branch = branches + 1; branch <= MAX_BRANCHES; ++branch ) {
nodes += radius;
 
if ( nodes > MAX_TREE_NODES || ( combinations * 2 >= nodes && branch >= MAX_BRANCHES ) ) {
return;
}
 
if ( branch == branches + 1 ) {
count[branches] = rooted[radius] * branches_count;
} else {
count[branches] *= ( rooted[radius] + branch - branches - 1 );
count[branches] /= ( branch - branches );
}
 
if ( combinations * 2 < nodes ) {
unrooted[nodes] += count[branches];
}
 
if ( branch < MAX_BRANCHES ) {
rooted[nodes] += count[branches];
}
 
for ( int32_t next_radius = radius - 1; next_radius > 0; --next_radius ) {
tree(branch, next_radius, combinations, nodes, count[branches]);
}
}
}
 
void bicenter(const int32_t& node) {
if ( ( node & 1 ) == 0 ) {
const uint64_t temp = ( rooted[node / 2] + 1 ) * rooted[node / 2];
unrooted[node] += temp / 2;
}
}
 
int main() {
rooted[0] = rooted[1] = 1;
unrooted[0] = unrooted[1] = 1;
 
for ( int32_t node = 1; node <= MAX_TREE_NODES; ++node ) {
tree(0, node, node, 1, 1);
bicenter(node);
std::cout << node << ": " << unrooted[node] << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
1: 1
2: 1
3: 1
4: 2
5: 3
 
// elided
 
48: 156192366474590639
49: 417612400765382272
50: 1117743651746953270
51: 2994664179967370611
52: 8031081780535296591
</pre>
=={{header|D}}==
{{trans|Go}}
<langsyntaxhighlight lang="d">import std.stdio, std.bigint;
 
enum uint nMax = 250;
Line 366 ⟶ 508:
writeln(n, ": ", unrooted[n]);
}
}</langsyntaxhighlight>
{{out}}
<pre>1: 1
Line 399 ⟶ 541:
{{trans|Pascal}}
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' version 31-12-2016
' compile with: fbc -s console
' uses gmp, translation from pascal
Line 498 ⟶ 640:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
<pre style="height:35ex;overflow:scroll"> 1: 1
2: 1
Line 1,002 ⟶ 1,144:
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,062 ⟶ 1,204:
fmt.Printf("%d: %d\n", n, &unrooted[n])
}
}</langsyntaxhighlight>
Output: (trimmed)
<pre>
Line 1,113 ⟶ 1,255:
=={{header|Haskell}}==
Using formula from OEIS page, similar to the Mathematica entry below:
<langsyntaxhighlight lang="haskell">-- polynomial utils
a `nmul` n = map (*n) a
a `ndiv` n = map (`div` n) a
Line 1,156 ⟶ 1,298:
a602 = a678 - a599 + x2
 
main = mapM_ print $ take 200 $ zip [0 ..] a602</langsyntaxhighlight>
 
Counting trees with some fairly primitive caching:
<langsyntaxhighlight lang="haskell">import Data.Array
 
choose :: Integer -> Int -> Integer
Line 1,190 ⟶ 1,332:
| otherwise = x * (x + 1) `div` 2 where x = build_block (n `div` 2)
 
main = mapM_ print $ map (\x->(x, unrooted x)) [1..max_nodes]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,215 ⟶ 1,357:
The following code is an interpretation of the Haskell program listed in the links above.
 
<langsyntaxhighlight lang="j">part3=: ;@((<@([(],.(-+/"1))],.]+i.@(]-~1+<.@-:@-))"0 i.@>:@<.@%&3))
 
part4=: 3 :0
Line 1,246 ⟶ 1,388:
)
 
NofParaff=: {. radGenN ((ccpGenN +:) + bcpGenN ) 2&|+<.@-:</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang="j"> 6 6 $ NofParaff 36
1 1 1 1 2 3
5 9 18 35 75 159
Line 1,254 ⟶ 1,396:
60523 148284 366319 910726 2278658 5731580
14490245 36797588 93839412 240215803 617105614 1590507121
4111846763 10660307791 27711253769 72214088660 188626236139 493782952902</langsyntaxhighlight>
 
=={{header|Java}}==
Translation of [[Paraffins#C|C]] via [[Paraffins#Go|Go]] and [[Paraffins#D|D]]
<langsyntaxhighlight lang="java">import java.math.BigInteger;
import java.util.Arrays;
 
Line 1,316 ⟶ 1,458:
}
}
}</langsyntaxhighlight>
<pre>1: 1
2: 1
Line 1,338 ⟶ 1,480:
Currently jq uses IEEE 754 64-bit numbers. Large integers are approximated by floats and therefore the results generated by the program presented here are only precise for n up to and including 45.
 
<langsyntaxhighlight lang="jq">def MAX_N: 500; # imprecision begins at 46
def BRANCH: 4;
 
Line 1,402 ⟶ 1,544:
;
 
paraffins</langsyntaxhighlight>
'''Output''' (trimmed):
<pre style="height:35ex;overflow:scroll">$ jq -M -n -c -f paraffins.jq
Line 1,460 ⟶ 1,602:
{{works with|jq|>1.4}}
The following is a more elegant alternative to paraffins/0 as defined above but requires "foreach":
<langsyntaxhighlight lang="jq">def paraffins:
foreach range(1; MAX_N) as $n
( [unrooted, ra];
Line 1,466 ⟶ 1,608:
[$n, .[0][$n]]
)
;</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Go}}
Output is the same as the Go version.
<langsyntaxhighlight lang="julia">const branches = 4
const nmax = 500
 
Line 1,513 ⟶ 1,655:
 
paraffins()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.4-3
 
import java.math.BigInteger
Line 1,564 ⟶ 1,706:
println("$n: ${unrooted[n]}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,571 ⟶ 1,713:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{works with|Mathematica|9.0}}
Using the formula on OEIS.
<langsyntaxhighlight lang="mathematica">s[m_, p_, n_] :=
CycleIndexPolynomial[SymmetricGroup[m],
Table[ComposeSeries[p, x^i + O[x]^(n + 1)], {i, m}]];
Line 1,583 ⟶ 1,725:
A000602[n_] := SeriesCoefficient[G000602[n], n];
A000602List[n_] := CoefficientList[G000602[n], x];
Grid@Transpose@{Range[0, 200], A000602List@200}</langsyntaxhighlight>
{{Out}}
<pre>0 1
Line 1,605 ⟶ 1,747:
=={{header|Nim}}==
{{trans|C}}
<lang nim>import {{libheader|bigints}}
<syntaxhighlight lang="nim">import bigints
 
const
Line 1,618 ⟶ 1,761:
unrooted[i] = 0.initBigInt
 
proc choose(m,: BigInt; k: int32): BigInt =
result = m
if k == 1: return
for i in 1 .. < k:
result = result * (m + i) div (i + 1)
 
proc tree(br, n, l, sum: int32,; cnt: BigInt) =
var s: int32 = 0
for b in br + 1 .. nBranches:
Line 1,638 ⟶ 1,781:
tree b, m, l, s, c
 
proc bicenter(s: int32) =
if (s and 1) == 0:
unrooted[s] += rooted[s div 2] * (rooted[s div 2] + 1) div 2
Line 1,645 ⟶ 1,788:
tree 0, n, n, 1, 1.initBigInt
n.bicenter
echo n, ": ", unrooted[n]</langsyntaxhighlight>
 
Output:
{{out}}
<pre>1: 1
2: 1
Line 1,675 ⟶ 1,819:
 
This function is for recent PARI/GP:
<langsyntaxhighlight lang="parigp">paraffin(p) =
{
local (P = p+1, R, U = R = Vec([1,1], P));
Line 1,689 ⟶ 1,833:
if (n % 2,, U[n+1] += R[n/2+1] * (R[n/2+1]+1)/2);
print([n, U[n+1]]))
}</langsyntaxhighlight>
 
 
Code for older version of PARI/GP < 2.9:
<langsyntaxhighlight lang="parigp">iso(B,n,C,S,l=n) =
{
my (b,c,i,s);
Line 1,711 ⟶ 1,855:
if (n % 2,, U[n+1] += R[n/2+1] * (R[n/2+1]+1)/2);
print([n, U[n+1]]))
}</langsyntaxhighlight>
 
Output:<pre>paraffin(32)
Line 1,753 ⟶ 1,897:
{{libheader|GMP}}
Conversion of the C example:
<langsyntaxhighlight lang="pascal">Program Paraffins;
 
uses
Line 1,840 ⟶ 1,984:
mp_printf('%d: %Zd'+chr(13)+chr(10), n, @unrooted[n]);
end;
end.</langsyntaxhighlight>
Output (trimmed):
<pre>1: 1
Line 1,887 ⟶ 2,031:
{{works with|Free_Pascal}}
This method of counting alkanes is based on a paper by Shinsaku Fujita (see program for reference). Multi-precision is not used, so alkanes are counted only up to order 49. The results are identical to those from the REXX program.
<langsyntaxhighlight lang="pascal">
program CountAlkanes;
 
Line 1,998 ⟶ 2,142:
WriteLn( SysUtils.Format( '%2d %d', [k, nrAlkanes[k]]));
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,016 ⟶ 2,160:
{{trans|C}}
This is using Math::GMPz for best performance. Math::GMP works almost as well. Math::BigInt is in core and only 9 times slower for this task.
<langsyntaxhighlight lang="perl">use Math::GMPz;
 
my $nmax = 250;
Line 2,055 ⟶ 2,199:
bicenter($n);
print "$n: $unrooted[$n]\n";
}</langsyntaxhighlight>
 
Output identical to C GMP example (truncated to 250).
Line 2,062 ⟶ 2,206:
{{trans|Ruby}}
{{trans|FreeBASIC}}
Using IEEE754 floats, hence imprecise above n=45 on 32 bit, n=52 on 64bit, tested to n=500600, giving 63.988e21799378e+262 using %g format, ie accurate to better than 1e-10%.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant MAX_N = 32,
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
BRANCH = 4
<span style="color: #008080;">constant</span> <span style="color: #000000;">MAX_N</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">BRANCH</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span>
sequence {rooted,unrooted} @= repeat(0,MAX_N+2)
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rooted</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;">MAX_N</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
procedure tree(integer br, n, l=n, tot=1, atom cnt=1)
<span style="color: #000000;">unrooted</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;">MAX_N</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
atom c
for b=br+1 to BRANCH do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">br</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tot</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">cnt</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
tot += n
<span style="color: #004080;">atom</span> <span style="color: #000000;">c</span>
if tot>=MAX_N+1
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">br</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">BRANCH</span> <span style="color: #008080;">do</span>
or (l*2>=tot and b>=BRANCH) then
<span style="color: #000000;">tot</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">n</span>
return
<span style="color: #008080;">if</span> <span style="color: #000000;">tot</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">MAX_N</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
end if
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">tot</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">BRANCH</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if b==br+1 then
c <span style="color: rooted[n+1]*cnt#008080;">return</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else
<span style="color: #004080;">integer</span> <span style="color: #000000;">n1</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>
c *= (rooted[n+1]+(b-br-1))/(b-br)
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tot</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">==</span><span style="color: #000000;">br</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if l*2<tot then
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n1</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">cnt</span>
unrooted[tot+1] += c
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">c</span> <span style="color: #0000FF;">*=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n1</span><span style="color: #0000FF;">]+(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">-</span><span style="color: #000000;">br</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))/(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">-</span><span style="color: #000000;">br</span><span style="color: #0000FF;">)</span>
if b<BRANCH then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
rooted[tot+1] += c
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;"><</span><span style="color: #000000;">tot</span> <span style="color: #008080;">then</span>
for m=1 to n-1 do
<span style="color: #000000;">unrooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">c</span>
tree(b,m,l,tot,c)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;"><</span><span style="color: #000000;">BRANCH</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">c</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">m</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>
end procedure
<span style="color: #000000;">tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tot</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
procedure bicenter(integer s)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if mod(s,2)=0 then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
atom aux = rooted[s/2+1]
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
unrooted[s+1] += aux*(aux+1)/2
end if
<span style="color: #008080;">procedure</span> <span style="color: #000000;">bicenter</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">if</span> <span style="color: #7060A8;">even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
 
<span style="color: #004080;">atom</span> <span style="color: #000000;">aux</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
rooted[1..2] = 1
<span style="color: #000000;">s</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
unrooted[1..2] = 1
<span style="color: #000000;">unrooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">aux</span><span style="color: #0000FF;">*(</span><span style="color: #000000;">aux</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span>
for n=1 to MAX_N do
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
tree(0, n)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
bicenter(n)
if n<10 or n=MAX_N then
<span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
printf(1,"%d: %d\n",{n, unrooted[n+1]})
<span style="color: #000000;">unrooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">MAX_N</span> <span style="color: #008080;">do</span>
end for</lang>
<span style="color: #000000;">tree</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>
<span style="color: #000000;">bicenter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">10</span> <span style="color: #008080;">or</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">MAX_N</span> <span style="color: #008080;">then</span>
<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;">"%d: %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">unrooted</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>
<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,121 ⟶ 2,272:
9: 35
32: 27711253769
</pre>
And the same thing using gmp, obviously without any such accuracy limits
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">max_n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">200</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">branch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</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: #004080;">sequence</span> <span style="color: #000000;">ivals</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</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;">max_n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">rooted</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">max_n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ivals</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">unrooted</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">max_n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ivals</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">branch</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">tmp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">br</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">mpz</span> <span style="color: #000000;">cnt</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">cbr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">br</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">br</span> <span style="color: #008080;">to</span> <span style="color: #000000;">branch</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">></span><span style="color: #000000;">max_n</span>
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">s</span> <span style="color: #008080;">and</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">branch</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">br</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cbr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rooted</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> <span style="color: #000000;">cnt</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rooted</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> <span style="color: #000000;">b</span><span style="color: #0000FF;">-</span><span style="color: #000000;">br</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cbr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cbr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_divexact_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cbr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cbr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">-</span><span style="color: #000000;">br</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;"><</span><span style="color: #000000;">s</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">u</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unrooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">u</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cbr</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">b</span><span style="color: #0000FF;"><</span><span style="color: #000000;">branch</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">mpz_add</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;">cbr</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">m</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: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cbr</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;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">bicenter</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">aux</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">u</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">unrooted</span><span style="color: #0000FF;">[</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">mpz_add_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">aux</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">aux</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_tdiv_q_2exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">u</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">tmp</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;">procedure</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">cnt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">max_n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">tree</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cnt</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">bicenter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">25</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<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;">"%d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">mpz_get_short_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">unrooted</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>
<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: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
Unusually this is significantly faster (6*) under pwa/p2js than desktop/Phix, the following output/time is from the former.<br>
The limit of 200 in the code above finishes on desktop/Phix in 6s, p2js 1s, 500 takes about 40s in the browser but a whopping three and a half minutes on desktop/Phix, compared to the Go entry above completing that task in 15.6s, all on the same box of course. I have earmarked this task for further investigation into performance improvements, should Phix 2.0 ever actually get started on, that is.
{{out}}
<pre>
1: 1
2: 1
3: 1
4: 2
5: 3
6: 5
7: 9
8: 18
9: 35
10: 75
11: 159
12: 355
13: 802
14: 1858
15: 4347
16: 10359
17: 24894
18: 60523
19: 148284
20: 366319
21: 910726
22: 2278658
23: 5731580
24: 14490245
25: 36797588
100: 5921072038125809849884993369103538010139
200: 94304332879903850516...51576307818857723954 (84 digits)
300: 30845274893235665913...77084032710062170279 (129 digits)
400: 13544322063698139999...74867143490557738328 (174 digits)
500: 69888806977968318652...40565376686372508743 (218 digits)
600: 39937810748209753430...83373172999304809224 (263 digits)
"1 minute and 23s"
</pre>
 
=={{header|Pike}}==
{{trans|Python}}
<langsyntaxhighlight Pikelang="pike">int MAX_N = 300;
int BRANCH = 4;
Line 2,187 ⟶ 2,440:
write("%d: %d\n", n, unrooted[n]);
}
}</langsyntaxhighlight>
 
=={{header|Python}}==
This version only counts different paraffins. The multi-precision integers of Python avoid overflows.
{{trans|C}}
<langsyntaxhighlight lang="python">try:
import psyco
psyco.full()
Line 2,244 ⟶ 2,497:
print "%d: %d" % (n, unrooted[n])
 
main()</langsyntaxhighlight>
Output (newlines added):
<pre>1: 1
Line 2,294 ⟶ 2,547:
=== Using generating function ===
This is almost the same as the one in [[Formal power series]]. Compare to the Mathematica and Haskell solutions.
<langsyntaxhighlight lang="python">from itertools import count, chain, tee, islice, cycle
from fractions import Fraction
from sys import setrecursionlimit
Line 2,406 ⟶ 2,659:
a602 = a678 - a599 + x2
 
for n,x in zip(count(0), islice(a602, 500)): print(n,x)</langsyntaxhighlight>
 
=== Using generating function without OO ===
Line 2,412 ⟶ 2,665:
This uses a different generating function, but also demonstrates a lower level approach and the use of <code>functools.lru_cache</code> to memoise a recursive function which would otherwise make an exponential number of recursive calls.
 
<langsyntaxhighlight lang="python">#!/usr/bin/python3
 
from functools import lru_cache
Line 2,443 ⟶ 2,696:
def A000602(k): return A000642(k) + (A000642((k-1) // 2) if k % 2 == 1 else 0) - A000631(k-1)
 
for k in range(500): print(k, A000602(k))</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 2,449 ⟶ 2,702:
 
Or, a direct translation of the C entry:
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 2,486 ⟶ 2,739:
(bicenter n)
(printf "~a: ~a\n" n (vector-ref unrooted n)))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,496 ⟶ 2,749:
Note how lexical scoping &mdash; rather than global variables or repeated arguments &mdash; is used to pass down information to subroutines.
 
<syntaxhighlight lang="raku" perl6line>sub count-unrooted-trees(Int $max-branches, Int $max-weight) {
my @rooted = flat 1,1,0 xx $max-weight - 1;
my @unrooted = flat 1,1,0 xx $max-weight - 1;
Line 2,540 ⟶ 2,793:
my constant N = 100;
my @paraffins = count-unrooted-trees(4, N);
say .fmt('%3d'), ': ', @paraffins[$_] for flat 1 .. 30, N;</langsyntaxhighlight>
 
{{out}}
Line 2,579 ⟶ 2,832:
 
Programming note: &nbsp; the biggest concern was calculating the number of decimal digits &nbsp; (so as to avoid integer overflow).
<langsyntaxhighlight lang="rexx">/*REXX pgm enumerates (without repetition) the number of paraffins with N carbon atoms. */
parse arg nodes . /*obtain optional argument from the CL.*/
if nodes=='' | nodes=="," then nodes= 100 /*Not specified? Then use the default.*/
Line 2,605 ⟶ 2,858:
if LL<sum then unrooted.sum= unrooted.sum + #.br
if b==4 then leave
rooted.sum = rooted.sum + #.br
do m=nm by -1 for nm; call tree b, m, L, sum, #.br
end /*m*/
end /*b*/ /* ↑↑↑↑↑↑↑↑↑ recursive. */
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 600 </tt>}}
 
Line 3,219 ⟶ 3,472:
=={{header|Ruby}}==
{{trans|Python}}
<langsyntaxhighlight lang="ruby">MAX_N = 500
BRANCH = 4
 
Line 3,254 ⟶ 3,507:
bicenter(n)
puts "%d: %d" % [n, $unrooted[n]]
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,299 ⟶ 3,552:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object Paraffins extends App {
val (nMax, nBranches) = (250, 4)
val rooted, unrooted = Array.tabulate(nMax + 1)(i => if (i < 2) BigInt(1) else BigInt(0))
Line 3,332 ⟶ 3,585:
println(f"$n%3d: ${unrooted(n)}%s")
}
}</langsyntaxhighlight>
{{Out}}See it in running in your browser by [https://scalafiddle.io/sf/JjR1R6k/2 ScalaFiddle (JavaScript)] or by [https://scastie.scala-lang.org/QycB9fBTTYG890zKOqREVw Scastie (JVM)].
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "bigint.s7i";
 
Line 3,397 ⟶ 3,650:
writeln(n <& ": " <& unrooted[n]);
end for;
end func;</langsyntaxhighlight>
 
Output (trimmed):
Line 3,434 ⟶ 3,687:
{{trans|C}}
Handles arbitrarily large values.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
set maxN 200
Line 3,479 ⟶ 3,732:
bicenter $n
puts "${n}: [lindex $unrooted $n]"
}</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 3,485 ⟶ 3,738:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
import "./fmt" for Fmt
 
var branches = 4
Line 3,535 ⟶ 3,788:
bicenter.call(n)
Fmt.print("$3d: $i", n, unrooted[n])
}</langsyntaxhighlight>
 
{{out}}
Line 3,579 ⟶ 3,832:
250: 16206624309085062837751018464745815688226709117091506494175397665527493805947344857313038875654104100026504
</pre>
 
 
=={{header|zkl}}==
Line 3,585 ⟶ 3,837:
{{trans|Go}}
Uses GMP for big ints, mostly modified in place. Rather slow.
<langsyntaxhighlight lang="zkl">var BN=Import("zklBigNum");
 
const nMax=100, nBranches=4;
Line 3,619 ⟶ 3,871:
bicenter(n);
println(n, ": ", unrooted[n]);
}</langsyntaxhighlight>
{{out}}
<pre>
9,486

edits