Skip to content

Instantly share code, notes, and snippets.

@phamtm
Last active September 17, 2015 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phamtm/a1389a6fee5d50b74945 to your computer and use it in GitHub Desktop.
Save phamtm/a1389a6fee5d50b74945 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
using namespace std;
/* print the fractions of denominator <= n between n1/d1 and n2/d2 */
void genfrac(int n, int n1, int d1, int n2, int d2) {
if (d1 + d2 > n) return;
genfrac(n, n1, d1, n1 + n2, d1 + d2);
cout << n1 + n2 << "/" << d1 + d2 << "\n";
genfrac(n, n1 + n2, d1 + d2, n2, d2);
}
int main() {
int n = 5;
cout << "0/1\n";
genfrac(n, 0, 1, 1, 1);
cout << "1/1\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment