learing prime numbers with loop/recursion

Feb 8, 2017 at 9:10pm
Hi programmers!
I want to write in c++ please,help me to solve this problem

Problem: Raja Beta is bad at maths, his teacher always complaints about him.Help him in his prime numbers homework.
Input: Two space separated positive integer m and n
Output: Print all prime numbers p such that m <= p <= n, one number per line
Constraints: 1 <= m <= n <= 1000000000
Sample Input: 1 10
Sample Output:
2
3
5
7
Feb 8, 2017 at 9:25pm
you sure don't want to do it with recursion.

What have you done so far? Google prime number sieve algorithm. You might also check out a related item, the GCD algorithm.

Feb 8, 2017 at 10:26pm
Here's some code to get you started. It's inefficient, but it seems that you haven't coded something like this before, so it's best to start basic and slowly optimise it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool is_prime( int n )
{
    // code for algorithm here
}

int main()
{
    int m = 0, n = 0;
    // get user input
    for( int i = m; i <= n; i++ ) {
        if( is_prime( i ) )
            std::cout << i << '\n';
    }
}

Feb 9, 2017 at 2:12pm
I want to other code
Feb 9, 2017 at 3:57pm
try. this forum is not for people to do your homework for you.

the best algorithm I know of is GCD((int)(sqrt(n)+1 !), n);
but the factorials get too large without installing a large integer handling class.

that leaves what I told you already...

Feb 10, 2017 at 9:20am
ho to write program print all prime numbers from 1 to 10 with loop/ recursion
I'm waiting to replay me
Feb 10, 2017 at 2:58pm
...why don't you post your code... before asking for help
Topic archived. No new replies allowed.