Advertisement
xah

0006 - Project Euler

xah
Oct 19th, 2021 (edited)
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.87 KB | None | 0 0
  1. // 0006.go contains some functions to solve ProjectEuler.net's Problem #6
  2. // https://projecteuler.net/problem=6
  3. package main
  4.  
  5. import "fmt"
  6.  
  7. // SumOfSquares calculates the sum of the squares of all natural numbers
  8. // up to the given maxNumber.
  9. func SumOfSquares(maxNum int) int {
  10.     // Initialise the sum of squares variable
  11.     sum := 0
  12.  
  13.     // Add squares of natural numbers
  14.     for i := 1; i <= maxNum; i++ {
  15.         sum += i*i
  16.     }
  17.  
  18.     // Return answer
  19.     return sum
  20. }
  21.  
  22. // SquareOfSum squares the sum of all natural numbers up to the given
  23. // maxNumber.
  24. func SquareOfSum(maxNum int) int {
  25.     // Initialise the sum of all numbers variable
  26.     sum := 0
  27.  
  28.     // Add natural numbers of variable
  29.     for i := 1; i <= maxNum; i++ {
  30.         sum += i
  31.     }
  32.  
  33.     // Square sum and return answer
  34.     return sum*sum
  35. }
  36.  
  37. func main(){
  38.     // Answer the problem
  39.     fmt.Println(SquareOfSum(100)-SumOfSquares(100))
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement