summaryrefslogtreecommitdiff
path: root/064.c
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2014-08-31 20:21:45 +0200
committerReiner Herrmann <reiner@reiner-h.de>2014-08-31 20:35:09 +0200
commit95341b61b030c9e1290f3b326cb7ec584f543aea (patch)
tree852386fa04d32eb859bca11c0eff7b5ef9e50f00 /064.c
parent571164d977f91925c4c76a292f74f5f93d09ae23 (diff)
moved files to higher directory after split to new repositoryHEADtrunk
Diffstat (limited to '064.c')
-rw-r--r--064.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/064.c b/064.c
new file mode 100644
index 0000000..0e60211
--- /dev/null
+++ b/064.c
@@ -0,0 +1,84 @@
+#include <math.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+
+const int size = 100;
+double *fraction;
+int pos = 0;
+
+void continued_fraction(int n)
+{
+ double sqrtn, x, y, an, a0;
+
+ memset(fraction, 0, size*sizeof(double));
+ pos = 0;
+
+ sqrtn = sqrt((double)n);
+ a0 = floor(sqrtn);
+
+ if((int)sqrtn*sqrtn == n) // works only for rational square roots
+ {
+ fraction[pos] = sqrtn;
+ return;
+ }
+
+ //
+ // floor + ( sqrt(23) - x ) / y
+ // nenner = (23 - x*x)
+ // tmp = y * (sqrt(23) + x) / nenner)
+ // an = floor(tmp)
+ // x = an*nenner/y - x
+ //
+ x = a0;
+ y = 1.0;
+
+ while(1)
+ {
+ double denominator = (n - x*x);
+ double tmp = y * (sqrtn + x) / denominator;
+ an = floor(tmp);
+ x = (an * denominator / y) - x;
+ y = denominator / y;
+
+ fraction[pos++] = an;
+
+ if(an == 2*a0)
+ break;
+ }
+
+ /*
+ // not working because of double imprecision
+ while(1)
+ {
+ an = floorl(a);
+ r = 1/(a - an);
+ a = r;
+
+ fraction[++pos] = an;
+
+ if(an == 2*a0)
+ break;
+ }
+ */
+}
+
+int main(void)
+{
+ int i, count=0;
+
+ fraction = (double*) malloc(size*sizeof(double));
+
+ for(i=2; i<=10000; i++)
+ {
+ continued_fraction(i);
+ if(pos > 0 && pos&1 == 1)
+ count++;
+ }
+
+ printf("%i\n", count);
+
+ return 0;
+}
+