RosettaCodeData/Task/Farey-sequence/Quackery/farey-sequence.quackery
2023-07-01 13:44:08 -04:00

45 lines
1,023 B
Text

[ $ "bigrat.qky" loadfile ] now!
[ rot + dip + reduce ] is mediant ( n/d n/d --> n/d )
[ 1+ temp put [] swap
dup size 1 - times
[ dup i^ peek
rot over nested join
unrot over i^ 1+ peek
join do mediant
dup temp share < iff
[ join nested
rot swap join
swap ]
else 2drop ]
drop
' [ [ 1 1 ] ] join
temp release ] is nextfarey ( fy n --> fy )
[ witheach
[ unpack vulgar$
echo$ sp ] ] is echofarey ( fy --> )
[ 0 swap dup times
[ i over gcd
1 = rot + swap ]
drop ] is totient ( n --> n )
[ 0 swap times
[ i 1+ totient + ] ] is totientsum ( n --> n )
[ totientsum 1+ ] is fareylength ( n --> n )
say "First eleven Farey series:" cr
' [ [ 0 1 ] [ 1 1 ] ]
10 times
[ dup echofarey cr
i^ 2 + nextfarey ]
echofarey cr
cr
say "Length of Farey series 100, 200 ... 1000: "
[] 10 times
[ i^ 1+ 100 *
fareylength join ]
echo