#!/usr/bin/env tclsh # From rosettacode proc ::tcl::mathfunc::mulinv {a b} { if {$b == 1} {return 1} set b0 $b; set x0 0; set x1 1 while {$a > 1} { set x0 [expr {$x1 - ($a/$b) * [set x1 $x0]}] set b [expr {$a % [set a $b]}] } incr x1 [expr {($x1 < 0) * $b0}] } proc chineseRemainder {nList aList} { set sum 0; set prod [::tcl::mathop::* {*}$nList] foreach n $nList a $aList { set p [expr {$prod / $n}] incr sum [expr {$a * mulinv($p,$n) * $p}] } expr {$sum % $prod} } #puts [chineseRemainder {3 5 7} {2 3 2}] #puts [chineseRemainder {17 13 19} {0 11 16}] #puts [chineseRemainder {67 7 59 61} {0 6 57 58}] #puts [chineseRemainder {67 7 59 61} {0 5 56 57}] set time -1 foreach id [split [read stdin] ,] { incr time if {$id eq "x"} continue lappend nlist $id lappend alist [expr {($id - $time) % $id}] } puts [chineseRemainder $nlist $alist]