-
Notifications
You must be signed in to change notification settings - Fork 0
/
oiler.js
133 lines (122 loc) · 2.9 KB
/
oiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
*
* @constructor
*/
var Oiler = function () {
};
/**
* Get the prime factors of 'n'.
* Shout-out: http://www.coderenaissance.com/2011/06/finding-prime-factors-in-javascript.html
*
* @param n
* @returns {*|Array}
*/
Oiler.prototype.getPrimeFactors = function primeFactorization(n) {
var root = Math.sqrt(n),
result = arguments[1] || [], // get unnamed parameter from recursive calls
x = 2;
if (n % x) { // if not divisible by 2
x = 3; // assign first odd
while ((n % x) && ((x = x + 2) < root)) {
} // iterate odds
}
// if no factor found then num is prime
x = (x <= root) ? x : n;
result.push(x); //push latest prime factor
// if num isn't prime factor make recursive call
return (x === n) ? result : primeFactorization(n / x, result);
};
/**
* Is 'n' a prime number?
* Embarrassingly simple.
*
* @param n
* @returns {boolean}
*/
Oiler.prototype.isEven = function (n) {
return n % 2 === 0;
};
/**
* Goldbach's other conjecture
* It was proposed by Christian Goldbach that every odd composite number can
* be written as the sum of a prime and twice a square.
* (It turns out that the conjecture was false.)
*
* @param n
* @returns {boolean}
*/
Oiler.prototype.isGoldbachsOtherConjecture = function (n) {
var self = this;
var diff;
if (self.isEven(n)) {
return false;
}
for (var i = 1; 2 * (i * i) < n; i++) {
diff = n - 2 * (i * i);
if (self.isPrime(diff)) {
return true;
}
}
return false;
};
/**
* Is 'n' a pandigital number?
*
* @param n
* @returns {boolean}
*/
Oiler.prototype.isPandigital = function (n) {
if (n % 9 !== 0) return false;
var masks = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512],
bits = 0;
do {
bits = bits | masks[n % 10];
n = Math.floor(n / 10);
} while (n > 0);
return bits == 1023;
};
/**
* Is 'n' a pentagonal number?
* I don't know where I found this formula.
*
* @param n
* @returns {boolean}
*/
Oiler.prototype.isPentagonal = function (n) {
return (Math.sqrt((n * 24) + 1) + 1) % 6 == 0;
};
/**
* Are n and m permutations of each other?
* Ugly, but works. Seems like there's a more elegant solution.
*
* @param n
* @param m
* @returns {boolean}
*/
Oiler.prototype.isPermutation = function (n, m) {
n = n.toString().split("").map(Number).sort().join('');
m = m.toString().split("").map(Number).sort().join('');
return n == m;
};
/**
* Is 'n' a prime number?
* Shout-out: http://en.wikipedia.org/wiki/Primality_test
*
* @param n
* @returns {boolean}
*/
Oiler.prototype.isPrime = function (n) {
if (n <= 3) {
return n > 1;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (var i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
};
module.exports = new Oiler();