Michael Clease
24-01-2009, 00:58
' The sum of the squares of the first ten natural numbers is,
' 1^2 + 2^2 + ... + 10^2 = 385
' The square of the sum of the first ten natural numbers is,
' (1 + 2 + ... + 10)^2 = 55^2 = 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.
uses "Console"
DIM n AS LONG
DIM SumSquare AS DWORD
DIM SquareSum AS DWORD
DIM Difference AS STRING
'...
Dim T1, T2 As QUAD
'---Initialize hi resolution timer
HiResTimer_Init
T1 = hirestimer_get
FOR n = 1 TO 100
SumSquare += n^2
SquareSum += N
NEXT
SquareSum = SquareSum^2
Difference = STR$(SquareSum-SumSquare)
T2 = hirestimer_get
console_printat ("Answer: " + Difference, 1, 1)
console_printat ("Sum of the square: " + STR$(SumSquare), 1, 2)
console_printat ("Square of the Sum: " + STR$(SquareSum), 1, 3)
console_printat ("Elapsed time in microseconds: " & FORMAT$(T2-T1, "#0000"),1,5)
console_waitkey
' 1^2 + 2^2 + ... + 10^2 = 385
' The square of the sum of the first ten natural numbers is,
' (1 + 2 + ... + 10)^2 = 55^2 = 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.
uses "Console"
DIM n AS LONG
DIM SumSquare AS DWORD
DIM SquareSum AS DWORD
DIM Difference AS STRING
'...
Dim T1, T2 As QUAD
'---Initialize hi resolution timer
HiResTimer_Init
T1 = hirestimer_get
FOR n = 1 TO 100
SumSquare += n^2
SquareSum += N
NEXT
SquareSum = SquareSum^2
Difference = STR$(SquareSum-SumSquare)
T2 = hirestimer_get
console_printat ("Answer: " + Difference, 1, 1)
console_printat ("Sum of the square: " + STR$(SumSquare), 1, 2)
console_printat ("Square of the Sum: " + STR$(SquareSum), 1, 3)
console_printat ("Elapsed time in microseconds: " & FORMAT$(T2-T1, "#0000"),1,5)
console_waitkey