-2

I am new in programming and trying to solve some questions and in this question, I am trying to find prime numbers up to n digits using only 1 loop statement.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sana Mev
  • 111
  • 8
  • 1
    perhaps this might be of interest https://codereview.stackexchange.com/questions/169202/sieve-of-eratosthenes-prime-number-finder-up-to-n-in-c – jspcal Jun 02 '21 at 19:53
  • 2
    [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) – Drew Dormann Jun 02 '21 at 19:54
  • Remember that 2 is the only even prime number, the others are all odd. – Thomas Matthews Jun 02 '21 at 19:55
  • You have misunderstood your assignment. What you ask is impossible. You need to loop through the potential numbers, then you need to loop to find whether the number is prime. – Tim Roberts Jun 02 '21 at 20:02
  • 1
    @TimRoberts I don't think it is impossible actually I am new in programming so I am just trying to practicing to build my logic building that's why I am trying this question and it is not an assignment. It's just a logic-building question. – Sana Mev Jun 02 '21 at 20:32
  • 2
    It takes two loops. You can hide one loop as "recursion", or you can use Tapesh's trick of hiding one of the loops inside the other, but there are still two loops here. – Tim Roberts Jun 02 '21 at 21:11

2 Answers2

0

One loop, and recursion:

#include <iostream>
#include <cmath>

#define NUDIGS 3 //number of digits

bool TestPrime(int n, int ndiv)
{
    if ( n == ndiv )
        return true;
    if ( (n % ndiv) == 0 )
        return false;
    return TestPrime (n, ++ndiv) ;
}

int main()
{
    //calculate the max number with BUDIGITS
    int max = pow(10, NUDIGS) - 1;
    std::cout << "testing numbers from 2 to " << max << std::endl;
    int ntot = 0;
    
    //Loop testing every number. '1' is not considered as a prime number.
    for (int i=2; i <= max; i++)
    {
         //call a recursive test
         if ( TestPrime(i, 2) )
         {
             std::cout << i << " is prime " << std::endl;
             ntot++;
         }
    }
    std::cout << ntot << " prime numbers found" << std::endl;
}
Ripi2
  • 7,031
  • 1
  • 17
  • 33
-1
//this program will print prime number from 2 to N . and break loop 
without using break statment

#include <iostream>

using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int range;
    cin>>range;
    int num=2;
    for(int i=2;i<=num;i++){
        if(num>range){
            i=num+1;
        }
        else{
            if(i==num){
                cout<<num<<" ";
                i=1;
                num=num+1;
            }
            else if(num%i==0){
                i=1;
                num=num+1;
            }
        }
    }
    cout<<endl;
    return 0;
}
  • Thanks for your help. – Sana Mev Jun 02 '21 at 20:34
  • 1
    Please **do not** recommend the use of `#include ` in an answer on Stack Overflow. See: [Why should I not #include ?](https://stackoverflow.com/q/31816095/10871073). You should edit your post to include the required **standard** headers or you are likely to receive downvotes. – Adrian Mole Jun 02 '21 at 21:28
  • @AdrianMole ohh sorry for that ...let me change this.... – tapesh menaria Jun 02 '21 at 22:36
  • Why are you starting with 2 and incrementing by 1? This means that you'll wasting time checking event numbers. You can cut 50% of your time by starting at 3 and incrementing by 2. The number 2 is the only even prime. All others are odd. – Thomas Matthews Jun 03 '21 at 00:38
  • ok ok i understood ... you want to say that i should do num=3; and then num=num+2; and also there is no even number so i should do i=3; and increment by i=i+2; am i right? – tapesh menaria Jun 03 '21 at 08:44