Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,161,627 members, 7,847,623 topics. Date: Saturday, 01 June 2024 at 10:34 PM

Can Programs Write Primes Number - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Can Programs Write Primes Number (1831 Views)

Java online compiler for swing programs / C/C++ Doctor: Get Help In Writing C/C++ Programs / Step-by-step Method Of Writing Contiki Programs (2) (3) (4)

(1) (Reply) (Go Down)

Can Programs Write Primes Number by jenwaf: 5:48pm On Jun 09, 2007
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
Re: Can Programs Write Primes Number by Fdeveloper(m): 9:00pm On Jun 09, 2007
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
Re: Can Programs Write Primes Number by luckyCO(m): 9:25pm On Jun 15, 2007
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"wink)
       M = Val(InputBox("Enter an End number", "Enter a Number"wink)

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"wink)
       M = Val(InputBox("Enter an End number", "Enter a number"wink)

       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"wink


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.
Re: Can Programs Write Primes Number by Fdeveloper(m): 8:43pm On Jun 16, 2007
@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.
Re: Can Programs Write Primes Number by luckyCO(m): 11:12pm On Jun 16, 2007
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"wink)
M = Val(InputBox("Enter an End number", "Enter a number"wink)

If N > M Then
MsgBox("Second number must be greater than the first", MsgBoxStyle.Information, "Error"wink
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"wink
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"wink

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"wink;
Cando = true;
}
if (N <= 0 || M <= 0)
{
MessageBox.Show("Numbers cannot be zero", "Plz enter correct numbers are continue"wink;
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"wink;
}

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

}
Re: Can Programs Write Primes Number by Fdeveloper(m): 2:39pm On Jun 18, 2007
@luckyCO, Nice one!

@jenwaf,  I see that you have posted this same topic again https://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?
Re: Can Programs Write Primes Number by my2cents(m): 3:22pm On Jun 18, 2007
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.
Re: Can Programs Write Primes Number by Fdeveloper(m): 5:16pm On Jun 18, 2007
@my2cents, I take your point
Re: Can Programs Write Primes Number by luckyCO(m): 5:32pm On Jun 19, 2007
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
Re: Can Programs Write Primes Number by luckyCO(m): 5:33pm On Jun 19, 2007
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
Re: Can Programs Write Primes Number by my2cents(m): 5:40pm On Jun 19, 2007
luckyCO,

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

Later
Re: Can Programs Write Primes Number by jenwaf: 6:09pm On Jun 19, 2007
God bless u lucyco,fdeveloper and 2cent for ur 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
Re: Can Programs Write Primes Number by jenwaf: 3:43pm On Jun 26, 2007
pls u poeple should bear with me and help me out pls, don't be hurt
Re: Can Programs Write Primes Number by luckyCO(m): 5:33am On Jun 27, 2007
jenwaf what do u want us to do for u?
Re: Can Programs Write Primes Number by jenwaf: 8:37am On Jun 27, 2007
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
Re: Can Programs Write Primes Number by jenwaf: 8:45am On Jun 27, 2007
pls I mean basic programming
Re: Can Programs Write Primes Number by luckyCO(m): 5:16pm On Jun 27, 2007
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.
Re: Can Programs Write Primes Number by jenwaf: 1:44pm On Jun 29, 2007
pls can give me the fiow chat of the above lucyco?
Re: Can Programs Write Primes Number by otuonye(m): 10:44pm On Jun 29, 2007
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
Re: Can Programs Write Primes Number by luckyCO(m): 12:20am On Jun 30, 2007
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.
Re: Can Programs Write Primes Number by jenwaf: 11:08am On Jul 02, 2007
Pls lucyco am very soory for disturbing u . You can help with the pseudocode, thanks.
Re: Can Programs Write Primes Number by luckyCO(m): 6:01pm On Jul 02, 2007
Am busy some how, but I will send the code to you
Re: Can Programs Write Primes Number by jenwaf: 8:39am On Jul 03, 2007
thanks very much ,God will reward u more abondantly. A rough for the chat will do
Re: Can Programs Write Primes Number by luckyCO(m): 10:55am On Jul 03, 2007
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
Re: Can Programs Write Primes Number by mosconet(m): 9:48pm On Jul 07, 2007
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
Re: Can Programs Write Primes Number by jenwaf: 5:07pm On Jul 11, 2007
Pls mosco help me ,God will bless u

(1) (Reply)

Connecting An Offline Server To An Online Server / Help! Visual Studio Won't Install / Where Can I Learn Programming In Anambra,awka Precisely

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 62
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.