HP Forums
(34S) 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: (34S) Collatz Conjecture (/thread-1496.html)



(34S) Collatz Conjecture - Dave Britten - 05-31-2014 08:11 PM

Briefly, the Collatz Conjecture states that you can start with any positive integer, and by repeating these two transformations, eventually arrive at 1:

1. If the number is even, divide it by 2.
2. If the number is odd, multiply it by 3 and add 1.

Here's a simple program to evaluate such sequences, and report the number of transformations that were needed to reach 1, as well as the max value that was obtained along the way. Optionally, you can set flag 1 before execution to view the current value being updated in real-time (this slows the program down considerably, though).

Input:
X: Starting Value

Output:
Y: Maximum Value
X: Number of Transformations

Example:
27 XEQ'CTZ'

In less than one second, you should see 111, the total number of transformations. Press x><y to see the max value, 9,232.

Code:
LBL 'CTZ'
LocR 002
STO .01
0
STO .00
RDown
LBL 00
x=1?
GTO 09
FS? 01
PSE 00
INC .00
EVEN?
GTO 02
3
*
1
+
x>? .01
STO .01
GTO 00
LBL 02
2
/
GTO 00
LBL 09
RCL .01
RCL .00
RTN
END



RE: (WP-34S) Collatz Conjecture - Didier Lachieze - 05-31-2014 08:44 PM

The Collatz Conjecture is also called the Syracuse problem.
Here is my version for the WP 34S in 19 steps:
Code:
001 LBL A
002 FILL
003 STO- Y
004 x#1?
005 SKIP 002
006 DROP
007 RTN
008 INC Y
009 EVEN?
010 SKIP 006 
011 3
012 *
013 INC X
014 x>?Z
015 STO Z
016 BACK 008
017 2
018 /
019 BACK 015