I want to scan prime numbers ,have you logic to solve this problem?
here is example of even numbers:
CODE
-------------------------------------------------------------------------------------------------------------- void main (void) {clrscr(); int a; printf("input any no.");
scanf("%d"&a); if(a%2=0) {printf("the number is even"); getch(); }
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?
CODE
int num; //The number from the user int divisor = Math.floor(Math.sqrt(num)); //Our largest divisor if (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.
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.
CODE
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 <math> header, if C++, look up <cmath> on the internet. A little work won't kill ya.
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?
CODE
int num; //The number from the user int divisor = Math.floor(Math.sqrt(num)); //Our largest divisor if (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 ?
Can anyone write a more efficient code than this to get the prime numbers from 1 -999? //
Assignment: sieve.cpp // Purpose: To write the most efficient program // that outputs prime
numbers. #include #include #include // declare constant MAX_NUMBER to be the # of arrays. const
MAX_NUMBER = 1000; // declare array primes. bool primes ; // function prototypes void
initializeArray(); void findMultiples(); void printSubscripts(); int main() { // call to functions
initializeArray(); findMultiples(); printSubscripts(); return 0; }
//*****************************....
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE
forum, Create your own topics, Ask Questions, track topics, setup
subscriptions & notifications and Get a Free Website w/ Email and FTP.