Pagini recente » Cod sursa (job #2956613) | Cod sursa (job #1954291) | Cod sursa (job #2864842) | Cod sursa (job #2719562) | Cod sursa (job #1641626)
#include <fstream>
#include <iostream>
using namespace std;
int gcd( int a, int b ) {
int t;
if ( a < 0 )
a = -a;
if ( ! b )
return a;
if ( b < 0 )
b = -b;
if ( ! a )
return b;
t = 0;
while ( ! ( ( a | b ) & 1 ) ) {
a >>= 1;
b >>= 1;
++t;
}
while ( ! ( a & 1 ) )
a >>= 1;
while ( ! ( b & 1 ) )
b >>= 1;
while ( a != b ) {
if ( a > b ) {
a -= b;
do {
a >>= 1;
} while ( ! ( a & 1 ) );
}
else {
b -= a;
do {
b >>= 1;
} while ( ! ( b & 1 ) );
}
}
return a << t;
}
int gcd2(int u, int v)
{
// simple cases (termination)
if (u == v)
return u;
if (u == 0)
return v;
if (v == 0)
return u;
// look for factors of 2
if (~u & 1) // u is even
{
if (v & 1) // v is odd
return gcd(u >> 1, v);
else // both u and v are even
return gcd(u >> 1, v >> 1) << 1;
}
if (~v & 1) // u is odd, v is even
return gcd(u, v >> 1);
// reduce larger argument
if (u > v)
return gcd((u - v) >> 1, v);
return gcd((v - u) >> 1, u);
}
int main(){
long int n,s=0;
ifstream fin ("fractii.in");
ofstream fout ("fractii.out");
fin>>n;
for (long int i=1;i<=n;i++){
for(long int j=1;j<=n;j++){
int r = gcd2(i,j);
if (r==1)
s++;
// cout<<"("<<i<<","<<j<<") "<<"r="<<r<<"\n";
}
}
fout<<s;
}