Jump to content
IPS Community Suite 简体中文
Sign in to follow this  
ipscn

Q6

Recommended Posts


已知:

前10个正整数的和的平方是
(1 + 2 + ... + 10)^2 = 55^2 = 3025
前10个正整数的平方和是
1^2 + 2^2 + ... + 10^2 = 385

上面第一个结果减去第二个结果的差是:
3025 − 385 = 2640


那么,前100个正整数的和的平方与平方的和的差是多少?

package main

import (
	"fmt"
	"math"
)

func GetSquareOfSumElThan(intV int) int {

	sum := (intV + 1) * intV / 2

	return int(math.Pow(float64(sum), float64(2)))

}

func GetSumOfSquareElThan(intV int) int {

	i := 1
	var r int = 0
	for ; i <= intV; i++ {

		r += int(math.Pow(float64(i), float64(2)))
	}

	return r
}

func GetDiff(intV int) int {

	return GetSquareOfSumElThan(intV) - GetSumOfSquareElThan(intV)
}

func main() {

	intV := 100

	fmt.Println(GetDiff(intV))

}

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×