Thread: Finding prime numbers in a loop

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    1

    Post Finding prime numbers in a loop

    I am trying to figure out how to create a program consisting of a player tossing two dices, then the value of the dices are added up, and then I must determine if the number is considered a prime number. The sequence must be done 500 times.


    I must test each integer I receive from the dice between 1 and sqrt(n) using modulus. If the number is prime, I return 1, otherwise, return 0. I must count every time a number is prime until the dice has rolled its 500th time. If the number is prime, the player wins and it gets counted in the program.
    (sqrt(n) is the value of the dice rolls)

    This is what I have so far:

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    //This program is to determine how many times a player wins by rolling two dice adding up to a prime number 500 times
    main()
    {
    int n = 0, die1 = 0, die2 = 0, roll_count = 0, playerwin = 0, i;
    while (roll_count <= 500)//Number of times dice will roll
    {
    srand(time(NULL));
    die1 = rand()%6+1; //randomizing the first die
    srand(time(NULL));
    die2 = rand()%6+1; //randomizing the second die
    n = die1 + die2; //Adding the randomized values together
    for (i = 2;i <= sqrt(n); i++) //i is the integer being tested on n
    {
    if (n%i==0) //if they get even, playerwin gets added 1 and return 1
       {
       playerwin++;
       return 1;
       }
    if (i == 11) //If integer exceeds 11 which eliminates any possible  
                      even divisible between 2-11, it is considered not prime
                      and returns 0
       {
       return 0;
       }
    }
    roll_count++; //After the sequence is done, roll_count gets counted 
                         the and loop starts again
    }
    cout << "Number of times player has won is: " << playerwin << endl;             // Counted playerwin will be displayed
    return 0;
    }
    I'm fairly new to C++ and for this program, the output is blank. I am in need of help. Thank you.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I think you need to learn how to write and use a function.

    Functions in C and C++ - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to format, especially indent, your code properly. For example:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    
    using namespace std;
    
    //This program is to determine how many times a player wins by rolling two dice adding up to a prime number 500 times
    main()
    {
        int n = 0, die1 = 0, die2 = 0, roll_count = 0, playerwin = 0, i;
        while (roll_count <= 500)//Number of times dice will roll
        {
            srand(time(NULL));
            die1 = rand() % 6 + 1; //randomizing the first die
            srand(time(NULL));
            die2 = rand() % 6 + 1; //randomizing the second die
            n = die1 + die2; //Adding the randomized values together
            for (i = 2; i <= sqrt(n); i++) //i is the integer being tested on n
            {
                if (n % i == 0) //if they get even, playerwin gets added 1 and return 1
                {
                    playerwin++;
                    return 1;
                }
                if (i == 11) //If integer exceeds 11 which eliminates any possible 
                even divisible between 2-11, it is considered not prime
                and returns 0
                {
                    return 0;
                }
            }
            roll_count++; //After the sequence is done, roll_count gets counted
            the and loop starts again
        }
        cout << "Number of times player has won is: " << playerwin << endl;             // Counted playerwin will be displayed
        return 0;
    }
    A few things to note:
    • <stdlib.h> is a C header. The C++ header is <cstdlib>
    • <math.h> is a C header. The C++ header is <cmath>
    • You forgot the return type for the main function, i.e., it should be int main
    • You called srand twice, and moreover called it with a loop body! srand should only be called once, near the start of the main function.
    • You used time, which means that you should #include <ctime>

    You are apparently simulating two six sided dice on each iteration, so the range of their sum is [2, 12]. The primes within that range are well known: 2, 3, 5, 7, 11. These are few enough that you can check them directly. Alternatively, you can "test each integer I receive from the dice between 1 and sqrt(n) using modulus" by pre-computing the maximum prime divisor other than the prime itself for integers in that range, i.e., 3. This way, you just check if the number is greater than 3 and is divisible by 2 or 3: if so, it is composite, otherwise it is prime. An explicit computation of the square root is definitely unnecessary (i.e., you don't need <cmath> at all).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is also possible to create an array with all the primes between 1 and 12 inclusive. Then simply check if a value is in that array to determine if it is prime.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Divisers of non-prime numbers!
    By Frank1234 in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2013, 02:53 PM
  2. Help with finding only prime numbers with for loops
    By AlmondSeason in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2013, 12:51 AM
  3. Finding prime numbers
    By stdq in forum C Programming
    Replies: 3
    Last Post: 01-28-2012, 12:51 PM
  4. Finding Prime Numbers
    By alireza beygi in forum C Programming
    Replies: 2
    Last Post: 01-13-2012, 04:06 AM
  5. Finding Prime Numbers
    By dan724 in forum C Programming
    Replies: 11
    Last Post: 12-14-2008, 12:12 PM

Tags for this Thread