/* http://projecteuler.net/
 *
 * Problem 57
 * In the first one-thousand finite expansions of the continued fraction
 * for the square root of two, how many fractions contain a numerator with
 * more digits than denominator?
 *
 * Solution by Melkor (Filip Niksic, fniksic@gmail.com)
 *
 **/

#include <iostream>

#include "related/bignum.h"

using namespace std;

int main() {
    BigNum pp(1), p(1), qq(0), q(1);
    int total = 0;
    for (int i = 1; i <= 1000; ++i) {
	BigNum tmp_p(p), tmp_q(q);
	p.mul(2);
	p += pp;
	pp = tmp_p;
	q.mul(2);
	q += qq;
	qq = tmp_q;
	if (p.digits10() > q.digits10())
	    ++total;
	/*cout << "p(" << i << ") = " << p << endl
	    << "q(" << i << ") = " << q << endl
	    << "len_p = " << p.digits10() << endl
	    << "len_q = " << q.digits10() << endl
	    << endl;*/
    }
    cout << total << endl;
    return 0;
}

