Tuesday, May 22, 2012

Small Basic Prime Number


' Harry Hardjono
'May 2012
' Done in less than 30 minutes. :)


Init:
TextWindow.WriteLine("How many Prime numbers (1-1000)? ")
N=TextWindow.ReadNumber()
If N<1 Or N>1000 Then
  TextWindow.WriteLine("That's All Folks!")
  TextWindow.Pause()
  Program.End()
EndIf


Prime="1=2;"
Num=Prime[Array.GetItemCount(Prime)]


MainLoop:
Num=Num+1
  
  Flag=1
  For i=1 To Array.GetItemCount(Prime)
    If (Math.Remainder(Num,Prime[i])=0) Then
      'Not Prime
      Flag=0
      i=Array.GetItemCount(Prime)
    EndIf
  EndFor
  If Flag=1 Then
    Prime[Array.GetItemCount(Prime)+1]=Num
    TextWindow.Write(Array.GetItemCount(Prime))
    TextWindow.WriteLine("   "+Num)
    if Array.GetItemCount(Prime)>=N Then
      Goto Ender
    EndIf
  EndIf
  Goto MainLoop
  


Ender:
'TextWindow.Write("Press any key to continue...")
TextWindow.WriteLine("---")
TextWindow.Pause()
Goto Init

No comments: