HP Forums
(16C) Collatz conjecture - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (16C) Collatz conjecture (/thread-17844.html)



(16C) Collatz conjecture - wynen - 12-22-2021 01:41 PM

Starting with any positive integer, repeat the following operation

- If the number is even, divide it by two.
- If the number is odd, triple it and add one.

The conjecture is, this process will always reach the number one.

The HP16-C has the shift an rotate operations in integer modes, which are useful for this kind of programs.

Code:
f SR
is divide by 2
Code:
f SL
g LSTx
+
is multiply by 3, even in binary mode

Show the Collatz sequence:

Code:
001 43,22, C    g LBL C
002    43 34    g PSE       // Show number
003    42  b    f SR        // divide by 2
004    43 40    g x=0       // if it is zero now, it must be 1 before
005    43 21    g RTN       // finished
006 43, 6, 4    f F? 4      // if carry is set, the number was odd 
007    43 40    g x=0       // (always false) carry set 
008    22  C      GTO C     // carry not set, number was even, divide by 2 already done
009    43  C    g RCL       // carry set, restore the number
010    42  A    f SL        
011    43 36    g LSTx
012       40      +        
013        1      [1]
014       40      +         // 3*x+1
015    22  C      GTO C

Usage:
enter a positive number
GSB C

Example:
7 GSB C
shows
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 0


RE: (16C) Collatz conjecture - Maximilian Hohmann - 12-22-2021 04:08 PM

Hello!

For more background you can watch this video: https://www.youtube.com/watch?v=094y1Z2wpJg

I saw it by chance a couple of weeks ago (not really by chance as I watch nearly all videos from Derek Muller).

Regards
Max