Please compute the sum of squares for the given numbers: a, a+1, ..., b-1, b.
Input
Two numbers: a and b separated by space, where 1 <= a <= b <=100.
Output
Computed sum: a*a + (a+1)*(a+1)+ ... +(b-1)*(b-1) +b*b
Example
Input: 1 4 Output: 30
Example 2
Input: 5 6 Output: 61
代码如下:
<?php
$debug = false;
$file = STDIN;
if ($debug)
{
$file = fopen('./spoj.txt', 'r');
}
while (($line = fgets($file)) !== false)
{
$line = trim($line);
if (empty($line)) break;
sscanf($line, "%d%d", $a, $b);
$sum = 0;
for ($i = $a; $i <= $b; $i++)
{
$sum += $i * $i;
}
echo $sum.PHP_EOL;
}
if ($debug) fclose($file);
?>