Thread: Help with finding only prime numbers with for loops

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Help with finding only prime numbers with for loops

    So I'm new at all of this programming jazz and I could really use some help. The assignment has already been turned in, I would just like to figure this out before we move in class. Teacher isn't in town so I can't ask him for the moment. Could really use your help. Thanks.

    So this is the assignment...

    A prime number is a number greater than 1 which is only evenly divisible by 1 and itself. For this assignment you will find which numbers from 2 to n (where n is a user-specified number) are prime.
    Ask the user for a number, n, greater than 2. Keep asking for a number until a number greater than 2 is provided. Assume that the user will only enter numbers (that is, you do not need to check if a user enters text).
    Use a loop to iterate on a variable, i, from 2 through n. For each iteration, check all numbers from 2 through i to determine whether the number is prime. If it is prime, print out i and the word "Prime".
    Use the modulus operator, %, to determine if a number is prime
    Example output:

    Please enter a number larger than two: -939
    Please enter a number larger than two: 2
    Please enter a number larger than two: 20
    2 Prime
    3 Prime
    5 Prime
    7 Prime
    11 Prime
    13 Prime
    17 Prime
    19 Prime

    This is what I have so far. I suppose i'm not really understanding how to continue with the second for loop. The first one just prints out a list of numbers from 2 to the number designated by the user. In my case that variable is user_input.


    Code:
    // Alen
    // Prime
    //
    
    
    # include <iostream>
    using namespace std;
    int main()
    {
    
    
    int user_input;
    int i;
    int n = 1;
    int total=2;
    
    
    do {
      cout << "Enter a number greater than 2." << endl;
      cin >> user_input;
        } while (user_input<=2);
       
    
    
    
    
      for (i=2; i < user_input; i++)
      {
          } 
    
    
    
         
      
    
    
    
    
    
    
    
    
     
    
    
    
    
    
    
    
    
    
    
      return 0;
      
    }




  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > The assignment has already been turned in
    If you've posted what you turned in, you just got either D- or F.

    > Use a loop to iterate on a variable, i, from 2 through n.
    OK, you've done this

    > For each iteration, check all numbers from 2 through i
    Now, what would you do here?

    > Use the modulus operator, %, to determine if a number is prime
    Or this

    Have you looked on say wikipedia to see how the simple prime number algorithm works?
    There's even a pretty animation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    > The assignment has already been turned in
    If you've posted what you turned in, you just got either D- or F.
    >I'm not disputing that.

    > Use a loop to iterate on a variable, i, from 2 through n.
    OK, you've done this
    > Cool.
    > For each iteration, check all numbers from 2 through i
    Now, what would you do here?
    > I have no idea.
    > Use the modulus operator, %, to determine if a number is prime
    Or this
    > This % doesn't help if I don't know where to place it/ how to use it to get required results.

    Have you looked on say wikipedia to see how the simple prime number algorithm works?
    There's even a pretty animation.


  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    I want to modulate the user_input by every number less than the value of user_input. I just don't understand how to code it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > For each iteration, check all numbers from 2 through i
    You mean like
    for ( candidatePrime = 2 ; candidatePrime < i ; candidatePrime++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Could you explain how that differs from what I have up top?
    for (i=2; i < user_input; i++)

    Essentially all that does is list the numbers from 2 to n.

    How do I go about dividing each of the listed numbers; by every number in the list that is less than it?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It differs because the instructions read

    > Use a loop to iterate on a variable, i, from 2 through n. For each iteration (of i from 2 to n), check all numbers from 2 through i
    Means your loops look like this
    Code:
    for (i=2; i < user_input; i++) {
        for ( j = 2 ; j < i ; j++ ) {
            // now trial divide i with each j in turn
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    So we needed another variable with the same value as i ?

    I'm not sure I understand...

    Code:
    // Alen
    // Prime
    //
    
    
    # include <iostream>
    using namespace std;
    int main()
    {
    
    
    int user_input;
    int i;
    int n = 1;
    int total;
    
    
    do {
      cout << "Enter a number greater than 2." << endl;
      cin >> user_input;
        } while (user_input<=2);
       
    
    
    
    
      for (i=2; i < user_input; i++)
      { 
        for (total=2; total< i; total++)
        {cout <<"" << endl;
          if (total%i!=0);
            cout<<total%i << "Prime" << endl;
    
    
    
    
      
    
    
      } 
      } 
    
    
         
      
    
    
    
    
    
    
    
    
     
    
    
    
    
    
    
    
    
    
    
      return 0;
      
    }

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    I had to have missed something in class, this assignment can't be this difficult.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if (total%i!=0);
    1. The ; at the end makes the if statement redundant.
    2. Check which way round the trial division goes.


    Perhaps it would be better with meaningful variable names
    Code:
    for ( possiblePrime = 2 ; possiblePrime < n ; possiblePrime++ ) {
        for ( trialDivisor = 2 ; trialDivisor < possiblePrime ; trialDivisor++ ) {
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Okay I swapped the locations of the two variables.

    My cout statement still isn't giving me meaningful numbers though. I'm sure this is getting tiresome, but I appreciate all the help.

    Code:
    // Alen
    // Prime
    //
    
    
    # include <iostream>
    using namespace std;
    int main()
    {
    
    
    int user_input;
    int i;
    int n = 1;
    int total;
    
    
    do {
      cout << "Enter a number greater than 2." << endl;
      cin >> user_input;
        } while (user_input<=2);
       
    
    
    
    
      for (i=2; i < user_input; i++)
      { 
        for (total=2; total< i; total++)
        {cout <<"" << endl;
          if (total%i!=0);
            cout<<total%i << "Prime" << endl;
    
    
    
    
      
    
    
      } 
      } 
    
    
         
      
    
    
    
    
    
    
    
    
     
    
    
    
    
    
    
    
    
    
    
      return 0;
      
    }

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    I think i got it. Thanks for all your help Salem, it is highly appreciated. Apparently bools are required for this. Would have saved me hours of trying.

    Thanks for everything sir.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding prime numbers
    By stdq in forum C Programming
    Replies: 3
    Last Post: 01-28-2012, 12:51 PM
  2. Finding Prime Numbers
    By alireza beygi in forum C Programming
    Replies: 2
    Last Post: 01-13-2012, 04:06 AM
  3. Finding Prime Numbers
    By dan724 in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 12:12 PM
  4. Finding and Printing prime numbers using arrays
    By Sektor in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2003, 08:29 PM