// 0006.go contains some functions to solve ProjectEuler.net's Problem #6
// https://projecteuler.net/problem=6
package main
import "fmt"
// SumOfSquares calculates the sum of the squares of all natural numbers
// up to the given maxNumber.
func SumOfSquares(maxNum int) int {
// Initialise the sum of squares variable
sum := 0
// Add squares of natural numbers
for i := 1; i <= maxNum; i++ {
sum += i*i
}
// Return answer
return sum
}
// SquareOfSum squares the sum of all natural numbers up to the given
// maxNumber.
func SquareOfSum(maxNum int) int {
// Initialise the sum of all numbers variable
sum := 0
// Add natural numbers of variable
for i := 1; i <= maxNum; i++ {
sum += i
}
// Square sum and return answer
return sum*sum
}
func main(){
// Answer the problem
fmt.Println(SquareOfSum(100)-SumOfSquares(100))
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎