Skip to content

Instantly share code, notes, and snippets.

@jeremykendall
Created October 1, 2012 13:22
Show Gist options
  • Save jeremykendall/3811768 to your computer and use it in GitHub Desktop.
Save jeremykendall/3811768 to your computer and use it in GitHub Desktop.
<?php
function multiple_cycle($limit, $numbers)
{
$count = 0;
$index = 0;
$numbersLength = count($numbers);
for ($i = 1; $i <= $limit; $i++) {
if ($i % $numbers[$index] == 0) {
++$count;
++$index;
if ($index >= $numbersLength) {
$index = 0;
}
}
}
return $count;
}
var_dump(multiple_cycle(1000000000, array(5395, 7168, 2367, 9999, 3)));
@al-the-x
Copy link

al-the-x commented Oct 1, 2012

Good first try, and you should get the right answer for smaller values of $limit. The question asks for the number of cycles required to reach $limit using multiples of the values in $numbers, right? Instead of counting up from 1 to $limit by 1, try incrementing by the values in $numbers instead.

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