For the fun of it, I wanted to give the thing a closed, whole-integer form. The problem was, that a "close fitting" was wanted, and all I had was the following expression:
my $M; my $n; my $remaining = $n % $M; my $next_multiple = $n + $M - $remaining;
This means, that my calculation always was one multiple too large whenever $remaining was zero. So I needed to fudge that a bit, by the idea that, if I made $n one smaller in one part, and added that one later on, I could maybe shift the error into the right direction:
for my $n (1..20) { print "$n:\t"; for my $M (2..5) { my $remaining = ($n-1) % $M; # subtract one from the remainder my $next_multiple = $n-1 + $M - $remaining; print "$next_multiple\t"; }; print "\n"; };
or, as my unreadable shell test expression:
perl -e "my $n=20; for(@ARGV){$x=$n-1+($_-($n-1)%$_);print qq($n\[$_] +:$x\n)}" 2 3 4 5 6 7 8 9 10 # resp. $x=$n-1+($M-($n-1)%$M)
In reply to Re: Best way to make sure a number is an even multiple of another?
by Corion
in thread Best way to make sure a number is an even multiple of another?
by demerphq
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |