mirror of
https://github.com/Xevion/100prisoners.git
synced 2025-12-15 10:11:05 -06:00
Add probability helper functions
This commit is contained in:
@@ -3,4 +3,8 @@ export const range = (start: number, stop: number, step = 1) =>
|
||||
|
||||
export function classNames(...classes: (string | null | undefined)[]) {
|
||||
return classes.filter(Boolean).join(" ");
|
||||
}
|
||||
|
||||
export function sum(a: number, b: number): number {
|
||||
return a + b;
|
||||
}
|
||||
10
src/utils/math.ts
Normal file
10
src/utils/math.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import {range, sum} from "@/utils/helpers";
|
||||
|
||||
export function failureProbability(boxCount: number, attempts: number): number {
|
||||
const upperRange = boxCount - attempts;
|
||||
return range(attempts + 1, attempts + upperRange).map(n => 1.0 / n).reduce(sum);
|
||||
}
|
||||
|
||||
export function successProbability(boxCount: number, attempts: number) {
|
||||
return 1.0 - failureProbability(boxCount, attempts);
|
||||
}
|
||||
Reference in New Issue
Block a user