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'.

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__
(Sample output below:)
 $m
$n2345
00000
12345
22345
34345
44645
56685
666810
789810
889810
91091210
1010121210
1112121215
1212121215
1314151615
1414151615
1516151615
1616181620
1718182020
1818182020
1920212020
2020212020

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 '&'.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi

    Flux8


• 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.



In reply to Best way to make sure a number is an even multiple of another? by demerphq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.