r/QBprograms • • Apr 06 '22
QBASIC QBASIC 3n+1 problem
The 3n+1 problem: Start with any positive integer. If even divide by 2, if odd multiply by 3 and add 1. repeat, No matter what you start with you will end up at one.
https://en.wikipedia.org/wiki/Collatz_conjecture
DO
INPUT "Starting value", n
IF n > 0 THEN
m = n
e = 0
o = 0
DO
IF (n MOD 2) = 0 THEN
e = e + 1
n = n / 2
ELSE
o = o + 1
n = 3 * n + 1
END IF
IF n > m THEN m = n
PRINT "Step:"; e + o; " Max:"; m; " Current:"; n
LOOP WHILE n > 1
END IF
LOOP WHILE n > 0
2
Upvotes
View all comments
2
u/planetmikecom Apr 06 '22
There are some creative aspects going on here. Why count the number of iterations when you can just add the number of even and odd values you've used? Then the process keeps going (looping) until you enter a value of zero or lower.