Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamblam committed Jan 1, 2018
0 parents commit 682b5dd
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/nbproject/
package-lock.json
node_modules
72 changes: 72 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module.exports = function(grunt) {

var pkg = grunt.file.readJSON('package.json');
pkg.version = pkg.version.split(".");
var subversion = pkg.version.pop();
subversion++;
pkg.version.push(subversion);
pkg.version = pkg.version.join(".");
grunt.file.write('package.json', JSON.stringify(pkg, null, 2));

console.log("---------------------------------------");
console.log(" Building Methodus Version "+pkg.version);
console.log("---------------------------------------");

grunt.initConfig({
pkg: pkg,
concat: {
options: {
banner: '/**\n * <%= pkg.name %> - v<%= pkg.version %>' +
'\n * <%= pkg.description %>' +
'\n * @author <%= pkg.author %>' +
'\n */\n\n'
},
dist: {
src: [
'src/constructor.js',
'src/primeFactors.js',
'src/allFactors.js',
'src/count.js',
'src/everySubsequence.js',
'src/nthPrime.js',
'src/range.js',
'src/smallestCommonMultiple.js'
],
dest: 'Methodus.js',
},
},
'string-replace': {
source: {
files: {
"Methodus.js": "Methodus.js"
},
options: {
replacements: [{
pattern: /{{ VERSION }}/g,
replacement: '<%= pkg.version %>'
}]
}
},
readme: {
files: {
"README.md": "README.md"
},
options: {
replacements: [{
pattern: /\d+.\d+.\d+/g,
replacement: '<%= pkg.version %>'
}]
}
}
}
});

grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-string-replace');

grunt.registerTask('default', [
'concat',
'string-replace'
]);

};
102 changes: 102 additions & 0 deletions Methodus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* Methodus - v1.0.15
* A Math Library inspired by Project Euler
* @author Pamblam
*/

function Methodus(){
this.version = "1.0.15";
}

if(!!(typeof module !== 'undefined' && module.exports)) module.exports = Methodus;

Methodus.prototype.primeFactors = function(n){
var o = [], i;
while (n%2 == 0){
o.push(2);
n = n/2;
}
for (i=3, s=Math.sqrt(n); i<=s; i=i+2){
while (n%i == 0){
o.push(i);
n = n/i;
}
}
if (n > 2) o.push(n);
return o.sort((a,b)=>a-b);
};

Methodus.prototype.allFactors = function(n){
var factors = [1, n],
lowestProduct = n,
highestMultiplier;
for(highestMultiplier = 1; lowestProduct >= highestMultiplier; highestMultiplier++){
if(n % highestMultiplier === 0){
lowestProduct = n / highestMultiplier;
if(!~factors.indexOf(lowestProduct)) factors.push(lowestProduct);
if(!~factors.indexOf(highestMultiplier)) factors.push(highestMultiplier);
}
}
return factors.sort((a,b)=>a-b);
};

Methodus.prototype.count=function(array, needle) {
var count = 0, i;
for (i = 0; i < this.length; i++)
if (array[i] === needle) count++;
return count;
};

Methodus.prototype.everySubsequence = function(seq, len, funct){
var a = seq.split(''), n, i, subseq = [], e;
for(i=0; i<a.length; i++){
e = Math.min(a.length, i+len);
for(subseq = [], n=i; n<e; n++) subseq.push(a[n]);
funct(i, subseq);
}
};

Methodus.prototype.nthPrime = function(n){
let primes = [2,3,5];
const isPrime = num => {
for(let n=0; n<primes.length; n++){
if(num===primes[n]) return true;
if(num%primes[n]===0) return false;
}
return true;
}
for(var i=3; primes.length<n; i++){
if(isPrime(i)){
if(primes.indexOf(i) === -1) primes.push(i);
}
}
return primes.pop();
};

Methodus.prototype.range = function(lowestInt, highestInt){
var a = [], i;
for(i=lowestInt; i<=highestInt; i++) a.push(i);
return a;
};

Methodus.prototype.smallestCommonMultiple = function(){
var args = Array.from(arguments),
o = {},
i, p, n, c, m;
for(i=0; i<args.length; i++){
p = this.primeFactors(args[i]);
for(n=0; n<p.length; n++){
c = this.count(p, p[n]);
if(!o[p[n]] || c>o[p[n]]) o[p[n]]=c;
}
}
var n=1;
for(m in o){
if(!o.hasOwnProperty(m)) continue;
while(o[m]){
n = n*m;
o[m]--;
}
}
return n;
};
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

## Methodus.js

**Version 1.0.15**

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.

<center>
![enter image description here](https://i.imgur.com/GmQbuBT.jpg)
</center>

<hr>

## Usage

Create a new instance with `const m = new Methodus`. The constructor does not accept any arguments.

## Methods

- **`m.allFactors(123)`** Will return an array of all factors of `123` both prime and composite.
- **`m.count(myArray, 4)`** Will return an integer representing the number of times `4` occurs in `myArray`.
- **`m.everySubsequence(seq, 4, funct)`** Will execute `funct` for every `4` char subsequence in the string `seq`, passing the starting index of the current sequence and `4` char subsequence as an array to `funct`.
- **`m.nthPrime(8)`** Will return the `8`th Prime number.
- **`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.
18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Methodus",
"version": "1.0.15",
"keywords": [
"Project Euler",
"algorithms",
"math"
],
"author": "Pamblam",
"contributors": [],
"dependencies": {},
"description": "A Math Library inspired by Project Euler",
"devDependencies": {
"grunt-contrib-concat": "^1.0.1",
"grunt-string-replace": "^1.3.1",
"grunt": "^1.0.1"
}
}
7 changes: 7 additions & 0 deletions solutions/3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const M = require("../Methodus.js");

var lpf = new M().primeFactors(600851475143).pop();

console.log(lpf);


44 changes: 44 additions & 0 deletions solutions/4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

const M = require("../Methodus.js");

/**
* Get the greatest palindrome number less than a given integer
* @param {type} int
* @returns {Number}
*/
function getHighestPalidromeLessThan(int){
const reverseInt = int=>parseInt((""+int).split("").reverse().join("")),
isIntPalindrome = int=>reverseInt(int)===int;
for(var i=int; i--;){
if(isIntPalindrome(i)){
return i;
}
}
return 0;
}

/**
* Get teh largest palindrome that is the product of 2 n-digit numbers
* @param {type} digits
* @returns {Number}
*/
function largestPalindromeProduct(digits){
let n = parseInt("9".repeat(digits)), pfacts, i, m, met=new M;
for(var p=n*n+1; p=getHighestPalidromeLessThan(p);){
pfacts = m.allFactors(p);
for(i=pfacts.length; i--;){
if(pfacts[i]>n) continue;
m = p/pfacts[i];
if(m>n) continue;
return {
multiplicand: pfacts[i],
multiplier: m,
product: p
}
}
}
}

var res = largestPalindromeProduct(3),
answer = res.product+" = "+res.multiplicand+" x "+res.multiplier;
console.log("Largest Palindromic Product of 2 Three-digit numbers is: \n\t"+answer);
7 changes: 7 additions & 0 deletions solutions/5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

const M = require("../Methodus.js");
methodus = new M;

var range = methodus.range(1,20);
console.log(methodus.smallestCommonMultiple(...range));

4 changes: 4 additions & 0 deletions solutions/7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

const M = require("../Methodus.js");

console.log(new M().nthPrime(10001));
13 changes: 13 additions & 0 deletions solutions/8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

const M = require("../Methodus.js");

var seq = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
var greatestSubseqProduct = 0;

new M().everySubsequence(seq, 13, (index, seq)=>{
if(seq.length !== 13) return;
var product = seq[0] * seq[1] * seq[2] * seq[3];
if(product > greatestSubseqProduct) greatestSubseqProduct = product;
});

console.log(greatestSubseqProduct);
14 changes: 14 additions & 0 deletions src/allFactors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

Methodus.prototype.allFactors = function(n){
var factors = [1, n],
lowestProduct = n,
highestMultiplier;
for(highestMultiplier = 1; lowestProduct >= highestMultiplier; highestMultiplier++){
if(n % highestMultiplier === 0){
lowestProduct = n / highestMultiplier;
if(!~factors.indexOf(lowestProduct)) factors.push(lowestProduct);
if(!~factors.indexOf(highestMultiplier)) factors.push(highestMultiplier);
}
}
return factors.sort((a,b)=>a-b);
};
5 changes: 5 additions & 0 deletions src/constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Methodus(){
this.version = "{{ VERSION }}";
}

if(!!(typeof module !== 'undefined' && module.exports)) module.exports = Methodus;
7 changes: 7 additions & 0 deletions src/count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Methodus.prototype.count=function(array, needle) {
var count = 0, i;
for (i = 0; i < this.length; i++)
if (array[i] === needle) count++;
return count;
};
9 changes: 9 additions & 0 deletions src/everySubsequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Methodus.prototype.everySubsequence = function(seq, len, funct){
var a = seq.split(''), n, i, subseq = [], e;
for(i=0; i<a.length; i++){
e = Math.min(a.length, i+len);
for(subseq = [], n=i; n<e; n++) subseq.push(a[n]);
funct(i, subseq);
}
};
17 changes: 17 additions & 0 deletions src/nthPrime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Methodus.prototype.nthPrime = function(n){
let primes = [2,3,5];
const isPrime = num => {
for(let n=0; n<primes.length; n++){
if(num===primes[n]) return true;
if(num%primes[n]===0) return false;
}
return true;
}
for(var i=3; primes.length<n; i++){
if(isPrime(i)){
if(primes.indexOf(i) === -1) primes.push(i);
}
}
return primes.pop();
};
16 changes: 16 additions & 0 deletions src/primeFactors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Methodus.prototype.primeFactors = function(n){
var o = [], i;
while (n%2 == 0){
o.push(2);
n = n/2;
}
for (i=3, s=Math.sqrt(n); i<=s; i=i+2){
while (n%i == 0){
o.push(i);
n = n/i;
}
}
if (n > 2) o.push(n);
return o.sort((a,b)=>a-b);
};
Loading

0 comments on commit 682b5dd

Please sign in to comment.