Note that there are some explanatory texts on larger screens.

plurals
  1. POCode Golf: Collatz Conjecture
    primarykey
    data
    text
    <p>Inspired by <a href="http://xkcd.com/710/" rel="noreferrer">http://xkcd.com/710/</a> here is a code golf for it.</p> <p><strong>The Challenge</strong></p> <p>Given a positive integer greater than 0, print out the hailstone sequence for that number. </p> <p><strong>The Hailstone Sequence</strong></p> <p>See <a href="http://en.wikipedia.org/wiki/Collatz_conjecture" rel="noreferrer">Wikipedia</a> for more detail.. </p> <ul> <li>If the number is even, divide it by two.</li> <li>If the number is odd, triple it and add one.</li> </ul> <p>Repeat this with the number produced until it reaches 1. (if it continues after 1, it will go in an infinite loop of <code>1 -&gt; 4 -&gt; 2 -&gt; 1...</code>)</p> <p>Sometimes code is the best way to explain, so here is some from Wikipedia</p> <pre><code>function collatz(n) show n if n &gt; 1 if n is odd call collatz(3n + 1) else call collatz(n / 2) </code></pre> <p>This code works, but I am adding on an extra challenge. <strong>The program must not be vulnerable to stack overflows</strong>. So it must either use iteration or tail recursion. </p> <p>Also, bonus points for if it can calculate big numbers and the language does not already have it implemented. (or if you reimplement big number support using fixed-length integers)</p> <p><strong>Test case</strong></p> <pre><code>Number: 21 Results: 21 -&gt; 64 -&gt; 32 -&gt; 16 -&gt; 8 -&gt; 4 -&gt; 2 -&gt; 1 Number: 3 Results: 3 -&gt; 10 -&gt; 5 -&gt; 16 -&gt; 8 -&gt; 4 -&gt; 2 -&gt; 1 </code></pre> <p>Also, the code golf must include full user input and output. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload