Can Programs Write Primes Number

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 24, 2009, 04:50 PM
431962 members and 298931 Topics
Latest Member: Irozuruike
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Programming  |  Can Programs Write Primes Number
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Can Programs Write Primes Number  (Read 501 views)
jenwaf
Can Programs Write Primes Number
« on: June 09, 2007, 05:48 PM »

Help!!!!!!!!!!! I need some one that well help me with a program that can count prime number from n to m. please some one in the house should help .my e-mail is  joenwaf@yahoo.com
Fdeveloper (m)
Re: Can Programs Write Primes Number
« #1 on: June 09, 2007, 09:00 PM »

You didn't say if you were looking for a specific language however the good thing is as this is one of the topics covered in just about every tutorial for every programming language there are many resources available on the internet and here are just a couple of URLs:

Java http://java.sun.com/developer/JDCTechTips/2002/tt0806.html
C# http://www.codeproject.com/cs/algorithms/highspeed_primenumbers.asp?df=100&forumid=93480&exp=0&select=2046265

You may also want to check out some of the following results returned by Google:
http://www.google.com/search?hl=en&newwindow=1&safe=on&q=prime+number+generator+source+code
luckyCO
Re: Can Programs Write Primes Number
« #2 on: June 15, 2007, 09:25 PM »

QBasic

cls
Dim N as Long,M as Long,Sum as Long
Input "Enter First Number  ",N
Input "Enter Second Range Number " ,M
Dim Divisor as Integer
Divisor=int(m/3)
if M<100 then divisor=7

For j=N to M
For I = 2 to Divisor
if J=I then 20
if J/I-Int(J/I)=0 then 40
20 Next I
sum=sum+1
40 Next J

Print "Prime number found from " + Str$(N) + " to " + STR$(M) + " is " + str$(sum)


VB 6.0
Dim N As Long, M As Long, Sum As Long

       N = Val(InputBox("Enter a starting number", "Enter a number"))
       M = Val(InputBox("Enter an End number", "Enter a Number"))

Dim J As Long
Dim I As Long, Divisor As Integer
Divisor = Int(M / 3)

If M < 100 Then Divisor = 7

For J = N To M
For I = 2 To Divisor
If J = I Then GoTo 20
If J / I - Int(J / I) = 0 Then GoTo 40
20 Next I
Sum = Sum + 1
40 Next J

MsgBox "The number of prime Number in from " & N & " to " & M & " is " & Sum, vbInformation, "Result"


Vb.Net

Dim N, M As Long, Sum As Long

       N = Val(InputBox("Enter a starting number", "Enter a number"))
       M = Val(InputBox("Enter an End number", "Enter a number"))

       Dim J As Long
       Dim I As Long, Divisor As Integer
       Divisor = Int(M / 3)

       If M < 100 Then Divisor = 7

       For J = N To M
           For I = 2 To Divisor
               If J = I Then GoTo 20
               If J / I - Int(J / I) = 0 Then GoTo 40
20:         Next I
           Sum = Sum + 1
40:     Next J

       MsgBox("The number of prime Number in from " & N & " to " & M & " is " & Sum, MsgBoxStyle.Information, "Result")


C#

Drop a Command Button, Two textboxes.
Copy code below and paste it in the COmmand Buttpn's Click event

long N, M;
           N = long.Parse(textBox1.Text);
           M = long.Parse(textBox2.Text);



           int Divisor = Convert.ToInt32(M / 3);
           if (M < 100)
           {
               Divisor = 7;
           }

           
           long SUM = 0;
           for (long J = N; J <= M; J++)
           {
               
               for (long I = 2; I <= Divisor; I++)
               {
                   
                   
                   if (J != I)
                   {
                       if (J % I != 0)
                       {
                           if (I == Divisor) { SUM += 1; }

                       }
                       else
                           J++;
                   }
               }


           }

           MessageBox.Show("Prime number found from  " + N.ToString () + " to " + M.ToString () + " is " + SUM.ToString());

I wrote it in diff languages since u didnt specify


I hope this helps you.






Fdeveloper (m)
Re: Can Programs Write Primes Number
« #3 on: June 16, 2007, 08:43 PM »

@luckyCO, I hope you won't think that I'm being unnecessarily critical however I see a few problems with your C# & VB.NET sample code as follows:

- If you enter the same non prime number in both text boxes, the application still reports the number of prime numbers found as 1 which I guess is due to your choice of divisor.
- Your code assumes that the value entered in the 2nd text box will always be greater than the value entered in the 1st box
- Your code allows me to enter negative values whereas I believe the general consensus is that negative numbers cannot be prime.
- Your code does not check to ensure that a numeric value was entered in both text boxes and in fact there is no error handling at all

I appreciate that your intention was just to provide an example however I still think it's helpful to ensure that there are no basic errors in the code or make it clear that it is only an example and not meant to be bullet proof.
luckyCO
Re: Can Programs Write Primes Number
« #4 on: June 16, 2007, 11:12 PM »

Fdeveloper, thanks for your observation. I wasnt deleveloping a full application at same time was thinking that since the person is already a programmer he can validate the values.

Anyway see the correction below;


VB.Net

Dim N, M As Long, Sum As Long

        N = Val(InputBox("Enter a starting number", "Enter a number"))
        M = Val(InputBox("Enter an End number", "Enter a number"))

        If N > M Then
            MsgBox("Second number must be greater than the first", MsgBoxStyle.Information, "Error")
            Exit Sub
        End If
        If M <= 0 Or N <= 0 Then
            MsgBox("Both numbers should not be less or equal to zero", MsgBoxStyle.Information, "Error")
            Exit Sub
        End If

        Dim J As Long
        Dim I As Long, Divisor As Integer
        Divisor = Int(M / 3)

        If M < 100 Then Divisor = 7

        For J = N To M
            For I = 2 To Divisor
                If J = I Then GoTo 20
                If J / I - Int(J / I) = 0 Then GoTo 40
20:         Next I
            Sum = Sum + 1
40:     Next J

        MsgBox("The number of prime Number in from " & N & " to " & M & " is " & Sum, MsgBoxStyle.Information, "Result")

C#

try
            {
                bool Cando = false;

                long N, M;
                N = long.Parse(textBox1.Text);
                M = long.Parse(textBox2.Text);


                if (M < N)
                {
                    MessageBox.Show("First number must be less than the second one", "Plz enter correct numbers are continue");
                    Cando = true;
                }
                if (N <= 0 || M <= 0)
                {
                    MessageBox.Show("Numbers cannot be zero", "Plz enter correct numbers are continue");
                    Cando = true;
                }


                if (Cando == false)
                {


                    int Divisor = Convert.ToInt32(M / 3);
                    if (M < 100)
                    {
                        Divisor = 7;
                    }


                    long SUM = 0;
                    for (long J = N; J <= M; J++)
                    {
                        bool K = false;
                        for (long I = 2; I <= Divisor; I++)
                        {

                            if (J != I)
                            {
                                if (J % I == 0)
                                {
                                    I = Divisor;
                                    K=true ;
                                }

                            }
                        }

                        if (K==false) {  SUM += 1; }
                                   

                    }





                    MessageBox.Show("Prime number found from  " + N.ToString() + " to " + M.ToString() + " is " + SUM.ToString());

                }

            }

            catch (FormatException)
            {
                MessageBox.Show("Only numbers are needed in both boxes");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
Fdeveloper (m)
Re: Can Programs Write Primes Number
« #5 on: June 18, 2007, 02:39 PM »

@luckyCO, Nice one!

@jenwaf,  I see that you have posted this same topic again http://www.nairaland.com/nigeria/topic-60433.0.html despite the fact that luckyCO's sample code above demonstrates a solution to the problem.

Is it specifically writing the solution in C++ and/or Pasal that's the issue?
my2cents (m)
Re: Can Programs Write Primes Number
« #6 on: June 18, 2007, 03:22 PM »

I don't think any of these solutions will help jenwaf unless we get from him the language he is using or if he just wants it in pseudocode. 

On the other hand, perhaps jenwaf should come up with some pseudocode which we could then help fine-tune rather than come up with the solutions entirely ourselves and with no input from him/her.  I don't think jenwaf will learn that way.
Fdeveloper (m)
Re: Can Programs Write Primes Number
« #7 on: June 18, 2007, 05:16 PM »

@my2cents, I take your point

luckyCO
Re: Can Programs Write Primes Number
« #8 on: June 19, 2007, 05:32 PM »

Anyway My2cent I hope I have not offended you by attending to the question? My posting  the code is not only for the person that asked the question jenwaf , but for some other person(s) who may be looking for such a solution who can conveniently copy the code and use and it becomes one of my contributions to them that is why I wrote it in four diff languages.

Even if it is not what he wants, he could as well tell us what he wants and we help him. I think from what he posted becomes the solution in terms of code.

Thanks
luckyCO
Re: Can Programs Write Primes Number
« #9 on: June 19, 2007, 05:33 PM »

Anyway My2cent I hope I have not offended you by attending to the question? My posting  the code is not only for the person that asked the question jenwaf , but for some other person(s) who may be looking for such a solution who can conveniently copy the code and use and it becomes one of my contributions to them that is why I wrote it in four diff languages.

Even if it is not what he wants, he could as well tell us what he wants and we help him. I think from what he posted becomes the solution in terms of code.

Thanks
my2cents (m)
Re: Can Programs Write Primes Number
« #10 on: June 19, 2007, 05:40 PM »

luckyCO,

not at all.  I was just making a general statement.  In some ways, I feel I should be apologizing to you Cheesy

Later
jenwaf
Re: Can Programs Write Primes Number
« #11 on: June 19, 2007, 06:09 PM »

God bless u lucyco,fdeveloper and 2cent for your contributions .but Istill u to bring it down in a simple c or pascal lang.For am not aguru like u ,bear with me,thanks
jenwaf
Re: Can Programs Write Primes Number
« #12 on: June 26, 2007, 03:43 PM »

pls u poeple should bear with me and help me out pls,  don't be hurt
luckyCO
Re: Can Programs Write Primes Number
« #13 on: June 27, 2007, 05:33 AM »

jenwaf  what do u want us to do for u?
jenwaf
Re: Can Programs Write Primes Number
« #14 on: June 27, 2007, 08:37 AM »

pls lucyco I want it in a  simple c or pascal programming language not vb  and not  c++ for me to understand it more better.Thanks for your kindness and consideration, be happy
jenwaf
Re: Can Programs Write Primes Number
« #15 on: June 27, 2007, 08:45 AM »

pls I mean basic programming
luckyCO
Re: Can Programs Write Primes Number
« #16 on: June 27, 2007, 05:16 PM »

It is been long I programmed on C and pascal. I will find it difficult to code on them. I can explain what all the codes and what they do such that you will think like c or pascal person and convert them. But if u insist and could not get help on that I will charge you for my biz time trying to code on it.
jenwaf
Re: Can Programs Write Primes Number
« #17 on: June 29, 2007, 01:44 PM »

pls can give me the fiow chat of the above lucyco?
otuonye (m)
Re: Can Programs Write Primes Number
« #18 on: June 29, 2007, 10:44 PM »

It is like this is an assignment given to you by lecturer in your university. That is why you are asking us to code in the language the lecturer specified.


Cheers
Jojo
luckyCO
Re: Can Programs Write Primes Number
« #19 on: June 30, 2007, 12:20 AM »

Yes I can give you the flow chart. But I dont think I will be able to attach it here because it might be larger unless u said pseudocde.

If the flowchart, you may exceed to consume my biz time which u may have to pay me for doing so. Unless on the process it did not take too much time from me.
jenwaf
Re: Can Programs Write Primes Number
« #20 on: July 02, 2007, 11:08 AM »

Pls lucyco am very soory for disturbing u . You can help with the pseudocode, thanks.
luckyCO
Re: Can Programs Write Primes Number
« #21 on: July 02, 2007, 06:01 PM »

Am busy some how, but I will send the code to you
jenwaf
Re: Can Programs Write Primes Number
« #22 on: July 03, 2007, 08:39 AM »

thanks very much ,God will reward  u more abondantly. A rough for the chat will do
luckyCO
Re: Can Programs Write Primes Number
« #23 on: July 03, 2007, 10:55 AM »

Accept two range of numbers N,M
convert those number to long interger type
Divide the last range number by 3 Comment: this is an abitrary number. If n=1 and M=1000.
There is no how you divide 1-1000 with 1-333 and a number that is non-prime with scale through.

check if M is less than 100 make the divisor 7 to aviod wating processor time.
Store 7 or M/3 in a variable called divisor

loop from N to M while
loop from 1 to divisor

if on each loop the value of N to M and 1 to divisor is the same goto next step in N to M

if the value of of N-M/1-divisor minus integer of the value is equal to zero then goto the
next value of 1-Divisor meaning that it is not prime

loop till the end of divisor

add the number N-M since it refused to be trapped as non-prime. Meaning it is a prime number

loop till the end of M


Display The Result

I hope it helps
mosco.net (m)
Re: Can Programs Write Primes Number
« #24 on: July 07, 2007, 09:48 PM »

Hi, its is pretty very simple to write a prime generator or counter, am out of time now but next i will post it. either c, c#, or java anyone . bye
jenwaf
Re: Can Programs Write Primes Number
« #25 on: July 11, 2007, 05:07 PM »

Pls mosco help me ,God will bless u
 Need Advice On Oracle Course  Who Writes In Basic?  Who Earns More : Web Designer Or Application Developer  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.