Skip to content

Instantly share code, notes, and snippets.

@tomfun
Last active August 8, 2016 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomfun/b143b054c054fc9405d9b0c3af99a0b8 to your computer and use it in GitHub Desktop.
Save tomfun/b143b054c054fc9405d9b0c3af99a0b8 to your computer and use it in GitHub Desktop.
/*
```bash
npm i big-integer
```
*/
// const input = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,];
// const input = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,];
const input = [2, 3, 5, 7, 11, 13];
// const input = [2, 3, 5, 7, 11];
// const input = [2, 3, 5, 7];
// const input = [2, 3, 5];
// const input = [2, 3];
const out = {};
const bigInt = require('big-integer');
function init() {
out[1] = [];
for (let i = 0; i < input.length; i++) {
input[i] = bigInt(input[i]); // for string multiplying
out[1].push({
out: input[i],
numbers: new Set([input[i]]),
});
}
}
function log(output) {
// console.log(output);
for (const i in output) {
if (!output.hasOwnProperty(i)) {
continue;
}
const o = output[i];
console.log(o.map((v) => v.out).join(', '));
}
// console.log(output.map((v) => toFixed(v.out)).join(', '));
}
function verbose(output) {
// console.log(output);
for (const i in output) {
if (!output.hasOwnProperty(i)) {
continue;
}
const o = output[i];
console.log(o.map((v) => ({n: v.out.toString(), ns: [...v.numbers].map((n) => n.toString())})), o.length);
}
// console.log(output.map((v) => toFixed(v.out)).join(', '));
}
function multiplyAll(numbers, output) {
const processedHashs = new Set();
for (let multiTimes = 1; multiTimes < input.length; multiTimes++) {
const nextMultiTimes = multiTimes + 1;
output[nextMultiTimes] = [];
numbers.forEach((v) => {
const outputPart = output[multiTimes];
for (let i = 0; i < outputPart.length; i++) {
const item = outputPart[i];
if (item.numbers.has(v)) {
continue;
}
const newSet = new Set();
for (const t1 of item.numbers) {
if (t1.greater(v)) {
newSet.add(v);
}
newSet.add(t1);
}
if (newSet.size === item.numbers.size) {
newSet.add(v);
}
const hash = [...newSet].join(',');
if (processedHashs.has(hash)) {
continue;
}
processedHashs.add(hash);
output[nextMultiTimes].push({
// out: item.out * v,
out: item.out.multiply(v),
numbers: newSet,
});
}
});
}
}
init();
multiplyAll(input, out);
log(out);
// verbose(out);
// console.log('for you, Number.MAX_SAFE_INTEGER is ', Number.MAX_SAFE_INTEGER)
// console.log(2.3055679639455185e+36, 2305567963945518400000000000000000000)
// console.log(bigInt(5).multiply(2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment