Skip to content

Instantly share code, notes, and snippets.

@chadochan
Created January 29, 2019 11:03
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 chadochan/89623fe8c7b232f1a26c75ff92a6e57b to your computer and use it in GitHub Desktop.
Save chadochan/89623fe8c7b232f1a26c75ff92a6e57b to your computer and use it in GitHub Desktop.
Farey creates an ordered List containing decimal representations of a full Farey sequence up to F(n).
/*
Farey creates an ordered List containing decimal representations of a full Farey sequence up to F(n).
You can use my Rationalize class for conversion to rational list.
(translated from the Python example on Wikipedia)
*/
Farey {
*new { |n = 5|
var farey = List();
var a, b, c, d, k;
#a, b, c, d = [0, 1, 1, n];
while ({ c <= n },
{
k = floor((n + b) / d);
#a, b, c, d = [c, d, (k * c - a), (k * d - b)];
/*convert to decimals and put in the list.
at least six-decimal precision is required for .asFraction to convert back to rationals*/
farey.add((a/b).round(0.000001));
postf("%/%",a,b); // post the rationals
post(" ");
};
);
postln("************");
// return the decimal list
^farey
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment