snippets / euler / 025.c

// project euler - problem 25
// my first attempt, *very* lazy digit counting


#include <gmp.h>
#include <stdio.h>

int numdigits(mpz_t n) {
	return 2;
}

int main() {
	mpz_t a, b, c;
	size_t len;
	int i;

	mpz_init(a);
	mpz_init(b);
	mpz_init(c);

	mpz_set_ui(a, 1);
	mpz_set_ui(b, 1);

	for (i=3 ; i; i++) {
		// rotate through a, b, and c to save time
		if(i % 3 == 0) {
			mpz_add(c, a, b);
			len = mpz_out_str(stdout, 10, c);
			putch('\n');
			if(len >= 1000)
				break;
		} else if (i % 3 == 1) {
			mpz_add(a, b, c);
			len = mpz_out_str(stdout, 10, a);
			putch('\n');
			if(len >= 1000)
				break;
		} else if (i % 3 == 2) {
			mpz_add(b, c, a);
			len = mpz_out_str(stdout, 10, b);
			putch('\n');
			if(len >= 1000)
				break;
		}
	}
	printf("i:%i\n", i);
	return 0;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.