Farey sequence: Difference between revisions

m
→‎{{header|Tcl}}: Better efficiency
(→‎Tcl: Added implementation)
m (→‎{{header|Tcl}}: Better efficiency)
Line 547:
 
proc farey {n} {
set nums [lrepeat [expr {$n+1}] 1]
set result {{0 1}}
for {set ifound 01} {$i < $nfound} {incr i} {
while true {
for {set foundnj [setlindex i$nums [set j 01]]} {$i < $n} {incr i} {
for {set found 0;set i 1} {$i <= $n} {incr i} {
if {[lindex $nums $i]*($j+1) < [lindex $nums $j]nj*($i+1)} {
set j $i
lsetset nums $inj [expr {[lindex $nums [set j $i] + 1}]
set found 1
}
}
setlappend thisnumresult [lindexlist $numsnj $j]
for {set i $j} {$i <= $n} {incr i $j} {
lappend result [list $thisnum [expr {$j+1}]]
lset nums $i [expr {[lindex $nums $i] + 1}]
for {set i 0} {$i < $n} {incr i} {
if {[lindex $nums $i]*($j+1) == $thisnum*($i+1)} {
lset nums $i [expr {[lindex $nums $i] + 1}]
}
}
if {!$found} break
}
return $result
Anonymous user