# --- 1. SETUP CONSTANTS --- # O = 1 INCREASE O # M = 512. We use doubling to build this fast. # 1 -> 2 -> 4 ... -> 512 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 = 1 (Run Flag). CLEAR F INCREASE F # --- 2. MAIN LOOP --- REPEAT M TIMES ( # --- STEP A: PRINT --- REPEAT F TIMES ( PRINT X ) # --- STEP B: CALCULATION & STOP CHECK --- REPEAT F TIMES ( # 1. Fast Division (X / 2) # We calculate Quotient (Q) and Remainder (R) directly from X. CLEAR Q CLEAR R # "Logic Gate" Division: Toggle R between 0 and 1. REPEAT X TIMES ( # T is a Toggle Helper. Set T=1. CLEAR T INCREASE T # If R is 1: Flip to 0, Increment Q, Clear T (so next block skips) REPEAT R TIMES ( CLEAR T INCREASE Q CLEAR R ) # If R was 0: T is still 1. Flip R to 1. REPEAT T TIMES ( INCREASE R ) ) # 2. Stop Check Optimization # If Q == 0 (meaning X was 1), we must Stop (Set F=0). # We use Z as an inverter. # Set Z = 1. CLEAR Z INCREASE Z # If Q > 0, Z becomes 0. REPEAT Q TIMES ( CLEAR Z ) # If Z is still 1 (Q was 0), Clear F. REPEAT Z TIMES ( CLEAR F ) # 3. Update X # Only runs if F is still 1 REPEAT F TIMES ( CLEAR X # Base Step: Set X = Q # If X was Even, this is the final value. # If X was Odd, this is the starting point (Q). REPEAT Q TIMES ( INCREASE X ) # Odd Correction: X needs to be 6Q + 4. # We currently have Q. We need to add 5Q + 4. REPEAT R TIMES ( REPEAT Q TIMES ( INCREASE X INCREASE X INCREASE X INCREASE X INCREASE X ) INCREASE X INCREASE X INCREASE X INCREASE X ) ) ) )