diff --git a/pset3/3-sum-problem.c b/pset3/3-sum-problem.c new file mode 100644 index 0000000..1d0ec6f --- /dev/null +++ b/pset3/3-sum-problem.c @@ -0,0 +1,27 @@ +// Try to use recursion to solve this problem. +#include + +// 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); + } +} + diff --git a/pset3/3-sum.c b/pset3/3-sum.c new file mode 100644 index 0000000..9e545a3 --- /dev/null +++ b/pset3/3-sum.c @@ -0,0 +1,24 @@ +#include + +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"); + return 0; + } + + // Iterate through all + for (long long i = 3; i < n; i++) { + sum += i * (i - 1) * (i - 2); + } + + // Print the result + printf("%lld\n", sum); + + return 0; +}