% Prime Number Finder
% Mikhail D
% 2007-01-17
% ICS3M0-A
import GUI
% variable delerations
var timestart, timeend : int := 0
var num : real := 2
var len, pcount, nonpcount : int := 0
var progButton, quitButton, aboutButton, innum, outbox : int
% instead of lable, more simple and easy to include
procedure lableinnum
locate (5, 10)
put "Enter max range above."
end lableinnum
% program windows settings and title
procedure bg
GUI.SetBackgroundColor (16)
Draw.Box (0, 0, maxx, maxy, 17)
Draw.Box (1, 1, maxx - 1, maxy - 1, 17)
Draw.Box (2, 2, maxx - 2, maxy - 2, 17)
color (79)
colorback (16)
locate (2, 5)
put "Prime Number Finder"
lableinnum
end bg
% about the program
procedure aboutapp
locate (9, 10)
put "Program name: Prime Number Finder"
locate (11, 10)
put "Created by: Mikhail D."
locate (13, 10)
put "Contact:
doc776@gmail.com"
locate (15, 10)
put "Final version completed on: Jan 21, 2007"
locate (17, 10)
put "What it does: Filters out prime numbers "
locate (18, 10)
put " and displays them."
end aboutapp
% main code of the prime number filter
procedure mainprog
cls
% hide buttons of no use during procedure
GUI.Hide (progButton)
GUI.Hide (quitButton)
GUI.Hide (aboutButton)
% reset used variables to defoult state
timestart := 0
timeend := 0
num := 2
len := 0
pcount := 0
nonpcount := 0
% start error trap input
loop
cls
bg
if strnatok (GUI.GetText (innum)) = true then
if strnat (GUI.GetText (innum)) < 10000 and strnat (GUI.GetText (innum)) > 1 then
% exit if number is number and with in range, and update main variable
len := strnat (GUI.GetText (innum))
exit
else % else show buttons and quit procedure
GUI.Show (progButton)
GUI.Show (quitButton)
GUI.Show (aboutButton)
GUI.Refresh
return
end if
else % else show buttons and quit procedure
GUI.Show (progButton)
GUI.Show (quitButton)
GUI.Show (aboutButton)
GUI.Refresh
return
end if
end loop
% end error trap input
% set starting time of filter
timestart := Time.Elapsed
locate (9, 15)
put "Staring... " ..
% declear the array that hold the new numbers, range based on input
var list : array 2 .. len, 1 .. 2 of int
put "Done!"
locate (11, 15)
put "Filtering... "
locate (13, 15)
put "Cheking "
% start prime number filter
for i : 2 .. len % go through each number
% set the array variables as number and state
list (i, 1) := i
list (i, 2) := 1
% display curent number working on
locate (13, 23)
put i, " of ", len
% for each number chek division outcome for each posible divider
for x : 2 .. i
num := list (i, 1) / x
if strnatok (realstr (num, 1)) = true and num not= 1 then
list (i, 2) := 0 % deactivate the number if not a prime
end if
end for
% display persentage of how many numbers cheked so far
locate (11, 28)
put (i * 100) / len : 0 : 0, "\b%"
end for
% end prime number filter
% set the end time of filtering
timeend := Time.Elapsed
cls
bg
% dispose of old box if been used befor
if len > 0 then
GUI.Dispose(outbox)
end if
% recreate the box
outbox := GUI.CreateTextBox (10, 10, 60, maxy - 50)
% show first 2 stats of the filtering
locate (9, 15)
put "Prime numbers from 2 to ", len, "."
locate (11, 15)
put "Filtering time: ", ((timeend - timestart) / 1000) : 0 : 2, " seconds"
% display prime numbers in box and cound how many of them
for i : 2 .. len
if list (i, 2) = 1 then
GUI.AddText (outbox, " ")
GUI.AddText (outbox, intstr (list (i, 1)))
GUI.AddLine (outbox, "")
pcount := pcount + 1
else
nonpcount := nonpcount + 1
end if
end for
% display last 2 stats of filtered numbers
locate (13, 15)
put "Prime numbers: ", pcount, " of ", len
locate (15, 15)
put "Non prime numbers: ", nonpcount, " of ", len
% show buttons
GUI.Show (progButton)
GUI.Show (quitButton)
GUI.Show (aboutButton)
GUI.Refresh
end mainprog
% this used for input as error trap
procedure none (str : string)
end none
% display actual window
var winID : int := Window.Open ("position:300;300,graphics:400;400,nobuttonbar")
cls
bg
% the human interface is created
innum := GUI.CreateTextFieldFull (maxx - 300, maxy - 57, 50, "", none, GUI.INDENT, 0, 0)
outbox := GUI.CreateTextBox (10, 10, 60, maxy - 50)
progButton := GUI.CreateButtonFull (maxx - 225, maxy - 60, 0, "Start", mainprog, 0, 'S', true)
aboutButton := GUI.CreateButtonFull (maxx - 150, maxy - 60, 0, "About", aboutapp, 0, 'A', true)
quitButton := GUI.CreateButtonFull (maxx - 75, maxy - 60, 0, "Quit", GUI.Quit, 0, 'Q', true)
% loop the interface until the 'quit' is called
loop
exit when GUI.ProcessEvent
end loop
% if upper loop is passed, close program window
Window.Close (winID)