diff --git a/Gruntfile.js b/Gruntfile.js index 6612e88..4c65a27 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -50,7 +50,8 @@ module.exports = function(grunt) { 'src/factorial.js', 'src/isAbundant.js', 'src/permute.js', - 'src/fibonacci.js' + 'src/fibonacci.js', + 'src/divideInts.js' ], dest: 'Methodus.js', }, diff --git a/Methodus.js b/Methodus.js index 1abb8d6..f5efa9c 100644 --- a/Methodus.js +++ b/Methodus.js @@ -1,11 +1,11 @@ /** - * methodus - v1.0.71 + * methodus - v1.0.72 * A Math Library inspired by Project Euler * @author Pamblam */ function Methodus(){ - this.version = "1.0.71"; + this.version = "1.0.72"; } if(!!(typeof module !== 'undefined' && module.exports)) module.exports = Methodus; @@ -390,4 +390,29 @@ Methodus.prototype.fibonacci = function (funct) { b = temp; if (funct(i, b) === false) return; } +}; + +Methodus.prototype.divideInts = function(n, d, peren, maxlen) { + var num = n + "", + numLength = num.length, + remainder = 0, + answer = '', + pStack = [], + pattern, i, pIndex, digit; + maxlen = maxlen || 1000000; + for(i=0; i < numLength + maxlen; i++) { + digit = i < numLength ? parseInt(num[i]) : 0; + if (i == numLength) answer = answer + "."; + answer = answer + Math.floor((digit + (remainder * 10)) / d); + remainder = (digit + (remainder * 10)) % d; + pattern = remainder + "," + digit; + pIndex = pStack.indexOf(pattern); + if (pIndex > -1) { + return peren ? + answer.substring(0, pIndex + 1) + "(" + answer.substring(pIndex + 1, answer.length - 1) + ")" : + answer.substring(0, answer.length - 1); + } + pStack.push(pattern); + } + return answer; }; \ No newline at end of file diff --git a/README.md b/README.md index f90fef9..59d12d2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ## Methodus.js -**Version 1.0.71** | [NPM](https://www.npmjs.com/package/methodus) | [Github](https://github.com/Pamblam/Methodus) +**Version 1.0.72** | [NPM](https://www.npmjs.com/package/methodus) | [Github](https://github.com/Pamblam/Methodus) Named after Euler's work which includes 100 problems that Euler considers to illustrate his methods, and was, in all likelihood, part of the inspiration for Project Euler. Thus, the library is an arbitrary list of mathematical algorithms and helper functions that are useful for, among other things, solving the Project Euler problems. @@ -36,7 +36,7 @@ Create a new instance with `const m = new Methodus`. The constructor does not ac | **`m.primeFactors(1233)`** | Will return n array containing the prime factorization of `1233`. | | **`m.range(3,78)`** | Will return an array of numbers ranging from `3` to `78` | | **`m.smallestCommonMultiple(123, 32, 12, 32)`** | Will return the smallest common multiple of all the arguments passed to it. You can pass as many arguments as you need. | -| **`m.greatestCommonDivisor(1.0.71)`** | Will return the greatest common divisor of the two numbers passed to it. | +| **`m.greatestCommonDivisor(1.0.72)`** | Will return the greatest common divisor of the two numbers passed to it. | | **`m.pythagoreanTripletFromCircumference(1000)`** | Will generate a Pythagorean triplet for a triangle whose circumference is 1000. | | **`m.generateTriangleNumbers(funct)`** | Will call `funct` once for every triangle number until `funct` returns false. The function is provided the index of the triangle number and it's value. | | **`m.addInts(arr)`** | Will add the large integers in the array `arr` and return the sum. | @@ -49,4 +49,5 @@ Create a new instance with `const m = new Methodus`. The constructor does not ac | **`m.factorial(n)`** | Will return the factorial of `n`. | | **`m.isAbundant(n)`** | Will return `true` if `n` is an abundant number, else false. | | **`m.permute(arr, limit)`** | Will all permutations of `arr`, optionally truncating at the given `limit`, if provided. | -| **`m.fibonacci(funct)`** | Will continue to call `funct`, passing it the index of the fibonacci sequence as well as the value at that index (starting at one) until the function returns `false`. | \ No newline at end of file +| **`m.fibonacci(funct)`** | Will continue to call `funct`, passing it the index of the fibonacci sequence as well as the value at that index (starting at one) until the function returns `false`. | +| **`m.divideInts(a, b, perens, limit)`** | Will return the result of `a` divided by `b`. If `perens` is true it will put repeating digits in parentheses. Result is truncated after the first occurrence of repeating digits, or at the number of digits defined by `limit`. | \ No newline at end of file diff --git a/package.json b/package.json index 4ab31f7..a2c5e8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "methodus", - "version": "1.0.71", + "version": "1.0.72", "keywords": [ "Project Euler", "algorithms", diff --git a/solutions/26.js b/solutions/26.js new file mode 100644 index 0000000..6da18af --- /dev/null +++ b/solutions/26.js @@ -0,0 +1,13 @@ + +const M = require("../Methodus.js"); +var lr = 0, i, d, l, m=new M(); +for(i=2; i<1000; i++){ + d = m.divideInts(1, i, true); + if(d.indexOf("(") > -1){ + l = d.match(/\(\d+\)/g)[0].length-2; + if(l > lr){ + lr = l; + console.log("New longest: 1/"+i+" ("+l+" repeating) = "+d); + } + } +} \ No newline at end of file diff --git a/src/divideInts.js b/src/divideInts.js new file mode 100644 index 0000000..bc06f40 --- /dev/null +++ b/src/divideInts.js @@ -0,0 +1,25 @@ + +Methodus.prototype.divideInts = function(n, d, peren, maxlen) { + var num = n + "", + numLength = num.length, + remainder = 0, + answer = '', + pStack = [], + pattern, i, pIndex, digit; + maxlen = maxlen || 1000000; + for(i=0; i < numLength + maxlen; i++) { + digit = i < numLength ? parseInt(num[i]) : 0; + if (i == numLength) answer = answer + "."; + answer = answer + Math.floor((digit + (remainder * 10)) / d); + remainder = (digit + (remainder * 10)) % d; + pattern = remainder + "," + digit; + pIndex = pStack.indexOf(pattern); + if (pIndex > -1) { + return peren ? + answer.substring(0, pIndex + 1) + "(" + answer.substring(pIndex + 1, answer.length - 1) + ")" : + answer.substring(0, answer.length - 1); + } + pStack.push(pattern); + } + return answer; +}; \ No newline at end of file