Farey sequence: Difference between revisions
Content added Content deleted
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
|||
Line 1,759: | Line 1,759: | ||
F900: 246327 members |
F900: 246327 members |
||
F1000: 304193 members</pre> |
F1000: 304193 members</pre> |
||
=={{header|jq}}== |
|||
{{works with|jq}} |
|||
'''Also works with gojq, the Go implementation of jq, and with fq.''' |
|||
This solution uses two jq functions from the [[Arithmetic/Rational#jq]] page, |
|||
and can be included using jq's `include` directive. |
|||
<syntaxhighlight lang=jq> |
|||
include "rational" ; # actually, only `r/2` and `gcd/2` are actually needed |
|||
# Emit an ordered stream of the Farey sequence of order $order |
|||
# by recursively generating the mediants |
|||
def FS($order): |
|||
def f($l; $r; $n): |
|||
r($l.n + $r.n; $l.d + $r.d) as $m |
|||
| select($m.d <= $n) |
|||
| f($l; $m; $n), $m, f($m; $r; $n); |
|||
r(0;1) as $l |
|||
| r(1;1) as $r |
|||
| $l, f($l; $r; .), $r; |
|||
# Pretty-print Farey sequences of order $min up to and including order $max |
|||
def FareySequences($min; $max): |
|||
def rpp: "\(.n)/\(.d)"; |
|||
def pp(s): [s|rpp] | join(" "); |
|||
range($min;$max+1) |
|||
| "F(\(.)): " + pp(FS(.)); |
|||
# Use `count/1` for counting to save space |
|||
def count(s): reduce s as $_ (0; .+1); |
|||
def FareySequenceMembers($N): |
|||
count(FS($N)); |
|||
# The tasks: |
|||
FareySequences(1;11), |
|||
"", |
|||
(range(100; 1001; 100) | "F\(.): \(FareySequenceMembers(.)|length) members" ) |
|||
</syntaxhighlight> |
|||
''Invocation'': jq -r |
|||
<pre> |
|||
F(1): 0/1 1/1 |
|||
F(2): 0/1 1/2 1/1 |
|||
F(3): 0/1 1/3 1/2 2/3 1/1 |
|||
F(4): 0/1 1/4 1/3 1/2 2/3 3/4 1/1 |
|||
F(5): 0/1 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1/1 |
|||
F(6): 0/1 1/6 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 5/6 1/1 |
|||
F(7): 0/1 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 1/1 |
|||
F(8): 0/1 1/8 1/7 1/6 1/5 1/4 2/7 1/3 3/8 2/5 3/7 1/2 4/7 3/5 5/8 2/3 5/7 3/4 4/5 5/6 6/7 7/8 1/1 |
|||
F(9): 0/1 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 1/1 |
|||
F(10): 0/1 1/10 1/9 1/8 1/7 1/6 1/5 2/9 1/4 2/7 3/10 1/3 3/8 2/5 3/7 4/9 1/2 5/9 4/7 3/5 5/8 2/3 7/10 5/7 3/4 7/9 4/5 5/6 6/7 7/8 8/9 9/10 1/1 |
|||
F(11): 0/1 1/11 1/10 1/9 1/8 1/7 1/6 2/11 1/5 2/9 1/4 3/11 2/7 3/10 1/3 4/11 3/8 2/5 3/7 4/9 5/11 1/2 6/11 5/9 4/7 3/5 5/8 7/11 2/3 7/10 5/7 8/11 3/4 7/9 4/5 9/11 5/6 6/7 7/8 8/9 9/10 10/11 1/1 |
|||
F100: 3045 members |
|||
F200: 12233 members |
|||
F300: 27399 members |
|||
F400: 48679 members |
|||
F500: 76117 members |
|||
F600: 109501 members |
|||
F700: 149019 members |
|||
F800: 194751 members |
|||
F900: 246327 members |
|||
F1000: 304193 members |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |