in reply to Simple arithmetic?

I would first determine the least common multiple (LCM) of R and P, which is almost entry level algorithmic. Then divide S by the LCM, and multiply the LCM by the integer part of the result of that division.

Je suis Charlie.

Replies are listed 'Best First'.
Re^2: Simple arithmetic?
by hdb (Monsignor) on Mar 07, 2015 at 23:51 UTC
    Too late, forget about it...

    Exactly, and one can also utilize the fact that 4096 is a power of 2:

    $lcm = $r * 4096; $lcm /= 2 until $lcm % 8192; $c = int( $s/$lcm) * $lcm;

    UPDATE: 7 hours of sleep later, here is what I wanted to say, but Anonymous Monk has posted it already in node 1119206.

    use strict; use warnings; my $s = 3221225472; my $r = 5007; my $lcm = $r; my $m = 0; $lcm/=2 while 12>$m++ and !($lcm%2); $lcm *= 4096; my $c = int( $s/$lcm ) * $lcm; print "$c\n";
      and one can also utilize the fact that 4096 is a power of 2

      That's a really useful insight. It makes deriving the lcm substantially more efficient; especially for large values of S combined with R=prime. Thank you.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
Re^2: Simple arithmetic?
by BrowserUk (Patriarch) on Mar 08, 2015 at 12:18 UTC
    I would first determine the least common multiple (LCM) of R and P, which is almost entry level algorithmic. Then divide S by the LCM, and multiply the LCM by the integer part of the result of that division.

    That's effectively, exactly what I was doing, but in my sleep-deprived state, I was convinced that I could avoid the iteration and calculate the result directly, despite that I couldn't see how. Hence my question. Thank you.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked