Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
39
Task/Prime-decomposition/JavaScript/prime-decomposition-1.js
Normal file
39
Task/Prime-decomposition/JavaScript/prime-decomposition-1.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
function run_factorize(input, output) {
|
||||
var n = new BigInteger(input.value, 10);
|
||||
var TWO = new BigInteger("2", 10);
|
||||
var divisor = new BigInteger("3", 10);
|
||||
var prod = false;
|
||||
|
||||
if (n.compareTo(TWO) < 0)
|
||||
return;
|
||||
|
||||
output.value = "";
|
||||
|
||||
while (true) {
|
||||
var qr = n.divideAndRemainder(TWO);
|
||||
if (qr[1].equals(BigInteger.ZERO)) {
|
||||
if (prod)
|
||||
output.value += "*";
|
||||
else
|
||||
prod = true;
|
||||
output.value += "2";
|
||||
n = qr[0];
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
while (!n.equals(BigInteger.ONE)) {
|
||||
var qr = n.divideAndRemainder(divisor);
|
||||
if (qr[1].equals(BigInteger.ZERO)) {
|
||||
if (prod)
|
||||
output.value += "*";
|
||||
else
|
||||
prod = true;
|
||||
output.value += divisor;
|
||||
n = qr[0];
|
||||
}
|
||||
else
|
||||
divisor = divisor.add(TWO);
|
||||
}
|
||||
}
|
||||
40
Task/Prime-decomposition/JavaScript/prime-decomposition-2.js
Normal file
40
Task/Prime-decomposition/JavaScript/prime-decomposition-2.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
function run_factorize(n) {
|
||||
if (n <= 3)
|
||||
return [n];
|
||||
|
||||
var ans = [];
|
||||
var done = false;
|
||||
while (!done) {
|
||||
if (n % 2 === 0) {
|
||||
ans.push(2);
|
||||
n /= 2;
|
||||
continue;
|
||||
}
|
||||
if (n % 3 === 0) {
|
||||
ans.push(3);
|
||||
n /= 3;
|
||||
continue;
|
||||
}
|
||||
if (n === 1)
|
||||
return ans;
|
||||
var sr = Math.sqrt(n);
|
||||
done = true;
|
||||
// try to divide the checked number by all numbers till its square root.
|
||||
for (var i = 6; i <= (sr + 6); i += 6) {
|
||||
if (n % (i - 1) === 0) { // is n divisible by i-1?
|
||||
ans.push((i - 1));
|
||||
n /= (i - 1);
|
||||
done = false;
|
||||
break;
|
||||
}
|
||||
if (n % (i + 1) === 0) { // is n divisible by i+1?
|
||||
ans.push((i + 1));
|
||||
n /= (i + 1);
|
||||
done = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ans.push(n);
|
||||
return ans;
|
||||
}
|
||||
14
Task/Prime-decomposition/JavaScript/prime-decomposition-3.js
Normal file
14
Task/Prime-decomposition/JavaScript/prime-decomposition-3.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function factors(n) {
|
||||
if (!n || n < 2)
|
||||
return [];
|
||||
|
||||
var f = [];
|
||||
for (var i = 2; i <= n; i++){
|
||||
while (n % i === 0){
|
||||
f.push(i);
|
||||
n /= i;
|
||||
}
|
||||
}
|
||||
|
||||
return f;
|
||||
};
|
||||
48
Task/Prime-decomposition/JavaScript/prime-decomposition-4.js
Normal file
48
Task/Prime-decomposition/JavaScript/prime-decomposition-4.js
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/// <reference path="PrimeFactors.js" />
|
||||
|
||||
describe("Prime Factors", function() {
|
||||
it("Given nothing, empty is returned", function() {
|
||||
expect(factors()).toEqual([]);
|
||||
});
|
||||
|
||||
it("Given 1, empty is returned", function() {
|
||||
expect(factors(1)).toEqual([]);
|
||||
});
|
||||
|
||||
it("Given 2, 2 is returned", function() {
|
||||
expect(factors(2)).toEqual([2]);
|
||||
});
|
||||
|
||||
it("Given 3, 3 is returned", function() {
|
||||
expect(factors(3)).toEqual([3]);
|
||||
});
|
||||
|
||||
it("Given 4, 2 and 2 is returned", function() {
|
||||
expect(factors(4)).toEqual([2, 2]);
|
||||
});
|
||||
|
||||
it("Given 5, 5 is returned", function() {
|
||||
expect(factors(5)).toEqual([5]);
|
||||
});
|
||||
|
||||
it("Given 6, 2 and 3 is returned", function() {
|
||||
expect(factors(6)).toEqual([2, 3]);
|
||||
});
|
||||
|
||||
it("Given 7, 7 is returned", function() {
|
||||
expect(factors(7)).toEqual([7]);
|
||||
});
|
||||
|
||||
it("Given 8; 2, 2, and 2 is returned", function() {
|
||||
expect(factors(8)).toEqual([2, 2, 2]);
|
||||
});
|
||||
|
||||
it("Given a large number, many primes factors are returned", function() {
|
||||
expect(factors(2*2*2*3*3*7*11*17))
|
||||
.toEqual([2, 2, 2, 3, 3, 7, 11, 17]);
|
||||
});
|
||||
|
||||
it("Given a large prime number, that number is returned", function() {
|
||||
expect(factors(997)).toEqual([997]);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue