See Original text in context
method polymod(Int: +)
Returns a sequence of mod results corresponding to the divisors in @mods
in the same order as they appear there. For the best effect, the divisors should be given from the smallest "unit" to the largest (e.g. 60 seconds per minute, 60 minutes per hour) and the results are returned in the same way: from smallest to the largest (5 seconds, 4 minutes). The last non-zero value will be the last remainder.
say 120.polymod(10); # OUTPUT: «(0 12)»say 120.polymod(10,10); # OUTPUT: «(0 2 1)»
In the first case, 120 is divided by 10 giving as a remainder 12, which is the last element. In the second, 120 is div
ided by 10, giving 12, whose remainder once divided by 10 is 2; the result of the integer division of 12 div
10 is the last remainder. The number of remainders will be always one more item than the number of given divisors. If the divisors are given as a lazy list, runs until the remainder is 0 or the list of divisors is exhausted. All divisors must be Int
s, unless the method is called on a non-Int
number.
my = 1 * 60*60*24 # days+ 3 * 60*60 # hours+ 4 * 60 # minutes+ 5; # secondssay .polymod(60, 60); # OUTPUT: «(5 4 27)»say .polymod(60, 60, 24); # OUTPUT: «(5 4 3 1)»say 120.polymod: 1, 10, 10², 10³, 10⁴; # OUTPUT: «(0 0 12 0 0 0)»say 120.polymod: lazy 1, 10, 10², 10³, 10⁴; # OUTPUT: «(0 0 12)»say 120.polymod: 1, 10, 10² … ∞; # OUTPUT: «(0 0 12)»say ⅔.polymod(⅓); # OUTPUT: «(0 2)»say 5.Rat.polymod(.3, .2); # OUTPUT: «(0.2 0 80)»my = 9123607.polymod(37 xx *); # Base conversionsay .reverse # OUTPUT: «[4 32 4 15 36]»
To illustrate how the Int
, non-lazy version of polymod works, consider this code that implements it:
my = 2 * 60*60*24 # days+ 3 * 60*60 # hours+ 4 * 60 # minutes+ 5; # secondsmy ;for 60, 60, 24 ->.push: ;say ; # OUTPUT: «[5 4 3 2]»
For a more detailed discussion, see this blog post.
We can use lazy lists in polymod
, as long as they are finite:
my = lazy gather ;say 600.polymod( ); # OUTPUT: «(0 2 6 3)»
See Original text in context
method polymod(Real: +)
Returns the remainders after applying sequentially all divisors in the @mods
argument; the last element of the array will be the last remainder.
say (1e8+1).polymod(10 xx 8); # OUTPUT: «(1 0 0 0 0 0 0 0 1)»
10 xx 8
is simply an array with eight number 10s; the first division by 10 will return 1
as a remainder, while the rest, up to the last, will return 0. With 8 divisors, as above, the result will have one more elements, in this case for the last remainder.