Two integers a and b are relatively prime if and only if there are no integers:
x > 1, y > 0, z > 0 such that a = xy and b = xz.
I wrote a program that determines how many positive integers less than n
are relatively prime to n
. But my program works too slowly because the number is sometimes too big.
My program should work for n <= 1000000000
.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
long long int n;
int cntr = 0, cntr2 = 0;
cin >> n;
if (!n) return 0;
vector <int> num;
for (int i = 2; i < n; i++)
{
if (n % i != 0)
{
if (num.size()>0)
{
for (int j = 0; j < num.size(); j++)
{
if (i % num[j] != 0)
cntr2++;
}
if (cntr2 == num.size())
cntr++;
cntr2 = 0;
}
else
cntr++;
}
else
num.push_back(i);
}
cout << cntr + 1 << endl;
}