Tuesday, June 19, 2012

Small Basic Cube Root


'Cuberoot challenge - MLQ436-0
'Harry Hardjono
'June 2012
'Just a 10 minute quickie

Loop:
TextWindow.Write("Enter a number (1-1000): ")
N=TextWindow.ReadNumber()

If N=0 Then
  Program.End()
EndIf

D=1000 'Delta. Decreasing range in calcloop.
S=1 'Starting number. To be modified in calcloop.
E=0.00000000001 ' Epsilon. Desired accuracy.

calcloop:
While (D>E)
  C=(S+D)*(S+D)*(S+D)
  If C<=N Then
    S=S+D
  Else 'Not comfortable skipping else when D isn't a power of two.
    D=D/2
  EndIf
EndWhile

TextWindow.Write("Cube root is: ")
TextWindow.WriteLine(S)
TextWindow.WriteLine(" ")
Goto Loop

No comments: