I need a function (call it 'fix' for lack of a better name) to find the value $N that is an even multiple of $m that is larger than or equal to $n. (See note at bottom for clarification of "even multiple") The best I came up with requires control flow logic and I was wondering if anyone could suggest something better, preferably that doesn't require an 'if'.
(Sample output below:)sub fix{ my ($n,$m)=@_; my $M = $n % $m; $M ? $n + $m - $M : $n } print "<table width='80%' align='center' border='1'>\n"; print "<tr><th></th><th colspan='4'>\$m</th></tr>\n"; for my $n (0..20) { print " <tr>".join("", map { "<th>$_</th>" } '$n',2..5),"</tr>\n" unless $n; print " <tr>".join("", map { "<td>$_</td>" } $n,map fix($n,$_),2. +.5),"</tr>\n"; } print "</table>"; __END__
| $m | ||||
|---|---|---|---|---|
| $n | 2 | 3 | 4 | 5 |
| 0 | 0 | 0 | 0 | 0 |
| 1 | 2 | 3 | 4 | 5 |
| 2 | 2 | 3 | 4 | 5 |
| 3 | 4 | 3 | 4 | 5 |
| 4 | 4 | 6 | 4 | 5 |
| 5 | 6 | 6 | 8 | 5 |
| 6 | 6 | 6 | 8 | 10 |
| 7 | 8 | 9 | 8 | 10 |
| 8 | 8 | 9 | 8 | 10 |
| 9 | 10 | 9 | 12 | 10 |
| 10 | 10 | 12 | 12 | 10 |
| 11 | 12 | 12 | 12 | 15 |
| 12 | 12 | 12 | 12 | 15 |
| 13 | 14 | 15 | 16 | 15 |
| 14 | 14 | 15 | 16 | 15 |
| 15 | 16 | 15 | 16 | 15 |
| 16 | 16 | 18 | 16 | 20 |
| 17 | 18 | 18 | 20 | 20 |
| 18 | 18 | 18 | 20 | 20 |
| 19 | 20 | 21 | 20 | 20 |
| 20 | 20 | 21 | 20 | 20 |
Note that $m and $n may be any non negative integer,( and thus be odd, or even prime), so you can't use binary operators like '&'.
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
• Update:
To clarify the use of 'evenly' in the above I mean the definition X divides Y evenly if and only if Y % X == 0. Or expressed another way, Y is an even multiple of X if and only if Y % X == 0. Sorry, I'm not much of a mathematician so I forget the textbook definitions and terms for these things.
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |