Math Operations
Some mathematical functions in JavaScript
This is a custom JavaScript library for some of Math methods.
This is not overriding built-in Math() method, just a custom module with different functions combined.
List of methods
Factorial | Combination | Permutation | Comma for large integer | Fibonacci sequence | Get factors from positive integer | Check prime number | Generate prime numbers | Generate Pascal's triangle | Least Common Multiple | Greatest Common Factor
DEMO
The main script is updated on 2015-09-02: this is the link.
The main script in this demo and main script in the main repository are the same.
I'll be adding more methods lator en.
Meanwhile, it consists of:
1. Factorial
Usage: math.factorial(n)
Example:
math.factorial(3)will generate output6(number).
2. Combination
Usage: math.combination(n, k)
Example:
math.combination(5, 2)will generate output10(number).
3. Permutation
Usage: math.permutation(n, k)
Example:
math.permutation(5, 2)will generate output20(number).n is total number
k is the number of combination or permutation
Also, browser only accommodates 21 digits without compacting it with the exponent. So, that.
4. Adding comma for large integer
Usage: math.addcomma(your_number)
your_numbercan be positive or negative. It has to be number (integer). If it has decimal point, it will be rounded down to nearest integer. Once again, browser only accommodates 21 digits before it is compacted using exponent.Examples:
math.addcomma(10000.8)will generate output10,000. It is astringoutput.math.addcomma(-28900251)will generate output-28,900,251(string).This isn't actually a math operation, more like structuring-number-so-it-can-be-read.
Explanation on
Monkey Raptor
5. Generating Fibonacci sequence
Usage: math.fibonacci(1st_number, 2nd_number, how_many)
You have to provide all three arguments.
Each has to be non-zero1st_numberand2nd_numbercan be zero andhow_manyhas to be greater than 0. It will also be rounded down to nearest integer if you put number with decimal point.Example:
math.fibonacci(-1, 2, 5)will generate output:-1, 2, 1, 3, 4, 7, 11(stringoutput). It started from -1 and 2, then generated 5 additional numbers sequence.Demo on
CodePen
6. Get the factors of positive integer
Usage: math.factorof(the_number, flag)
It has
flag.0to return only the factors (string).1to return list of multiplications of the factors.This is using iterations, so put considerably small input to avoid browser crash. It should work fine with max 5 digits length (99,999) input. I didn't put actual limit in this.
If the input is negative, it will be converted into positive.
Examples:
math.factorof(625, 0)or justmath.factorof(625)will generate output1, 5, 25, 125, 625(string).math.factorof(625, 1)will generate output1 * 625, 5 * 125, 25 * 25(string).
7. Check prime number
Usage: math.prime_check(the_number)
The
the_numberhas to be greater than 1 and an integer.Examples:
math.prime_check(210000000)will generate outputNot a prime number(string).math.prime_check(31)will generate outputPRIME number(string).Demo with user interface on
Port Raptor
8. Generate prime numbers
Usage: math.generate_prime(start_from, how_many)
The
start_fromhas to be greater than 1 and an integer.how_manyhas to be larger than 0.Examples:
math.generate_prime(2, 3)will generate output2, 3, 5(string).math.generate_prime(4, 1)will generate output5(string).Demo with user interface on
Port Raptor
9. Generate Pascal's triangle sequence
Usage: math.pascal_triangle(which_line [or] how_many, flag)
The
which_line [or] how_manyhas to be natural number (N*).flagcan be 0 or 1.
flag0 is for generating a particular line of Pascal's triangle sequence.flag1 is for generating all lines of Pascal's triangle sequence until the given limit.
PLEASE NOTE that this is using iteration, so, limit the input. I recommend around 20-ish. It will still work smoothly with that input range. Above that, the it'll "break" the browser.
Examples:
math.pascal_triangle(2, 0)will generate output1, 1(string).math.pascal_triangle(3, 1)will generate output1 | 1 1 | 1 2 1(string).Credit for neat example www.ywhmaths.webs.com/Arithmetic/Pascal.html
10. Least Common Multiple (LCM)
Usage: math.lcm(num_1, num_2[, num_3])
num_3is optional. It can calculate 2 or 3 arguments.Each input must be natural number (N*) (non-zero positive integer)
Examples:
math.lcm(12, 18)will generate output36(number).math.lcm(15, 8, 42)will generate output840(number).Demo on
Port Raptor
11. Greatest Common Factor (GCF)
Usage: math.gcf(num_1, num_2[, num_3])
num_3is also optional. It can calculate 2 or 3 arguments.Each input must be natural number (N*) (non-zero positive integer)
Examples:
math.gcf(12, 15)will generate output3(number).math.lcm(51, 18, 42)will generate output3(number).Demo on
Port Raptor
String to array
If you need to get array from string output for your other additional functions, it can be done with:
the_array = the_string_output.split(", ");This mostly uses
join(", ")to convert thearrayintostringoutput.Except the pascal_triangle and factorof with
flag1. You can customize thejoin()part in those.
How to use it in HTML
Grab the custom_math.js, you can compress it yourself.
Then:
<html>
<head>
<!--your head content-->
<script>
/*
\ *custom_math.js* goes here. It can be in HTML or external (synchronous with [script] tag).
*/
</script>
</head>
<body>
<!--your body content-->
<p>
So, blabla, something something, as such, we have the total ways of doing that, like:
<!--For instance, this is the element to be injected with *combination* result-->
<div id="something"></div>
</p>
<script>
//this is the element with id *something*
var your_element = document.getElementById("something");
//invoking the *combination* function and injecting the result to that element:
your_element.innerHTML = "Combination(5, 2) = " + math.combination(5, 2);
</script>
</body>
</html>Loading externally and asynchronously
For loading the script from external storage (not placed in the HTML) and asynchronous, read these gists:
-
Not recommended...Gist one: usingscripttag withasyncattribute and using timer to detect the readiness of the external script. -
Gist two: inject thescriptelement via JS. -
RecommendedUse the method used on the independent demo page (see page source) or look at the latesthtml file in the demo folder.