Created
August 5, 2017 01:59
-
-
Save helton/50908f2c16229c50061df4f8eac5c6d8 to your computer and use it in GitHub Desktop.
Factorial with String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const factorial = number => { | |
let f = 1 | |
for (let i = 1; i <= number; i++) { | |
console.time('elapsed time') | |
f *= i; | |
console.log(`${i}! = ${f}`) | |
console.timeEnd('elapsed time') | |
if (f === Infinity) | |
break; | |
} | |
return f | |
} | |
const factorialString = number => { | |
let numberAsArray = asArray(number) | |
let f = asArray(1) | |
for (let i = 1; i <= number; i++) { | |
console.time('elapsed time') | |
f = multiply2StringArrays(f, asArray(i)) | |
console.log(`${i}! = ${arrayToString(f)}`) | |
console.timeEnd('elapsed time') | |
} | |
return arrayToString(f) | |
} | |
const asArray = n => [...String(n)] | |
const multiply2StringArrays = (leftArray, rightArray) => { | |
let result = [] | |
for (let i = 0; i < leftArray.length; i++) { | |
for (let j = 0; j < rightArray.length; j++) { | |
const leftChar = leftArray[i] | |
const rightChar = rightArray[j] | |
const sum = String(Number(leftChar) * Number(rightChar)) | |
const position = (leftArray.length - i) + (rightArray.length - j) + sum.length - 2 | |
const value = asArray(sum.padEnd(position, '0')) | |
result = [...result, value] | |
} | |
} | |
return sumStringArrays(result) | |
} | |
const sumStringArrays = arrays => arrays.reduce((acc, value) => sum2StringArrays(acc, value), []) | |
const sum2StringArrays = (leftArray, rightArray) => { | |
let result = [] | |
let maxSize = Math.max(leftArray.length, rightArray.length) | |
leftArray = leftPadArray(leftArray, maxSize) | |
rightArray = leftPadArray(rightArray, maxSize) | |
for (let i = maxSize - 1; i >= 0; i--) { | |
let leftValue = 0 | |
let rightValue = 0 | |
if (i < leftArray.length) { | |
leftValue = leftArray[i] | |
} | |
if (i < rightArray.length) { | |
rightValue = rightArray[i] | |
} | |
const value = String(Number(leftValue) + Number(rightValue)) | |
result = [value, ...result] | |
} | |
return normalizeArray(result) | |
} | |
const shouldNormalize = array => array.filter(item => [...item].length > 1).length > 0 | |
const leftPadArray = (array, size) => [...('0'.repeat(size - array.length)), ...array] | |
const rightPadArray = (array, size) => [...array, ...('0'.repeat(size - array.length))] | |
const arrayToString = array => array.join('') | |
const normalizeArray = (array) => { | |
if (!shouldNormalize(array)) | |
return array | |
let result = [] | |
for (let i = array.length - 1; i >= 0; i--) { | |
const value = array[i] | |
const pad = [...value].length + array.length - i - 1 | |
const valueAsArray = rightPadArray([...value], pad) | |
result = sum2StringArrays(result, valueAsArray) | |
} | |
return result | |
} | |
console.log( | |
factorial(99999) | |
) | |
console.log( | |
factorialString(99999) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment