KingShahmoo
KingShahmoo

Reputation: 53

Prime number nested loop Logic

I am currently working on assignment which is stated below. My question is that why does my code only repeat prime number as 2 and not the remainder of the numbers. I would much appreciate if someone could help me to walk through the logic so i can try to solve the solution, rather than posting the answer outright. Kudos to all :)

Write a program that uses two nested for loops and the modulus operator (%) to detect and print the prime numbers from 1 to 10,000. (Prime numbers are natural numbers that are not evenly divisible by any other number except for themselves and one). Display all the primes found.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> n; // will store our values from 1-10000

    for (int i= 0; i < 10000; i++) { // vector loading
       n.push_back(i);
       n[i]= n[i] + 1;

       for (int j= 1; j < 10000  ; j++ ) { // Error is here?
           if (n[j] % j == 0) { // supposed to go through all the numbers 
                                // and flag the prime numbers
               cout<<n[i]  <<" is a prime";
               i++;
               break;
            }
        }
    }

    return 0;
}

Upvotes: 1

Views: 2140

Answers (4)

Do not need to iterate through the entire range for inner loop, for inner loop there will possible values are starting from 2 to <= that number /2 .

Like if you want to check whether 99 is prime or not then you need to set inner loop from 2 to 49 (99 / 2 is the max possible factor for that number) so do not iterate through the rest all.

So if you iterate inner loop from 2 to 98 then it's meaning less to iterate this loop after 49, think about it.

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //vector<int> n; // will store our values from 1-10000
    int flag  =0;
    for (int i= 2; i < 10000; i++) // vector loading
    {
        //n.push_back(i);
        //n[i]= n[i] + 1;
        flag  =0;
        for (int j= 2; j <= i / 2  ; j++ ) 
        {
            if (i % j == 0)             
            {
                flag = 1;
                break;
            }

        }
        if(flag == 0)
        {
            cout << "%d number is Prime" << i;
        }
    }
return 0;
}

Upvotes: 0

smac89
smac89

Reputation: 43088

Understand the purpose of your loops

  • The outer loop is the one that supplies the numbers, therefore you don't need the vector

  • The inner loop is the one that does the checking

Where do you print?

  • The inner loop checks if a number is not prime. The only way it knows this is if the number supplied by the outer loop is not divisible by any number supplied by the inner loop. Therefore the inner loop does not print anything because it has to exhaust all checks before it knows that a number is prime

  • Your print statement should come last. This means it should be after the inner loop but still inside the outer loop and it should print the number if it is prime based on what was discovered by the inner loop. So you should have a way of knowing if the inner loop found a prime or not

Finally note that the inner loop has to start at 2 and end at i - 1 because every number is divisible by 1 and itself

Upvotes: 1

Jaege
Jaege

Reputation: 1863

The trivial method is rather easy to understand.

  1. Outer loop (suppose the loop variable here is num) go through from 2 to 10000, and check every number whether it is a prime number.

  2. Inner loop (suppose the loop variable here is fact) go through form 2 to num - 1

  3. Check if num has a factor fact by num % fact == 0. If fact is a factor of num, then break the inner loop and continue to check the next number.

  4. If after checking all the numbers from 2 to num - 1 and none of them is the factor of num, then we are sure that num is a prime number and continue to check the next number.

Note that 0 and 1 are special case so we exclude them from the loop.

This method doesn't need any array. The time complexity is O(n2) and space complexity is O(1).

BTW, there are other better solution to this problem, for example Sieve of Eratosthenes.

Upvotes: 2

4386427
4386427

Reputation: 44274

There are a couple of problems but for a starter consider the very first loop.

i = 0
n.push_back(i);       // Now n[0] is now valid (having the value 0)
n[0] = n[0] + 1;      // Now n[0] is still valid (having the value 1)
j = 1;
if (n[j] % j == 0)    // Ups... access to n[1] which is invalid
                      // as you have only pushed one element, i.e. n[0]

Upvotes: 1

Related Questions