C++ problem - did the code, but it doesn't work properly.

So - a natural number n. I need to do a program for determinating the number of (a,b) pairs (a,b <=n) which are primes between them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <iostream>

using namespace std;

int main()
{
    int a=0, b=0, n, i, nr;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        while(a!=b)
            if(a<=n && b<=n) {
            if(a>b) {a=a-b;}
            else {b=b-a;}
            if(a==1 || b==1)
            {
                nr++;
                a++;
                b++;
            }
    }
    else cout<<"0";
    }
    cout<<nr;
    return 0;
}
pairs which are primes between them

Please, explain what that means.
This is how it sounds in my language. erm.. their only common divisor is 1. I don't know how it is in english.
Last edited on
Perhaps, https://en.wikipedia.org/wiki/Greatest_common_divisor


You want to count "valid" pairs.

For that, you should list all possible pairs. Or just all possible unique pairs?

For each pair, do compute GCD(a,b).

If the result is 1 for that pair, then count that pair.
I read what I wrote again and I'm sorry "
pairs which are primes between them". - I meant pairs OF NUMBERS which are primes between them - that means their only common divisor is 1.
I need to do a program for determinating the number of (a,b) pairs OF NUMBERS (a,b <=n) which are primes between them.
Do you mean: "where a is a prime AND b is a prime"?
Using google translate - "prime among them".
Omg.. I mean, their only common divisor is 1. I don't know how the numbers with this property are called in english. An example of a pair of numbers which would be okay is 3 and 8. Only common divisor - 1.
In number theory, two integers a and b are said to be relatively prime, mutually prime, or coprime (also spelled co-prime) if the only positive integer that divides both of them is 1.
https://en.wikipedia.org/wiki/Coprime_integers
i thinl the problem is the fact that the while has no use since a is the same as b
Topic archived. No new replies allowed.