project-euler

https://projecteuler.net/
Log | Files | Refs | README

Euler_73.cpp (1054B)


      1 #include "Euler.h"
      2 
      3 //def farey( n, asc=True ):
      4 //    """Python function to print the nth Farey sequence, either ascending or descending."""
      5 //    if asc: 
      6 //        a, b, c, d = 0, 1,  1 , n     # (*)
      7 //    else:
      8 //        a, b, c, d = 1, 1, n-1, n     # (*)
      9 //    print "%d/%d" % (a,b)
     10 //    while (asc and c <= n) or (not asc and a > 0):
     11 //        k = int((n + b)/d)
     12 //        a, b, c, d = c, d, k*c - a, k*d - b
     13 //        print "%d/%d" % (a,b)
     14 
     15 
     16 
     17 llui Euler::CountingRangedFractions()
     18 {
     19     llui count = 0;
     20     bool counting = false;
     21 
     22     int n = 12000;
     23     int a = 0;
     24     int b = 1;
     25     int c = 1;
     26     int d = n;
     27     int x = 0;
     28 
     29     while (c <= n && !(a == 1 && b == 2))
     30     {
     31         int k = 0;
     32 
     33         if (d != 0)
     34             k = int((n + b)/d);
     35         else
     36             ++x;
     37 
     38         int temp_a = a;
     39         int temp_b = b;
     40 
     41         a = c;
     42         b = d;
     43         c = k * c - temp_a;
     44         d = k * d - temp_b;
     45 
     46         if (counting)
     47             ++count;
     48 
     49         if (a == 1 && b == 3)
     50             counting = true;
     51     }
     52 
     53     return count - 1;
     54 }