28 lines
537 B
C
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);
|
||
|
}
|
||
|
}
|
||
|
|