# --- 1. SETUP CONSTANTS ---
# Create number 1 ( O ) and Loop Limit (M = 512).
INCREASE O
# Build M = 512 using powers of 2
INCREASE M
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
REPEAT M TIMES ( INCREASE M )
# F is our "Run" flag. 1 = Run, 0 = Stop.
CLEAR F
INCREASE F
# --- 2. MAIN LOOP ---
REPEAT M TIMES (
# --- STEP A: PRINT ---
REPEAT F TIMES (
PRINT X
)
# --- STEP B: CALCULATION & STOP CHECK ---
# We combine Division and Stop Logic to save ops.
REPEAT F TIMES (
# 1. Copy X to Y
CLEAR Y
REPEAT X TIMES ( INCREASE Y )
# 2. Division ( X / 2 )
# Q = Quotient, R = Remainder, E = Even Flag
CLEAR Q
CLEAR R
CLEAR E
INCREASE E
# Loop Y times ( Cost: ~9 ops per X )
REPEAT Y TIMES (
# If R=1, we complete a pair, so Q++
REPEAT R TIMES ( INCREASE Q )
# Swap R and E ( Toggle Logic )
CLEAR T
REPEAT R TIMES ( INCREASE T )
CLEAR R
REPEAT E TIMES ( INCREASE R )
CLEAR E
REPEAT T TIMES ( INCREASE E )
)
# 3. Check if Done ( Optimization )
# If Q is 0, it means X was 1. We must Stop ( F=0 ) .
# If Q > 0, X was >= 2. We Continue ( F=1 ) .
# Reset F to 0.
CLEAR F
# If Q > 0, Set F back to 1.
# If Q == 0, this loop skips, F stays 0.
REPEAT Q TIMES (
CLEAR F
INCREASE F
)
# 4. Update X
# Only run if F is still 1 ( i.e., X was not 1 )
REPEAT F TIMES (
CLEAR X
# Even Case: X = Q
REPEAT E TIMES (
REPEAT Q TIMES ( INCREASE X )
)
# Odd Case: X = 3Y + 1
REPEAT R TIMES (
REPEAT Y TIMES (
INCREASE X
INCREASE X
INCREASE X
)
INCREASE X
)
)
)
)