Euler_6
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
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 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 6 int func(int n) 7 { 8 int a = n; 9 int b = a * n; 10 int c = b * n; 11 int d = c * n; 12 13 return (3 * d + 2 * c - 3 * b - 2 * a) / 12; 14 } 15 16 int main(int argc, const char *argv[]) 17 { 18 printf("%d\n", func(100)); 19 20 return 0; 21 }
浙公网安备 33010602011771号