Paraffins

From Rosetta Code
Revision as of 13:29, 30 November 2011 by 79.54.58.148 (talk) (Created first draft of the Paraffins task)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Paraffins is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The purpose of this Task is to count how many different paraffin molecules there are for a given number of Carbon atoms, or to actually show them. The molecules must be topologically different, so all reflections, etc. are counted once.

The input is just the number 'n' of Carbon atoms of a molecule, like 17.

The output is how many different different paraffins there are with 'n' Carbon atoms (like 24_894 if n = 17).

The sequence of those results is in the Sloane encyclopaedia too, "Number of n-node unrooted quartic trees; number of n-carbon alkanes C(n)H(2n+2) ignoring stereoisomers":

http://oeis.org/A000602

The sequence is (the index starts from 1, it's the number of Carbon atoms):

1, 1, 1, 1, 2, 3, 5, 9, 18, 35, 75, 159, 355, 802, 1858, 4347, 10359, 24894, 60523, 148284, 366319, 910726, 2278658, 5731580, 14490245, 36797588, 93839412, 240215803, 617105614, 1590507121, 4111846763, 10660307791, 27711253769, ...


Stretch goal: show the paraffins in some way. A flat 1D representation, with arrays or lists is enough, like:

<lang haskell>

  • 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)) (C H H (C H H H)),CCP 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 H (C H H H)),CCP (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 (C H H
(C H H (C H H H))) (C H (C H H H) (C H H 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 H
(C H H H)) (C 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))]

</lang>

Showing a basic 2D ASCII-art representation of the molecules is even better.

A Scheme implementation: http://www.ccs.neu.edu/home/will/Twobit/src/paraffins.scm

A Haskell implementation: http://darcs.brianweb.net/nofib/imaginary/paraffins/Main.hs

An old paper that explains the functional code, with some molecules shown too: http://www.cs.wright.edu/~tkprasad/courses/cs776/paraffins-turner.pdf

There is a Fortress implementation too (turnersParaffins0.fss).