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

Dec 27, 2016 at 12:06pm
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;
}
Dec 27, 2016 at 12:22pm
pairs which are primes between them

Please, explain what that means.
Dec 27, 2016 at 1:24pm
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 Dec 27, 2016 at 1:24pm
Dec 27, 2016 at 1:51pm
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.
Dec 27, 2016 at 11:50pm
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.
Dec 27, 2016 at 11:57pm
Do you mean: "where a is a prime AND b is a prime"?
Dec 27, 2016 at 11:59pm
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.
Dec 28, 2016 at 1:44am
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
Dec 28, 2016 at 5:14pm
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.