Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Prime Number Finder
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
doc




PostPosted: Sun Jan 28, 2007 3:10 pm   Post subject: Prime Number Finder

My first Program Final Program in Turing

I got a lame final project at school for computer science so i did it user friendly. Dam it my friend got a diferent one witch was a game.

Any way it asked me to make a prog that finds the prime numbers. Instead of using just code that he teached the class i added user interface for drag and click people. :D

I attached the compiled program and the code with no quotes to the post.

Here is code with quotes that explain most stuff:
code:

% 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)



Prime Number Finder.zip
 Description:
Prime Number Finder final compiled program and code.

Download
 Filename:  Prime Number Finder.zip
 Filesize:  325.96 KB
 Downloaded:  146 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Bored




PostPosted: Sun Jan 28, 2007 4:17 pm   Post subject: Re: Prime Number Finder

Yes your program works, yes it used GUI, but the point of the task was to create a program for finding prime number, which means the best program is the most efficient. Your program could do this task a lot quicker if you used a little brain power. First off, rather then going anc checking everynumber from 2 to itself, you only need to ceck every number from 2 to itself div 2. That is it can't be evenly divided by numbers more then half of itself. Secondly, the way you have gone about it is the slowest possible way. The more efficient way would be to cycle through all the number from 2 to your max div 2, then for each number flag all it's multiples as not prime. This can be done by using for and by. Now if a numbers been flaged as not prime you can skip it as all of it's multiple have then been flagged. This reduces the time taken hugely especially with larger numbers. Try immplementing this and resubmitting.
Skynet




PostPosted: Sun Jan 28, 2007 5:15 pm   Post subject: Re: Prime Number Finder

Bored @ Sun Jan 28, 2007 4:17 pm wrote:
Yes your program works, yes it used GUI, but the point of the task was to create a program for finding prime number, which means the best program is the most efficient. Your program could do this task a lot quicker if you used a little brain power. First off, rather then going anc checking everynumber from 2 to itself, you only need to ceck every number from 2 to itself div 2. That is it can't be evenly divided by numbers more then half of itself. Secondly, the way you have gone about it is the slowest possible way. The more efficient way would be to cycle through all the number from 2 to your max div 2, then for each number flag all it's multiples as not prime. This can be done by using for and by. Now if a numbers been flaged as not prime you can skip it as all of it's multiple have then been flagged. This reduces the time taken hugely especially with larger numbers. Try immplementing this and resubmitting.

Correct advice or not...that's an abrupt way of wording what should be "constructive criticism."
BenLi




PostPosted: Mon Jan 29, 2007 2:37 pm   Post subject: RE:Prime Number Finder

actually, the way it was said was very kind. You have to know that people are here with good intentions. He did not bash your program, he simply made suggestions
Bored




PostPosted: Mon Jan 29, 2007 6:07 pm   Post subject: RE:Prime Number Finder

That's one of the major disadvantages of the internet, the lack of general tone, and it's lead a many a times to misinterpretation. It's sometimes very hard to tell wether someones being sincere, and ass, sarcastic, or whatever by text alone, as generally 55% of comunication is body language (more for french people lol), 38% tone, and only 7% the actual words we use. Basically the internet tends to remove context leaving only the facts, and facts without context are rairly helpfull. In this case the facts being words (though things I say may not always be fact, it's fact that I say them), and the context being tone and body language.

So what I'm trying to say is that was meant to be sincere and helpfull, not condesending and insulting. Try to imagine my voice as calm, and rather encouraging, as usually on this site and generally in real life that's what I try to be.
BenLi




PostPosted: Mon Jan 29, 2007 11:26 pm   Post subject: Re: Prime Number Finder

Quote:
Try to imagine my voice as calm, and rather encouraging, as usually on this site and generally in real life that's what I try to be.


Infact, that goes for most other people on this site as well
Cervantes




PostPosted: Tue Jan 30, 2007 12:28 am   Post subject: RE:Prime Number Finder

Except for me. My voice is rough and satanic sounding. You know, the kind of voice that can stir ghouls from their resting places. The internet does wonders for my ability to talk to people without them turning heel and bolting.

Look, there's an example of sarcasm! If you got that, you've proven that my intentions can make it past the maze that is the internet.
Clayton




PostPosted: Tue Jan 30, 2007 1:01 pm   Post subject: Re: Prime Number Finder

Offtopic:
And yet that has nothing to do with you suggesting you can stir ghouls from the dead? Razz


Generally yes, consider people here willing to help and not trying to condescend you, rather trying to give you constructive criticism. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Skynet




PostPosted: Tue Jan 30, 2007 2:45 pm   Post subject: Re: Prime Number Finder

One note: I was not offended, as it wasn't my topic, nor my program. Smile However, assuming I was new(er) to the site and unfamiliar with the helpful nature of people here, the barrage of imperative statements could have offended me. Since the internet does hide a certain amount of "tone" and makes things appear to be more blunt then they really are, someone answering questions such as these should word their statements in a way to avoid the reader thinking that they're being "talked down to."
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: