FriCAS Code
Brought to you by:
whebisch
--Copyright The Numerical Algorithms Group Limited 1991. -- This is the 3n + 1 function: f(n) = n/2 if n is even, -- 3n + 1 otherwise -- -- It can obviously be coded as -- f n == -- even? n => n quo 2 -- 3 * n + 1 -- but we code using the pattern matching syntax: -- f n == -- n is 2*m% => m% -- 3 * n + 1 -- The 'integer' is to return an integer instead of a polynomial f n == not empty?(u := Is(n, 2*m%)) => integer eval(m%, u) 3 * n + 1 )set stream showall on -- The function g generates the stream of the repeated applications of f g(n:INT):STREAM(INT) == stream(f, n) -- There is a conjecture that g(n) gets into the cycle (1 4 2) -- for every n s := g 27 extend(s, 150)