分类: C/C++
2007-06-23 18:57:32
typedef unsigned long ulong; //求自然数的数列之和 ulong natural_number_sequence_sum( ulong n) { return (ulong)(n*(n+1)/2); } //求自然数平方的数列之和 ulong natural_number_sequence_square_sum( ulong n) { return (ulong)(n*(n+1)*(2*n+1)/6); } //求自然数立方的数列之和 ulong natural_number_sequence_cube_sum( ulong n) { return (ulong)(n*n*(n+1)*(n+1)/4); } |
求1^2 + 2^2 + …… + n^2 = ? 解: (n+1)^3 = n^3 + 3n^2 + 3n + 1 所以 (n+1)^3 - n^3 = 3n^2 + 3n + 1 n^3 - (n-1)^3 = 3(n-1)^2 + 3(n-1) + 1 (n-1)^3 - (n-2)^3 = 3(n-2)^2 + 3(n-2) + 1 ............ 3^3 - 2^3 = 3*2^2 + 3*2 + 1 2^3 - 1^3 = 3*1^2 + 3*1 +1 把以上n个等式的两边分别相加得到 (n+1)^3-1^3 = 3×(1^2+2^2+3^2+...+n^2) + 3×(1+2+3+……+n) + n个1的和 (n+1)^3-1 = 3×(1^2+2^2+...+n^2) + 3×n×(n+1)/2 + n 所以 3(1^2+2^2+......+n^2) = n^3 + 3n^2 + 3n - 3n(n+1)/2 - n = n(n^2+3n+2) - 3n(n+1)/2 = n(n+1)(n+2)-3n(n+1)/2 = n(n+1)(2n+1)/2 最后 1^2+2^2+......+n^2 = n(n+1)(2n+1)/6. |