How to resolve the algorithm Paraffins step by step in the Wren programming language
How to resolve the algorithm Paraffins step by step in the Wren programming language
Table of Contents
Problem Statement
This organic chemistry task is essentially to implement a tree enumeration algorithm.
Enumerate, without repetitions and in order of increasing size, all possible paraffin molecules (also known as alkanes).
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. So all paraffins with n carbon atoms share the empirical formula CnH2n+2 But for all n ≥ 4 there are several distinct molecules ("isomers") with the same formula but different structures. The number of isomers rises rather rapidly when n 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), so rotations or re-orientations of parts of the molecule (without breaking bonds) do not give different isomers. So what seem at first to be different molecules may in fact turn out to be different orientations of the same molecule.
With n = 3 there is only one way of linking the carbons despite the different orientations the molecule can be drawn; and with n = 4 there are two configurations:
Due to bond rotations, it doesn't matter which direction the branch points in. The phenomenon of "stereo-isomerism" (a molecule being different from its mirror image due to the actual 3-D arrangement of bonds) is ignored for the purpose of this task. The input is the number n of carbon atoms of a molecule (for instance 17). The output is how many different different paraffins there are with n carbon atoms (for instance 24,894 if n = 17). The sequence of those results is visible in the OEIS entry: The sequence is (the index starts from zero, and represents the number of carbon atoms):
Show the paraffins in some way. A flat 1D representation, with arrays or lists is enough, for instance: Showing a basic 2D ASCII-art representation of the paraffins is better; for instance (molecule names aren't necessary): http://www.cs.wright.edu/~tkprasad/courses/cs776/paraffins-turner.pdf https://github.com/ghc/nofib/blob/master/imaginary/paraffins/Main.hs http://www.ccs.neu.edu/home/will/Twobit/src/paraffins.scm http://java.net/projects/projectfortress/sources/sources/content/ProjectFortress/demos/turnersParaffins0.fss?rev=3005
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Paraffins step by step in the Wren programming language
Source code in the wren programming language
import "/big" for BigInt
import "/fmt" for Fmt
var branches = 4
var nMax = 250
var rooted = List.filled(nMax + 1, BigInt.zero)
var unrooted = List.filled(nMax + 1, BigInt.zero)
var c = List.filled(branches, BigInt.zero)
var tree
tree = Fn.new { |br, n, l, sum, cnt|
var b = br + 1
while (b <= branches) {
sum = sum + n
if (sum > nMax) return
if (l*2 >= sum && b >= branches) return
if (b == br + 1) {
c[br] = rooted[n] * cnt
} else {
var tmp = rooted[n] + BigInt.new(b - br - 1)
c[br] = c[br] * tmp
c[br] = c[br] / BigInt.new(b - br)
}
if (l*2 < sum) unrooted[sum] = unrooted[sum] + c[br]
if (b < branches) rooted[sum] = rooted[sum] + c[br]
var m = n - 1
while (m > 0) {
tree.call(b, m, l, sum, c[br])
m = m - 1
}
b = b + 1
}
}
var bicenter = Fn.new { |s|
if (s%2 == 0) {
var tmp = (rooted[(s/2).floor] + BigInt.one) * rooted[(s/2).floor]
tmp = tmp >> 1
unrooted[s] = unrooted[s] + tmp
}
}
rooted[0] = BigInt.one
rooted[1] = BigInt.one
unrooted[0] = BigInt.one
unrooted[1] = BigInt.one
for (n in 1..nMax) {
tree.call(0, n, n, 1, BigInt.one)
bicenter.call(n)
Fmt.print("$3d: $i", n, unrooted[n])
}
You may also check:How to resolve the algorithm Bitmap step by step in the PHP programming language
You may also check:How to resolve the algorithm DNS query step by step in the Groovy programming language
You may also check:How to resolve the algorithm Angle difference between two bearings step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Calendar - for REAL programmers step by step in the Go programming language
You may also check:How to resolve the algorithm Range expansion step by step in the C++ programming language