首页 > 其他 > 详细

我的欧拉工程之路_6

时间:2014-04-06 20:45:19      阅读:532      评论:0      收藏:0      [点我收藏+]

Sum square difference

Problem 6

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 bubuko.com,布布扣 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

平方和之差

问题 6

前十个正自然数的平方的和如下,

12 + 22 + ... + 102 = 385

前十个正自然数的和的平方如下,

(1 + 2 + ... + 10)2 = 552 = 3025

因此前10个正自然数的和的平方与前10个正自然数的平方的和的差是 3025 bubuko.com,布布扣 385 = 2640.

求前100个正自然数的和的平方与前100个正自然数的平方的和的差是多少.

public class Euler6
{
    public static void main(String[] args)
    {
        int sumSquare=0,t=0,squareSum=0;
        for(int i=1;i<=100;i++)
        {
            squareSum+=i*i;
        }
        t=100*(100+1)/2;
        sumSquare=t*t;
        System.out.print("平方和之差=");
        System.out.println(sumSquare-squareSum);
    }
}


本文出自 “迁客的小屋” 博客,请务必保留此出处http://qianke.blog.51cto.com/5616176/1391173

我的欧拉工程之路_6,布布扣,bubuko.com

我的欧拉工程之路_6

原文:http://qianke.blog.51cto.com/5616176/1391173

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!