Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Forked from jeremykendall/big-loop.php
Created October 1, 2012 13:38
Show Gist options
  • Save al-the-x/3811849 to your computer and use it in GitHub Desktop.
Save al-the-x/3811849 to your computer and use it in GitHub Desktop.
<?php
function next_highest_multiple($number, $multiple)
{
if ( $multiple > $number ) return $multiple;
if ( $multiple == 0 ) return 0;
return ( ($remainder = $number % $multiple) ?
$number + $multiple - $remainder : $number
);
}
function multiple_cycle($limit, array $numbers)
{
$current = 0; $cycles = 0;
while ( $current < $limit )
{
$cycles++;
$current = next_highest_multiple($current, current($numbers));
next($numbers) or reset($numbers);
}
return $cycles;
}
assert('multiple_cycle(15, array(5, 7, 3)) == 6') or var_dump(
multiple_cycle(15, array(5, 7, 3))
);
var_dump(multiple_cycle(1000000000, array(5395, 7168, 2367, 9999, 3)));
@al-the-x
Copy link
Author

al-the-x commented Oct 1, 2012

With the next_highest_multiple() calculation, I get much more respectable benchmarks and a different answer!

int(408626)

real    0m7.536s
user    0m7.488s
sys     0m0.033s

Which one is right? Damned if I know...! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment