尔冬橙

博客园 首页 新随笔 联系 订阅 管理

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 − 385 = 2640.

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

(a+b+c)^2-(a^2+b^2+c^2)=a^2+b^2+c^2+2ab+2ac+2bc-a^2-b^2-c^2 =2(ab+ab+bc)

结论:任意2个不同数向乘的累加和的2倍。

static void Main(string[] args)
        {
            long startTime, endTime;
            Int64 f=0;
            int i,j;
            startTime = DateTime.Now.Ticks;
            for (i = 1; i <= 99; i++)
            {
                for (j = i + 1; j <= 100; j++)
                {
                    f += i * j;
                }
            }
            endTime = DateTime.Now.Ticks;
            Console.WriteLine("result:{0}  run time:{1} ms",2*f,(endTime-startTime)/10000.0);
            Console.ReadLine();
        }

 结果:25164150

 

posted on 2012-05-13 22:29  尔冬橙  阅读(250)  评论(0编辑  收藏  举报