Skip to content

Instantly share code, notes, and snippets.

@sguzman
Created April 12, 2021 16:57
Show Gist options
  • Save sguzman/c2b8df89798d42196a939a8ba653b70e to your computer and use it in GitHub Desktop.
Save sguzman/c2b8df89798d42196a939a8ba653b70e to your computer and use it in GitHub Desktop.
Studying collatz using even/odd tokens as a grammar
$RecursionLimit = Infinity
collatz[1] = 1
collatz[n_Integer /; And[n > 1, OddQ[n]]] := collatz[n] = 3 n + 1
collatz[n_Integer /; And[n > 1, EvenQ[n]]] := collatz[n] = n/2
collatzseq[1] = {1}
collatzseq[n_Integer /; n > 1] :=
collatzseq[n] = {n, Splice@collatzseq@collatz@n}
collatzseq[93571393692802302];
map[n_Integer /; OddQ[n]] = o
map[n_Integer /; EvenQ[n]] = e
c = Map[map, collatzseq[93571393692802302]];
seqrules = {{e, o, e, o, e} :> \[Alpha], {e, o,
e} :> \[Beta], {\[Alpha], o} :> \[Gamma], {e,
e} :> \[Delta], {\[Beta], \[Beta]} :> \[Zeta], {e, \[Gamma]} :>
A, {e, \[Alpha]} :> B, {e, \[Beta]} :> F, {e, \[Zeta]} :>
G, {\[Delta], e, o} :> T}
oseqrules = {{e, o, e, o, e} :> \[Alpha], {e, o,
e} :> \[Beta], {\[Alpha], o} :> \[Gamma]}
seqfuncs = Apply[RightComposition, Map[SequenceReplace, seqrules]]
oseqfuncs = Apply[RightComposition, Map[SequenceReplace, oseqrules]]
With[{c = c},
Grid@
Prepend[
TakeLargestBy[
Select[
Map[{
First@#,
Last@#,
Length@First@# Last@#,
(Length@First@# - 1) Last@#
} &,
Tally@
Table[
Splice@
Partition[c, i, 1],
{i, 2, Length@c/2, 1}]
]
, And[
Length@First@# != 1,
Extract[#, 2] != 1
] &]
, Last, 15]
, {"Lexeme", "Freq", "Freq * Len[Lexeme]",
"(Len[Lexeme]-1)* Freq"}]
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment