array-math
Advanced tools
Comparing version 1.0.0 to 1.1.0
40
index.js
function factors(n) { | ||
var result = [] | ||
var startN = n | ||
var startN = Math.abs(Math.round(n)) | ||
var finished = false | ||
@@ -22,21 +22,22 @@ while(!finished) { | ||
function divisors(n, opts) { | ||
opts = opts?opts:{} | ||
var safeN = Math.abs(Math.round(n)) | ||
if (Math.abs(n)===safeN) | ||
opts = opts || {} | ||
var result = [1] | ||
for (var i=2; i<=n/2; i++) | ||
if (n%i==0) | ||
for (var i=2; i<=safeN/2; i++) | ||
if (safeN%i==0) | ||
result.push(i) | ||
if (!opts.proper) | ||
result.push(n) | ||
result.push(safeN) | ||
return result | ||
} | ||
function isPrime(num) { | ||
var n = num | ||
function isPrime(n) { | ||
var safeN = Math.abs(Math.round(n)) | ||
if (!n) { | ||
return false | ||
} else if (n!==2) { | ||
n = Math.abs(n) | ||
var goUntil = Math.ceil(Math.sqrt(n)) | ||
var goUntil = Math.ceil(Math.sqrt(safeN)) | ||
for (var i=2; i<=goUntil; i++) { | ||
if (n%i==0) { | ||
if (safeN%i==0) { | ||
return false | ||
@@ -49,16 +50,11 @@ } | ||
function range(low, high) { | ||
if (typeof low === 'undefined') { | ||
low = 0 | ||
high = 0 | ||
} else if (high && high<low) { | ||
var t = low | ||
low = high | ||
high = t | ||
} else if (!high) { | ||
high = low | ||
low = 0 | ||
function range() { | ||
var start = 0, step = 1, stop = arguments[0] || 0 | ||
if (arguments.length>=2) { | ||
start = arguments[0] || 0 | ||
stop = arguments[1] || 0 | ||
step = arguments[2] || 1 | ||
} | ||
var result = [] | ||
for(var i=low; i<high; i++) | ||
for(var i=start; i!=stop && (i>stop == i-step>stop); i+=step) | ||
result.push(i) | ||
@@ -65,0 +61,0 @@ return result |
{ | ||
"name": "array-math", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "./index.js", | ||
@@ -5,0 +5,0 @@ "author": { |
143
README.md
@@ -5,2 +5,3 @@ array-math | ||
[![Build Status](https://travis-ci.org/ArtskydJ/array-math.svg?branch=master)](https://travis-ci.org/ArtskydJ/array-math) | ||
[![Github Repository](http://img.shields.io/badge/Repository-Github-brightgreen.svg)](https://github.com/ArtskydJ/array-math) | ||
@@ -21,89 +22,115 @@ - [Install](#install) | ||
##Install | ||
Install with [NPM](http://nodejs.org) | ||
npm install array-math | ||
##Require | ||
var aMath = require('array-math') | ||
```js | ||
var aMath = require('array-math') | ||
``` | ||
##Methods | ||
##aMath.factors(n) | ||
`n` is a positive integer | ||
- `n` must be a positive integer | ||
aMath.factors(2) // -> [2] | ||
aMath.factors(96) // -> [2, 2, 2, 2, 2, 3] | ||
aMath.factors(100) // -> [2, 2, 5, 5] | ||
```js | ||
aMath.factors(2) // -> [2] | ||
aMath.factors(96) // -> [2, 2, 2, 2, 2, 3] | ||
aMath.factors(100) // -> [2, 2, 5, 5] | ||
``` | ||
##aMath.divisors(n) | ||
##aMath.divisors(n[, opts]) | ||
`n` is a positive integer | ||
- `n` must be a positive number. | ||
- `opts` is an object with the options. Defaults to `{}`. | ||
- `proper` can be inside `opts`. If `true`, it will make the resulting array not include `n`. Defaults to `false`. | ||
aMath.divisors(2) // -> [2] | ||
aMath.divisors(96) // -> [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 96] | ||
aMath.divisors(100) // -> [1, 2, 4, 5, 10, 20, 25, 50, 100] | ||
```js | ||
aMath.divisors(2) // -> [2] | ||
aMath.divisors(96) // -> [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 96] | ||
aMath.divisors(100) // -> [1, 2, 4, 5, 10, 20, 25, 50, 100] | ||
aMath.divisors(100, {proper:true}) // -> [1, 2, 4, 5, 10, 20, 25, 50] | ||
``` | ||
##aMath.isPrime(n) | ||
`n` is a positive integer | ||
- `n` must be a positive integer | ||
aMath.isPrime(2) // -> true | ||
aMath.isPrime(3) // -> true | ||
aMath.isPrime(4) // -> false | ||
aMath.isPrime(7) // -> true | ||
aMath.isPrime(96) // -> false | ||
aMath.isPrime(97) // -> true | ||
aMath.isPrime(100) // -> false | ||
aMath.isPrime(113) // -> true | ||
aMath.isPrime(117) // -> false | ||
```js | ||
aMath.isPrime(2) // -> true | ||
aMath.isPrime(3) // -> true | ||
aMath.isPrime(4) // -> false | ||
aMath.isPrime(7) // -> true | ||
aMath.isPrime(96) // -> false | ||
aMath.isPrime(97) // -> true | ||
aMath.isPrime(100) // -> false | ||
aMath.isPrime(113) // -> true | ||
aMath.isPrime(117) // -> false | ||
``` | ||
##aMath.range(n[, m]) | ||
##aMath.range([start,] stop [,step]) | ||
`n` is a number. If the `m` parameter was passed in, then `n` is the low number and m is the high number. If the m parameter was not passed in, then n is the high number. (See examples below.) | ||
- `start` is the starting number of the range. Defaults to `0`. If there are 2 or 3 arguments, this is assumed to be the first. | ||
- `stop` is the ending number of the range. Defaults to `0`. If there is 1 argument, this is assumed to be it. | ||
- `step` is the step between each number. Defaults to `1`. This is may not be `0`, and is set to `1` if it is. | ||
aMath.range(0) // -> [] | ||
aMath.range(1) // -> [0] | ||
aMath.range(2) // -> [0, 1] | ||
aMath.range(2, 2) // -> [] | ||
aMath.range(2, 3) // -> [2] | ||
aMath.range(3) // -> [0, 1, 2] | ||
aMath.range(10) // -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
aMath.range(2, 10) // -> [2, 3, 4, 5, 6, 7, 8, 9] | ||
aMath.range(5, 10) // -> [5, 6, 7, 8, 9] | ||
```js | ||
aMath.range() // -> [] | ||
aMath.range(0) // -> [] | ||
aMath.range(1) // -> [0] | ||
aMath.range(2) // -> [0, 1] | ||
aMath.range(2, 2) // -> [] | ||
aMath.range(2, 3) // -> [2] | ||
aMath.range(3) // -> [0, 1, 2] | ||
aMath.range(10) // -> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
aMath.range(2, 10) // -> [2, 3, 4, 5, 6, 7, 8, 9] | ||
aMath.range(5, 10) // -> [5, 6, 7, 8, 9] | ||
``` | ||
##aMath.multiply(a) | ||
`a` is an array of numbers (integers, floats, negative, whatever) | ||
- `a` must be an array of numbers (integers, floats, negative, whatever). | ||
aMath.multiply([2, 96, 100]) // -> 19200 | ||
aMath.multiply([40, 3, 17]) // -> 2040 | ||
aMath.multiply([4, 5, 2, 5.2, 3.8]) // -> 790.4 | ||
aMath.multiply([520, 0.2, 0.2, 0.8]) // -> 16.64 | ||
```js | ||
aMath.multiply([2, 96, 100]) // -> 19200 | ||
aMath.multiply([40, 3, 17]) // -> 2040 | ||
aMath.multiply([4, 5, 2, 5.2, 3.8]) // -> 790.4 | ||
aMath.multiply([520, 0.2, 0.2, 0.8]) // -> 16.64 | ||
``` | ||
##aMath.sum(a) | ||
`a` is an array of numbers (integers, floats, negative, whatever) | ||
aMath.sum([2, 96, 100]) // -> 198 | ||
aMath.sum([2, -96, 100]) // -> 6 | ||
aMath.sum([45, 20, 8.3]) // -> 73.3 | ||
- `a` must be an array of numbers (integers, floats, negative, whatever). | ||
```js | ||
aMath.sum([2, 96, 100]) // -> 198 | ||
aMath.sum([2, -96, 100]) // -> 6 | ||
aMath.sum([45, 20, 8.3]) // -> 73.3 | ||
``` | ||
##aMath.factorial(h[, l]) | ||
`h` is a number. It is the high number. It defaults to 0. | ||
`l` is a number. It is the low number. It defaults to 0. | ||
- `h` must be a number. It is the high number. It defaults to 0. | ||
- `l` must be a number. It is the low number. It defaults to 0. | ||
While multiplying, it will never multiply by 0. | ||
aMath.factorial() // -> 1 | ||
aMath.factorial(0) // -> 1 | ||
aMath.factorial(1) // -> 1 | ||
aMath.factorial(2) // -> 2 (2x1) | ||
aMath.factorial(3) // -> 6 (3x2x1) | ||
aMath.factorial(5) // -> 120 (5x4x3x2x1) | ||
aMath.factorial(5, 0) // -> 120 (5x4x3x2x1) | ||
aMath.factorial(5, 1) // -> 120 (5x4x3x2x1) | ||
aMath.factorial(5, 2) // -> 120 (5x4x3x2) | ||
aMath.factorial(5, 3) // -> 60 (5x4x3) | ||
aMath.factorial(5, 4) // -> 20 (5x4) | ||
aMath.factorial(10) // -> 3628800 (10x9x8x7x6x5x4x3x2x1) | ||
aMath.factorial(10, 3) // -> 1814400 (10x9x8x7x6x5x4x3) | ||
aMath.factorial(10, 5) // -> 151200 (10x9x8x7x6x5) | ||
```js | ||
aMath.factorial() // -> 1 | ||
aMath.factorial(0) // -> 1 | ||
aMath.factorial(1) // -> 1 | ||
aMath.factorial(2) // -> 2 (2x1) | ||
aMath.factorial(3) // -> 6 (3x2x1) | ||
aMath.factorial(5) // -> 120 (5x4x3x2x1) | ||
aMath.factorial(5, 0) // -> 120 (5x4x3x2x1) | ||
aMath.factorial(5, 1) // -> 120 (5x4x3x2x1) | ||
aMath.factorial(5, 2) // -> 120 (5x4x3x2) | ||
aMath.factorial(5, 3) // -> 60 (5x4x3) | ||
aMath.factorial(5, 4) // -> 20 (5x4) | ||
aMath.factorial(10) // -> 3628800 (10x9x8x7x6x5x4x3x2x1) | ||
aMath.factorial(10, 3) // -> 1814400 (10x9x8x7x6x5x4x3) | ||
aMath.factorial(10, 5) // -> 151200 (10x9x8x7x6x5) | ||
``` | ||
@@ -110,0 +137,0 @@ ##License |
@@ -47,1 +47,12 @@ var test = require('tap').test | ||
}) | ||
test('Check for anomalous conditions', function(t) { | ||
t.deepEqual(aMath.range(0, 3, 0), [0, 1, 2], 'step set to 0 should default to 1') | ||
t.deepEqual(aMath.range(5, 2, -1), [5, 4, 3], 'negative step should be allowed') | ||
t.deepEqual(aMath.range(1, 5.5), [1, 2, 3, 4, 5], 'floating stop that never hits exactly should be allowed') | ||
t.deepEqual(aMath.range(0, 2, 0.5), [0, 0.5, 1, 1.5], 'floating step should be allowed') | ||
t.deepEqual(aMath.range(4.3, 2, -0.5), [4.3, 3.8, 3.3, 2.8, 2.3], 'negative floating step, and floating start should be allowed') | ||
t.deepEqual(aMath.range(-2, -5, -1), [-2, -3, -4], 'passing stop should be allowed') | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12970
251
138