xah icon

0006 - Project Euler

xah | PRO | 11/23/21 06:50:45 AM UTC (Edited) | 0 ⭐ | 14605 👁️ | Never ⏰ | []
Go |

870 B

|

None

|

0 👍

/

0 👎

// 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

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎