Computing.Net > Forums > Programming > prime numbers execution problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

prime numbers execution problem

Reply to Message Icon

Name: torrie
Date: July 28, 2005 at 11:39:18 Pacific
OS: winxp
CPU/Ram: 256
Comment:

hi people i am having a great problem in vb.net while trying to make a program about prime numbers. is there is any 1 who will provide me the code. the requirement about it is "the user will enter a number in a textbox and on clicking on a button it will have to tell us that weather the number is prime or not"



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 28, 2005 at 12:03:49 Pacific
Reply:

What code do you have so far?

M2


If at first you don't succeed, you're about average.


0

Response Number 2
Name: torrie
Date: July 28, 2005 at 12:10:37 Pacific
Reply:

no code founded yet
plz help me


0

Response Number 3
Name: Michael J (by mjdamato)
Date: July 28, 2005 at 14:00:07 Pacific
Reply:

Well, there is no formula for generating prime numbers so you need to work backwards.

A prime number can only be divided by itself and one. So a straitforward solution would be to run a loop from 2 to n-1 (where n is the number). Divide the number by the loop count and see if there is a remainder that is not a whole number. If there is no remainder you can stop - the number is not prime. Here is a sample from javascript;

var chkNum = n; //n is the number to check
var type = "prime"; //assume num is prime

for (i=2; i<n; i++) {
//Test if num can be evenly divided
if ( ((n%i)-Math.round(n%i)) == 0 ) {
//Not prime
type = "not prime";
i = n; //end loop
}
}
alert(type);

Now, that is a brute fiorce method since it has to test every single number (at least until it finds a case to prove the hypothesis false). There are ways to refine the search. For example, after you test the number 2, there is no reason to test any other even number. So, you could implemetn a way to skip all the other even numbers and reduce the processing time in half.

Michael J


0

Response Number 4
Name: Michael J (by mjdamato)
Date: July 28, 2005 at 14:11:09 Pacific
Reply:

Ok, shoot me I was thinking too hard, my logic is messed up. You only need to check if there is a remainder, not if the remainder is not a whole number (plus I did not reference the variable chkNum in the loop!) the code should be:

var chkNum = 8951; //n is the number to check
var type = "prime"; //assume chkNum is prime

for (i=2; i<chkNum; i++) {
//Test if num can be evenly divided
if ( (chkNum%i) == 0 ) {
//Not prime
type = "not prime";
i = chkNum; //end loop
}
}
alert(type);

Michael J


0

Response Number 5
Name: borelli35
Date: July 29, 2005 at 18:02:14 Pacific
Reply:

You don't have to loop from 2 to n-1 just 2 to 7...think about it....

John W. Borelli
IT Specialist
Hawkeye Security

Link to my email address


0

Related Posts

See More



Response Number 6
Name: Michael J (by mjdamato)
Date: July 29, 2005 at 22:11:26 Pacific
Reply:

Uh, no. Apparently you do not understand prime numbers. A prime number is a number which can only be evenly divided by itself and the number 1. There are many non-prime numbers that are not divisable by the numbers 2-7.

For instance the number 253 is not evenly divisable by any number from 2 to 7, but it is not a prime number because it can be evenly divided by the numbers 11 & 23 (two other primes). So, Mr. Math Wiz, how would you have determined that 253 is not a prime number just by using the numbers 2 - 7?

And, as I said, that was jst the brute force method. There are many other restrictions that could be put in place to reduce the processing time - for instance there would be no need to go past n/2 because if there was no evenly divisable number below that then there can not be one more than that. But, 2-7, no way, that will not suffice.


0

Response Number 7
Name: borelli35
Date: July 30, 2005 at 22:09:10 Pacific
Reply:

==========================================================
Micheal,

Obviously you are correct and I jumped to the conclusion without fully thinking it through but I would appreciate it (and you will find people more receptive) if you did not respond with such an abrasive attitude. It is uncalled for and I would never even think about belittling anyone to make myself look good. Ask the late Dr. Freud what he would think about it and you might find a thing or two to think about. My personal experience is that people treat others that way because they feel poorly about themselves but that's just one persons point of view. Next time put someone who is at least closer to you down first.

"Mr. Math Wiz"

John W. Borelli
IT Specialist
Hawkeye Security
borelli35


0

Response Number 8
Name: Michael J (by mjdamato)
Date: July 30, 2005 at 22:33:31 Pacific
Reply:

Well, to be honest, I was a little put off by your "...think about it...." comment, as if I had overlooked something obviously simple. A little condescending perhaps? And, had you attempted to explain the reasoning behind your statement, instead of the "think about it" comment, perhaps you would have caught your own error before posting.

So now you are going to try and analyze my personality! Yeah, I feel real poorly about myself. But I feel much better now that I've had a good laugh at your expense.


0

Response Number 9
Name: borelli35
Date: July 31, 2005 at 14:25:04 Pacific
Reply:

It's strange that you should take offense to that when it wasn't for you....

John W. Borelli
IT Specialist
Hawkeye Security

Link to my email address


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: prime numbers execution problem

Find prime numbers in Java? www.computing.net/answers/programming/find-prime-numbers-in-java/11287.html

Prime number checking www.computing.net/answers/programming/prime-number-checking/6158.html

Prime Number in MIPS help www.computing.net/answers/programming/prime-number-in-mips-help/13454.html