xisto Community

# How I Shall Scan Prime Numbers.. that is 1,3,5,7,11,13,17,19...

## Recommended Posts

I want to scan prime numbers ,have you logic to solve this problem?

here is example of even numbers:

`--------------------------------------------------------------------------------------------------------------void main (void){clrscr();int a;printf("input any no.");scanf("%d"&a);if(a%2=0){printf("the number is even");getch();}------------------------------------------------------------------------------------------------------------`
Now I want to scan prime numbers and I dont have logic please help me to solve my problem.

Notice from serverph:

##### Share on other sites

The only logic I can think of is to divide the number by all numbers up to the square root of the original number and check the modulus of each one. So, get the number from the user. Then do the floor of the square root of that number. This is the highest value you need to divide by. Then create a loop, starting with that divisor, decreasing by one each time, stopping when it is equal to 1. Get the modulus of the number divided by the divisor. If at any point you get a 0 value, end the loop and output that the number is not prime. If it finishes with no 0 values then the number must be prime.

Sorry, I don't know enough C to write the code, but I could do it in Java if you could sort of translate from that?

`int num; //The number from the userint divisor = Math.floor(Math.sqrt(num)); //Our largest divisorif (num == 2)	return true;else if (num < 2)	return false;for (i = 2; i <= divisor; i++) {	if (num % i == 0)		return false;}return true;`

If it needs more explaining (which it probably does ) then feel free to ask.

##### Share on other sites

rvalkass is basically right, just divide your test number by every prime number up till the square root of the test number, and if none of them make an even division, the test number is prime. Of course, use some common sense in doing this. Don't waste time testing even numbers, and use algorithms like the digits of x add up to a multiple of 3 if x is divisible by 3 and so on. Remember, division is the lengthiest common arithmetic process to do. If you want your 'scan' to go faster, you'll need better algorithms.

`int divisor = Math.floor(Math.sqrt(num)); //Our largest divisor`
Lol. That's about the only thing you might need a little help translating to C/C++.
If you're using C, look up the [itex] header, if C++, look up <cmath> on the internet.
A little work won't kill ya.

##### Share on other sites

The only logic I can think of is to divide the number by all numbers up to the square root of the original number and check the modulus of each one. So, get the number from the user. Then do the floor of the square root of that number. This is the highest value you need to divide by. Then create a loop, starting with that divisor, decreasing by one each time, stopping when it is equal to 1. Get the modulus of the number divided by the divisor. If at any point you get a 0 value, end the loop and output that the number is not prime. If it finishes with no 0 values then the number must be prime.
Sorry, I don't know enough C to write the code, but I could do it in Java if you could sort of translate from that?

`int num; //The number from the userint divisor = Math.floor(Math.sqrt(num)); //Our largest divisorif (num == 2)	return true;else if (num < 2)	return false;for (i = 2; i <= divisor; i++) {	if (num % i == 0)		return false;}return true;`

If it needs more explaining (which it probably does ) then feel free to ask.

Okay

but I have to use only "if-else" statement not "for" statement.Have you any tips ?

##### Share on other sites

I think a loop would be needed , unless you have a function like this :

`boolean isPrime (int num,int divisor){   if (num % divisor == 0)	   return false;   if (num / 2 < divisor ) 	   return true;    else	   isPrime(num,++divisor);}`

Edited by Jdeveloper (see edit history)

## Create an account

Register a new account