zoukankan      html  css  js  c++  java
  • projecteuler Sum square difference

    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.

    译文:

    自然数1到10的平方之和为385,他们的和的平方为3025,现将自然数1到10的和的平方减去平方之和等于2640,求出自然数从1到100的和的平方减去平方之和。

    ================================

    第一次code:

    
    
     1 public class Main
     2 {  
     3     public static void main(String[] args)
     4     {
     5         System.out.println(start(100)-run(100));
     6     }
     7     /**
     8      * 求前N项平方之和
     9      * @param n
    10      * @return
    11      */
    12     public static int run(int n)
    13     {
    14         int m=0;
    15         for(int i=0;i<n+1;i++)
    16         {
    17             m += i*i;
    18         }
    19         return m;
    20     }
    21     /**
    22      * 求前N项和的平方
    23      * @param n
    24      * @return
    25      */
    26     public static int start(int n)
    27     {
    28         int m=0,o=0;
    29         for(int i=0;i<n+1;i++)
    30         {
    31             m += i;
    32         }
    33         o = m*m;
    34         return o;
    35     }
    36 }
    
    
    
    
    
  • 相关阅读:
    微信开发-Jssdk调用分享实例
    软件工程
    Homework_4 四则运算
    Homework 3
    每周总结
    自动生成四则运算题目
    Homework 1 -- The beginning
    程序员视角的餐饮行业
    iOS9网络适配_ATS:改用更安全的HTTPS
    Xcode的 发展史
  • 原文地址:https://www.cnblogs.com/niithub/p/5801690.html
Copyright © 2011-2022 走看看