I have been managing to read the book Recreations in Theory of Numbers. A good informal book on Number Theory. Farey series is one of topics I wanted to write a program for. Following is the Groovy code of generating Farey series until the given value of the denominator.
def farey = { def a = 1, b = it, c = 1, d = it - 1; printf "%d/%d ", a, b while (c != 1 || d != 1) { printf "%d/%d ", c, d z = (int) ((it + b) / d) (a, b, c, d) = [c, d, z * c - a, z * d - b] } } farey(7) Output: 1/7 1/6 1/5 1/4 2/7 1/3 2/5 3/7 1/2 4/7 3/5 2/3 5/7 3/4 4/5 5/6 6/7
No comments:
Post a Comment