Microsoft Small Basic

Program Listing: FDS098
TextWindow.WriteLine( "Collatz Conjecture" )
TextWindow.WriteLine( "Any number you enter must be greater than 1. Decimal numbers will be rounded to the nearest integer. ")
Sub enternum
TextWindow.Write("Please enter a number: ")
n = TextWindow.ReadNumber()
n = Math.Round( n ) ' Round a decimal number to the nearest whole number.
If n <= 1 Then ' If the number is less than or equal to 1 do not continue.
TextWindow.WriteLine("The number must be greater than 1")
enternum()
EndIf
EndSub
While 1 = 1 ' Infinite Loop
enternum()
TextWindow.WriteLine( "Starting with the number " + n )
steps = 0
While n > 1 ' End loop if n reaches 1.
r = Math.Remainder(n, 2) ' Check if number is even or odd.
If (r = 0) Then
' Number is even. Divide it by 2.
n = n / 2
Else
' Number is odd. Multiply it by 3 and then add 1.
n = 3 * n + 1
EndIf
TextWindow.WriteLine( n )
steps = steps + 1 ' Update steps counter.
EndWhile
TextWindow.WriteLine( "The program has reached a solution of 1." )
TextWindow.WriteLine( "This took " + steps + " steps." ) ' Show how many steps it took.
EndWhile