Programmering A - Förklara tankesätt
Hej!
Jag har inga större svårigheter att programmera och lösa matematiska problem. Problemet ligger snarare i att jag har svårt att förklara hur jag har gjort.
Jag har en uppgift i Programmering A som innebär att jag ska lösa ett Project Euler-problem, och sedan redovisa detta inför klassen. Jag körde Problem 6, och frågan lyder som följande:
The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Detta är vad jag har kommit fram till:
$sum = 0;
$squareSum = 0;
for($x = 1; $x<=100; $x++) {
$sum += $x;
$squareSum += ($x*$x);
}
$diff = $squareSum-($sum*$sum);
echo abs($diff);
----
Enligt ProjectEuler så har jag rätt på frågan, men nu till problemet: Hur skall jag förklara hur jag har gjort på bästa sätt?