update 3 to the right solution
This commit is contained in:
parent
38a17714e4
commit
dbff279288
|
@ -1,27 +0,0 @@
|
|||
// 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +1,31 @@
|
|||
// 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
|
||||
long long sum = 0;
|
||||
long long n;
|
||||
scanf("%lld", &n);
|
||||
|
||||
// if n is smaller than 3
|
||||
if (n < 3) {
|
||||
printf("0\n");
|
||||
return 0;
|
||||
}
|
||||
// Initialize variables and get input from user.
|
||||
unsigned long long int num;
|
||||
scanf("%lld", &num);
|
||||
|
||||
// Iterate through all
|
||||
for (long long i = 3; i <= n; i++) {
|
||||
sum += i * (i - 1) * (i - 2);
|
||||
}
|
||||
|
||||
// Print the result
|
||||
printf("%lld\n", sum);
|
||||
// Calculate the sum of the numbers. (Can be optimized.)
|
||||
unsigned long long int num1 = 0;
|
||||
for (int i = 1; i <= num; i++) {
|
||||
num1 += sum(i);
|
||||
}
|
||||
|
||||
printf("%lld\n", num1);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue