fork download
  1. #!/usr/bin/perl
  2. use 5.016;
  3. use warnings;
  4.  
  5. # Farey sequence
  6. # see: http://w...content-available-to-author-only...s.jp/ikuro_kotaro/koramu/1529_f2.htm
  7. # see: http://m...content-available-to-author-only...m.com/FareySequence.html
  8. # thanks: http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1390525149/194
  9.  
  10. sub f {
  11. my ($n) = @_;
  12.  
  13. return sub {
  14. my ($p, $q, $r, $s) = @_;
  15.  
  16. my $pd = $p + $r;
  17. my $qd = $q + $s;
  18. ($qd <= $n) or return ();
  19.  
  20. __SUB__->($p, $q, $pd, $qd),
  21. "$pd/$qd",
  22. __SUB__->($pd, $qd, $r, $s)
  23. );
  24. }->(0, 1, 1, 1);
  25. }
  26.  
  27. say join(' ', f(3));
  28. say join(' ', f(5));
  29.  
Success #stdin #stdout 0s 3736KB
stdin
Standard input is empty
stdout
1/3 1/2 2/3
1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5