From a77f0d3afe5c737fe083abe9cd8cd6b1e3986fe3 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Fri, 24 Apr 2020 21:01:14 +0300 Subject: [PATCH 1/7] Create double_factorial Calculates the double factorial of an integer --- math/double_factorial | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 math/double_factorial diff --git a/math/double_factorial b/math/double_factorial new file mode 100644 index 000000000..5f41337f2 --- /dev/null +++ b/math/double_factorial @@ -0,0 +1,30 @@ +#include +#include + +/* Double factorial of a non-negative integer n, +is defined as the product of all the integers from 1 to n +that have the same parity (odd or even) as n. +It is also called as semifactorial of a number and is denoted by !! */ + +unsigned long long double_factorial_iterative(unsigned int n){ + unsigned long long res = 1; + for(unsigned long long i = n; i >= 0; i -= 2){ + if(i == 0 || i == 1) return res; + res *= i; + } +} + +/* Recursion can be costly for large numbers */ + +unsigned long long double_factorial_recursive(unsigned int n){ + if(n <= 1) return 1; + return n * double_factorial_recursive(n - 2); +} + +int main() +{ + int n{}; + std::cin >> n; + assert(n >= 0); + std::cout << double_factorial_iterative(n); +} From e4504d641b288a602c308d9fa77af549e8551e5f Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 16:18:32 +0300 Subject: [PATCH 2/7] Rename double_factorial to double_factorial.cpp --- math/{double_factorial => double_factorial.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename math/{double_factorial => double_factorial.cpp} (100%) diff --git a/math/double_factorial b/math/double_factorial.cpp similarity index 100% rename from math/double_factorial rename to math/double_factorial.cpp From e6874e4d18bced2ad2374edc9b0afe6ea607454f Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 22:28:20 +0300 Subject: [PATCH 3/7] Update double_factorial.cpp --- math/double_factorial.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 5f41337f2..3dd3e4a31 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -2,13 +2,15 @@ #include /* Double factorial of a non-negative integer n, -is defined as the product of all the integers from 1 to n +is defined as the product of +all the integers from 1 to n that have the same parity (odd or even) as n. -It is also called as semifactorial of a number and is denoted by !! */ +It is also called as semifactorial +of a number and is denoted by !! */ unsigned long long double_factorial_iterative(unsigned int n){ unsigned long long res = 1; - for(unsigned long long i = n; i >= 0; i -= 2){ + for( unsigned long long i = n; i >= 0; i -= 2 ){ if(i == 0 || i == 1) return res; res *= i; } From 9fef832f5749f09bcb055f31efa9dcd8f45cea03 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 22:34:44 +0300 Subject: [PATCH 4/7] Update double_factorial.cpp --- math/double_factorial.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 3dd3e4a31..8a918cde2 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -8,23 +8,22 @@ that have the same parity (odd or even) as n. It is also called as semifactorial of a number and is denoted by !! */ -unsigned long long double_factorial_iterative(unsigned int n){ +uint64_t double_factorial_iterative(unsigned int n){ unsigned long long res = 1; - for( unsigned long long i = n; i >= 0; i -= 2 ){ - if(i == 0 || i == 1) return res; + for ( unsigned long long i = n; i >= 0; i -= 2 ) { + if (i == 0 || i == 1) return res; res *= i; } } /* Recursion can be costly for large numbers */ -unsigned long long double_factorial_recursive(unsigned int n){ - if(n <= 1) return 1; +uint64_t double_factorial_recursive(unsigned int n){ + if (n <= 1) return 1; return n * double_factorial_recursive(n - 2); } -int main() -{ +int main() { int n{}; std::cin >> n; assert(n >= 0); From b4034871420aa7dfdc7e9228f7c663659f3e245b Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 22:37:50 +0300 Subject: [PATCH 5/7] Update double_factorial.cpp --- math/double_factorial.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 8a918cde2..6b9ff6500 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -8,8 +8,8 @@ that have the same parity (odd or even) as n. It is also called as semifactorial of a number and is denoted by !! */ -uint64_t double_factorial_iterative(unsigned int n){ - unsigned long long res = 1; +uint64_t double_factorial_iterative(uint64_t n) { + uint64_t res = 1; for ( unsigned long long i = n; i >= 0; i -= 2 ) { if (i == 0 || i == 1) return res; res *= i; @@ -18,13 +18,13 @@ uint64_t double_factorial_iterative(unsigned int n){ /* Recursion can be costly for large numbers */ -uint64_t double_factorial_recursive(unsigned int n){ +uint64_t double_factorial_recursive(uint64_t n) { if (n <= 1) return 1; return n * double_factorial_recursive(n - 2); } int main() { - int n{}; + uint64_t n{}; std::cin >> n; assert(n >= 0); std::cout << double_factorial_iterative(n); From 2aaba721fb5ae6c973b586aaec0b05bfbb722077 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sat, 25 Apr 2020 22:38:53 +0300 Subject: [PATCH 6/7] Update double_factorial.cpp --- math/double_factorial.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 6b9ff6500..1b09bebb4 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -10,7 +10,7 @@ of a number and is denoted by !! */ uint64_t double_factorial_iterative(uint64_t n) { uint64_t res = 1; - for ( unsigned long long i = n; i >= 0; i -= 2 ) { + for ( uint64_t i = n; i >= 0; i -= 2 ) { if (i == 0 || i == 1) return res; res *= i; } From c4f3f915f874dd66f19edec04b95e59d237a0a2d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 26 Apr 2020 08:41:22 +0200 Subject: [PATCH 7/7] Update double_factorial.cpp --- math/double_factorial.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/math/double_factorial.cpp b/math/double_factorial.cpp index 1b09bebb4..7b0d1d970 100644 --- a/math/double_factorial.cpp +++ b/math/double_factorial.cpp @@ -1,12 +1,9 @@ #include #include -/* Double factorial of a non-negative integer n, -is defined as the product of -all the integers from 1 to n -that have the same parity (odd or even) as n. -It is also called as semifactorial -of a number and is denoted by !! */ +/* Double factorial of a non-negative integer n, is defined as the product of +all the integers from 1 to n that have the same parity (odd or even) as n. +It is also called as semifactorial of a number and is denoted by !! */ uint64_t double_factorial_iterative(uint64_t n) { uint64_t res = 1;