Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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"

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 primefor (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

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 primefor (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

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

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.

==========================================================
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

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.

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

![]() |
![]() |
![]() |

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