-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added binomial_coefficient.js, count_decimal_digits.js, factorial.js,…
… multiply_range.js, round.js, truncate_decimal.js
- Loading branch information
1 parent
200fc34
commit 5d9aa07
Showing
47 changed files
with
445 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean.js"); | ||
|
||
function absolute(n) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean.js"); | ||
const compare_positive = require("./compare_positive.js"); | ||
const factorial = require("./factorial.js"); | ||
const long_addition = require("./long_addition.js"); | ||
const long_subtraction = require("./long_subtraction.js"); | ||
const long_division = require("./long_division.js"); | ||
const multiply_range = require("./multiply_range.js"); | ||
|
||
function binomial_coefficient(n, k) { | ||
n = clean(n); | ||
k = clean(k); | ||
|
||
switch (compare_positive(n, k)) { | ||
case "=": | ||
return "1"; | ||
case ">": { | ||
const diff = long_subtraction(n, k); | ||
const numerator = multiply_range(long_addition(k, "1"), n); | ||
const denominator = factorial(diff); | ||
return long_division(numerator, denominator); | ||
} | ||
case "<": { | ||
throw new Error("[binominal_coefficient] unsupported"); | ||
} | ||
} | ||
} | ||
|
||
module.exports = binomial_coefficient; | ||
module.exports.default = binomial_coefficient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean.js"); | ||
const compare_positive = require("./compare_positive.js"); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const clean = require("./clean.js"); | ||
"use strict"; | ||
|
||
// given: | ||
// - a and b are positive numbers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean.js"); | ||
|
||
function count_decimal_digits(n) { | ||
n = clean(n); | ||
|
||
const i = n.indexOf("."); | ||
|
||
// n is an integer | ||
if (i === -1) return "0"; | ||
|
||
return (n.length - i - 1).toString(); | ||
} | ||
|
||
module.exports = count_decimal_digits; | ||
module.exports.default = count_decimal_digits; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
const multiply_range = require("./multiply_range.js"); | ||
|
||
function factorial(n) { | ||
if (n === "0") return "1"; | ||
return multiply_range("1", n); | ||
} | ||
|
||
module.exports = factorial; | ||
module.exports.default = factorial; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
function is_factorial(n) { | ||
return !!n.match(/^\d+!$/i); | ||
} | ||
|
||
module.exports = is_factorial; | ||
module.exports.default = is_factorial; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module.exports = function is_infinity(n) { | ||
"use strict"; | ||
|
||
function is_infinity(n) { | ||
return !!n.match(/^(|-|\+)inf(inity)?$/i); | ||
}; | ||
} | ||
|
||
module.exports = is_infinity; | ||
module.exports.default = is_infinity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean.js"); | ||
const is_infinity = require("./is_infinity.js"); | ||
|
||
module.exports = function is_integer(n) { | ||
function is_integer(n) { | ||
if (is_infinity(n)) return false; | ||
n = clean(n); | ||
return !n.includes("."); | ||
}; | ||
} | ||
|
||
module.exports = is_integer; | ||
module.exports.default = is_integer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean"); | ||
|
||
function is_negative(n) { | ||
n = clean(n); | ||
return n[0] === "-"; | ||
} | ||
|
||
module.exports = is_negative; | ||
module.exports.default = is_negative; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module.exports = function is_negative_infinity(n) { | ||
"use strict"; | ||
|
||
function is_negative_infinity(n) { | ||
return !!n.match(/^-inf(inity)?$/i); | ||
}; | ||
} | ||
|
||
module.exports = is_negative_infinity; | ||
module.exports.default = is_negative_infinity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module.exports = function is_positive_infinity(n) { | ||
"use strict"; | ||
|
||
function is_positive_infinity(n) { | ||
return !!n.match(/^\+?inf(inity)?$/i); | ||
}; | ||
} | ||
|
||
module.exports = is_positive_infinity; | ||
module.exports.default = is_positive_infinity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
module.exports = function is_zero(n) { | ||
"use strict"; | ||
|
||
function is_zero(n) { | ||
return /^[-+]?0(\.0+)?(e[\.\d]+)?$/.test(n); | ||
}; | ||
} | ||
|
||
module.exports = is_zero; | ||
module.exports.default = is_zero; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"use strict"; | ||
|
||
const clean = require("./clean.js"); | ||
const compare = require("./compare.js"); | ||
|
||
|
Oops, something went wrong.