project-euler/064_odd_period_square_roots.c

40 lines
541 B
C
Raw Normal View History

2023-03-27 16:30:58 +02:00
#include <stdio.h>
#include <math.h>
int sq_frac(int x);
int N = 10000;
int main(int argc, char *argv[])
{
int i, c;
c = 0;
for(i = 1; i <= N; ++i){
if(sq_frac(i) % 2 == 0){
++c;
}
}
printf("%d\n", c);
return 0;
}
int sq_frac(int x)
{
int m, n, d, a, c;
if((m = sqrt(x)) * m == x){
return 1;
}
d = c = 1;
n = m;
do{
d = (x - n * n) / d;
a = (n + m) / d;
n = a * d - n;
++c;
}while(d != 1);
return c;
}