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.
is divide by 2
is multiply by 3, even in binary mode
Show the Collatz sequence:
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
- 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
Code:
f SL
g LSTx
+
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