From dab1635011650ea9e3d5d38585d8a92975b6eb48 Mon Sep 17 00:00:00 2001 From: Preetiraj3697 Date: Wed, 19 Apr 2023 18:30:46 +0530 Subject: [PATCH] Issue Closed #218 Create Lanczos_approximation --- maths/lanczos_approximation.dart | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 maths/lanczos_approximation.dart diff --git a/maths/lanczos_approximation.dart b/maths/lanczos_approximation.dart new file mode 100644 index 00000000..756e78a9 --- /dev/null +++ b/maths/lanczos_approximation.dart @@ -0,0 +1,35 @@ +import 'dart:math'; + +double gamma(double x) { + const double pi = 3.141592653589793; + if (x < 0.5) { + return pi / (sin(pi * x) * gamma(1 - x)); + } + x -= 1; + double a = 0.99999999999980993; + const List p = [ + 676.5203681218851, -1259.1392167224028, 771.32342877765313, + -176.61502916214059, 12.507343278686905, -0.13857109526572012, + 9.9843695780195716e-6, 1.5056327351493116e-7 + ]; + for (int i = 0; i < p.length; i++) { + a += p[i] / (x + i + 1); + } + double t = x + p.length - 0.5; + return sqrt(2 * pi) * pow(t, x + 0.5) * exp(-t) * a; +} + +double factorial(num n) { + if (n is int) { + assert(n >= 0, "Factorial of a negative number does not exist"); + } + return gamma(n + 1); +} + +double signedFactorial(int n) { + if (n % 2 == 0) { + return factorial(n); + } else { + return -factorial(n); + } +} \ No newline at end of file