patternphpModerate
Displaying all the numbers between 200 and 600
Viewed 0 times
theallnumbers200displayingbetweenand600
Problem
I have to display all the numbers between 200 and 600 (both numbers inclusive) that are divisible by 8:
It reaches maximum execution time.
for($i=200;$i<=600;$i++)
{
if($i%8==0)
echo $i.',';
}It reaches maximum execution time.
Solution
-
Why do you test each number?
Rather than
-
Your output is terminated by
I would do:
Why do you test each number?
Rather than
$i++ you can use $i += 8. Then the test becomes redundant. -
Your output is terminated by
,...584,592,500,
^^^^ Extra trailing comma.I would do:
printf('%d', 200);
for($i = 208; $i <= 600; $i += 8)
{
printf(', %d', $i);
}
printf('\n');Code Snippets
...584,592,500,
^^^^ Extra trailing comma.printf('%d', 200);
for($i = 208; $i <= 600; $i += 8)
{
printf(', %d', $i);
}
printf('\n');Context
StackExchange Code Review Q#13217, answer score: 12
Revisions (0)
No revisions yet.