; 3n+1 conjecture graph
; By Andy Yeh (2021)
cs ct reset
; sub routine checking if a number is odd or even
to isOdd :num
ifelse bitand 1 :num [ output true ] [ output false ]
end
; read a number from keyboard (user input)
make "n (readword [Entre a whole number (n)?])
; make sure the input number is a natural number
if not (number? :n) [ print [Please enter a whole number that is greater than 1] bye ]
if :n < 2 [ print [Please enter a whole number that is greater than 1] bye ]
make "n round :n
(print [ Number for graphing is ] :n)
; start turtle graphing
make "step 0 ; record number of steps
make "max 0 ; record maximum number in the number sequence
pd
while (:n > 1) [
ifelse isOdd :n
[ make "n :n*3+1 ]
[ make "n :n/2 ]
setxyz :step :n 0
make "step :step + 1
if :n > :max [ make "max :n ]
print :n
]
pu
; report results in the console
(print [Number of steps: ] :step)
(print [Max number: ] :max)
; scale the graph by the ration of max/step for better looking
select object
setsx :max / :step
showall ; fit the graph into the 3D screen