Skip to content

Instantly share code, notes, and snippets.

@JustenRickert
Last active April 13, 2019 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustenRickert/9ff94da53ef4d56ee3cd26707547f5a8 to your computer and use it in GitHub Desktop.
Save JustenRickert/9ff94da53ef4d56ee3cd26707547f5a8 to your computer and use it in GitHub Desktop.
Leibiz formula for pi
import { isArray, isString, isFunction } from "lodash";
const { performance } = require("perf_hooks");
type Parameters<Fn> = Fn extends (...args: infer T) => any ? T : never;
const trampoline = <Fn extends (...args: any[]) => any>(
fn: Fn,
...args: Parameters<Fn>
) => {
let result = fn(...args);
let i: number = 0;
while (result instanceof Function) {
result = result();
}
return result as ReturnType<Fn>;
};
const piOver4 = (precision: number) => {
const _piOver4 = (precision: number, current = 0, iteration = 0): number => {
const sign = iteration % 2 ? -1 : 1;
const nextApproximation = current + sign / (1 + 2 * iteration);
return Math.abs(current - nextApproximation) <= precision / 4
? (nextApproximation as any)
: ((() => _piOver4(precision, nextApproximation, iteration + 1)) as any);
};
return trampoline(_piOver4, precision);
};
const pi = (precision: number) => 4 * piOver4(precision);
const funcTimer = <Fn extends (...args: any[]) => any>(
fn: Fn,
...args: Parameters<Fn>
) => {
let result: ReturnType<Fn>;
const start = performance.now();
result = fn(...args);
const end = performance.now();
console.log({
time: (end - start).toFixed(3) + "ms",
result
});
return result;
};
const pi_while = (precision: number) => {
let approximation;
let nextApproximation = 0;
let iteration: number = 0;
do {
approximation = nextApproximation;
const sign = iteration % 2 ? -1 : 1;
const summand = sign / (1 + 2 * iteration);
nextApproximation = nextApproximation + summand;
iteration++;
} while (Math.abs(nextApproximation - approximation) > precision / 4);
return 4 * nextApproximation;
};
const factorial = (n: number): number => (n === 0 ? 1 : n * factorial(n - 1));
const e_while = (precision: number) => {
let approximation: number;
let nextApproximation: number = 0;
let iteration = 0;
do {
approximation = nextApproximation;
const summand = 1 / factorial(iteration);
nextApproximation += summand;
iteration++;
} while (Math.abs(nextApproximation - approximation) > precision);
return nextApproximation;
};
const timedPi = (precision: number) => funcTimer(pi, precision);
const timedPi_while = (precision: number) => funcTimer(pi_while, precision);
const timedE_while = (precision: number) => funcTimer(e_while, precision);
[
0.1,
0.01,
0.001,
0.0001,
0.00001,
0.000001,
0.0000001,
0.00000001,
0.000000001,
0.0000000001,
0.00000000001,
0.000000000001,
0.0000000000001,
0.00000000000001,
0.000000000000001,
0.0000000000000001,
0.00000000000000001
].forEach(timedE_while);
@JustenRickert
Copy link
Author

The while version had these times

{ time: '0.189ms', result: 3.189184782277596 }
{ time: '0.019ms', result: 3.1465677471829556 }
{ time: '0.177ms', result: 3.1420924036835256 }
{ time: '2.920ms', result: 3.1416426510898874 }
{ time: '1.324ms', result: 3.141597653564762 }
{ time: '5.234ms', result: 3.1415931535894743 }
{ time: '56.545ms', result: 3.1415927035898146 }
{ time: '532.923ms', result: 3.1415926485894077 }
{ time: '5139.991ms', result: 3.141592653088077 }

@JustenRickert
Copy link
Author

@JustenRickert
Copy link
Author

{ time: '0.277ms', result: 3.189184782277596 }
{ time: '0.041ms', result: 3.1465677471829556 }
{ time: '0.983ms', result: 3.1420924036835256 }
{ time: '5.079ms', result: 3.1416426510898874 }
{ time: '7.329ms', result: 3.141597653564762 }
{ time: '63.287ms', result: 3.1415931535894743 }
{ time: '566.173ms', result: 3.1415927035898146 }
{ time: '5674.105ms', result: 3.1415926485894077 }
{ time: '58817.543ms', result: 3.141592653088077 }

@JustenRickert
Copy link
Author

e

{ time: '0.160ms', result: 2.708333333333333 }
{ time: '0.007ms', result: 2.7166666666666663 }
{ time: '0.004ms', result: 2.7182539682539684 }
{ time: '0.004ms', result: 2.71827876984127 }
{ time: '0.004ms', result: 2.7182815255731922 }
{ time: '0.004ms', result: 2.7182818011463845 }
{ time: '0.005ms', result: 2.718281826198493 }
{ time: '0.005ms', result: 2.7182818282861687 }
{ time: '0.006ms', result: 2.7182818284467594 }
{ time: '0.007ms', result: 2.71828182845823 }
{ time: '0.007ms', result: 2.718281828458995 }
{ time: '0.006ms', result: 2.718281828458995 }
{ time: '0.007ms', result: 2.718281828459043 }
{ time: '0.008ms', result: 2.7182818284590455 }
{ time: '0.008ms', result: 2.7182818284590455 }
{ time: '0.008ms', result: 2.7182818284590455 }
{ time: '0.008ms', result: 2.7182818284590455 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment