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

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

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

alamzaib
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();
}

------------------------------------------------------------------------------------------------------------

Now I want to scan prime numbers and I dont have logic please help me to solve my problem.
thanks in advance. smile.gif

Notice from serverph:
added code tags... please use appropriate code tags as needed. thanks.

 

 

 


Reply

rvalkass
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 tongue.gif ) then feel free to ask.

 

 

 


Reply

osknockout
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. biggrin.gif

Reply

alamzaib
QUOTE(rvalkass @ Feb 15 2007, 07:36 PM) *
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 tongue.gif ) then feel free to ask.




Okay smile.gif

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

Reply

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

CODE
boolean isPrime (int num,int divisor){
   if (num % divisor == 0)
       return false;

   if (num / 2 < divisor )
       return true;
   else
       isPrime(num,++divisor);
}

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. how to scan numbers - 4.14 hr back. (1)
  2. prime code in java test number - 15.90 hr back. (1)
  3. how to return prime number in an array java - 17.22 hr back. (1)
  4. 1 to 100 prime numbers in c - 36.66 hr back. (1)
  5. c# code for testing if a number is prime - 52.19 hr back. (1)
  6. explainig prime number in math - 60.69 hr back. (1)
  7. prime no prog without division of modulus in java - 68.38 hr back. (1)
  8. c# get prime numbers - 73.54 hr back. (1)
  9. logic for prime number in java - 74.78 hr back. (1)
  10. program to generate prime factors in java - 74.81 hr back. (1)
  11. how to check for prime numbers c program - 92.02 hr back. (1)
  12. c program that outputs prime numbers - 100.02 hr back. (2)
  13. how to write a java program for even numbers up to 100 - 102.05 hr back. (1)
  14. java find highest prime number - 118.91 hr back. (1)
Similar Topics

Keywords : san, prime, numbers, 1, 3, 5, 7, 11, 13, 17, 19,

  1. I Need Help
    it's about roman numbers in c/c++ (7)
  2. Most Efficient Code To Get Prime Numbers
    (7)
    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; }
    //*****************************....
  3. Need Help With C Program To Test If A Number Is Prime
    Ending unexpectedly somewhere near for-loop (12)
    CODE #include <stdio.h> main() {       printf("Enter a number:
    ");              int n;       scanf("%d", &n);              if(n ==
    2)            printf("%d is prime", n);       else if(n % 2 == 0 || n <
    2)            printf("%d is not prime", n);       else       {            int x;
               for(x = 0; x < (int)sqrt((double)n); x++)
                    if(n % x == 0)                 {                      printf("%....

    1. Looking for san, prime, numbers, 1, 3, 5, 7, 11, 13, 17, 19,

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for san, prime, numbers, 1, 3, 5, 7, 11, 13, 17, 19,

*MORE FROM TRAP17.COM*
advertisement



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



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
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.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE