|
jenwaf
|
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
|
|
|
|
|
|
|
|
luckyCO
|
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)
|
@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
|
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)
|
@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)
|
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)
|
@my2cents, I take your point
|
|
|
|
|
|
luckyCO
|
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
|
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)
|
luckyCO, not at all. I was just making a general statement. In some ways, I feel I should be apologizing to you  Later
|
|
|
|
|
|
jenwaf
|
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
|
pls u poeple should bear with me and help me out pls, don't be hurt
|
|
|
|
|
|
luckyCO
|
jenwaf what do u want us to do for u?
|
|
|
|
|
|
jenwaf
|
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
|
pls I mean basic programming
|
|
|
|
|
|
luckyCO
|
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
|
pls can give me the fiow chat of the above lucyco?
|
|
|
|
|
|
otuonye (m)
|
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
|
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
|
Pls lucyco am very soory for disturbing u . You can help with the pseudocode, thanks.
|
|
|
|
|
|
luckyCO
|
Am busy some how, but I will send the code to you
|
|
|
|
|
|
jenwaf
|
thanks very much ,God will reward u more abondantly. A rough for the chat will do
|
|
|
|
|
|
luckyCO
|
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)
|
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
|
Pls mosco help me ,God will bless u
|
|
|
|
|
|