BUPT-homework/pset3/3-sum-problem.c
2021-10-12 13:18:54 +08:00

28 lines
537 B
C

// Try to use recursion to solve this problem.
#include <stdio.h>
// function prototypes.
unsigned long long int sum(unsigned long long int n);
int main(void) {
// Initialize variables and get input from user.
unsigned long long int num;
scanf("%lld", &num);
// Print values
printf("%lld\n", sum(num));
return 0;
}
unsigned long long int sum(unsigned long long int n) {
if (n == 1 || n == 2) { // Case 1: n is 1 or 2
return 0;
} else { // Case 2: n is not
return n * (n - 1) * (n - 2) + sum(n - 1);
}
}