Skip to content

Instantly share code, notes, and snippets.

@tebesfinwo
Last active August 29, 2015 14:15
Show Gist options
  • Save tebesfinwo/5e715cee23c8f6d356a9 to your computer and use it in GitHub Desktop.
Save tebesfinwo/5e715cee23c8f6d356a9 to your computer and use it in GitHub Desktop.
/**
@author : tebesfinwo
Link to some EULER problems :
https://docs.google.com/document/d/1URbpHu6STtlPgW3OeVUgRkJ3OnywQNujJZqYozfobEk/edit?usp=sharing
*/
'use strict';
var Ant = function(params){
this.north = 0;
this.east = 1;
this.south = 2;
this.west = 3;
this.direction = this.north;
this.size = params.size; //grid size
this.grid = [];
this.white = 0;
this.black = 1;
this.x = 9; //initial position X
this.y = 9; //initial position Y
this.moves = params.moves; //numnber of moves to make
};
Ant.prototype.init = function(){
//initialize all grid to white
for(var i = 0; i < this.size; i++){
this.grid[i] = [];
for(var j = 0; j < this.size; j++){
this.grid[i][j] = this.white;
}
}
this.move();
};
Ant.prototype.flipColor = function(){
this.grid[this.x][this.y] = (this.grid[this.x][this.y] === this.white)? this.black : this.white;
}
//move the ant based on its location and direction
Ant.prototype.position = function(){
switch(this.direction) {
case this.north:
this.y++;
break;
case this.east:
this.x++;
break;
case this.south:
this.y--;
break;
case this.west:
this.x--;
break;
}
};
//move the ant
Ant.prototype.move = function(){
var currMoves = 0;
while(currMoves < this.moves){
switch(this.direction){
case this.north :
if ( this.grid[this.x][this.y] === this.white ) {
this.direction = this.east;
}else {
this.direction = this.west;
}
break;
case this.east :
if( this.grid[this.x][this.y] === this.white ) {
this.direction = this.south;
}else{
this.direction = this.north;
}
break;
case this.south :
if ( this.grid[this.x][this.y] === this.white ) {
this.direction = this.west;
}else {
this.direction = this.east;
}
break;
case this.west :
if ( this.grid[this.x][this.y] === this.white ) {
this.direction = this.north;
}else {
this.direction = this.south;
}
break;
}
this.flipColor();
this.position();
currMoves++;
}
};
//calculate the total black grids
Ant.prototype.getTotalBlackGrids = function(){
var total = 0;
for(var i = 0; i < this.size; i++){
for(var j = 0; j < this.size; j++){
if ( this.grid[i][j] === this.black ) {
total++;
}
}
}
return total;
}
//Find number of multiples
function findMultiples(multiplier, upperBound){
var multiples = [], i;
for(i = multiplier; i < upperBound; i+=multiplier){
multiples.push(i);
}
return multiples;
}
//Find the sum of even fibnocci numbers
function findSumEvenFib(upperBound){
var total = 0,
prevNum = 1,
currNum = 2;
while( currNum < upperBound ){
if (currNum % 2 === 0)
total += currNum;
var tmp = currNum;
currNum = currNum + prevNum;
prevNum = tmp;
}
return total;
}
//Find the largest prime factor with given num
function findLargestPrimeFactor(num){
var i = 2;
while (i <= num) {
if (num % i == 0) {
num /= i;
} else {
i++;
}
}
return i;
}
//Calculate the sum of square
function findSumOfSquare(num){
var total = 0;
for(var i = 1; i <= num; i++){
total += i*i;
}
return total;
}
//Calculate the square of the sum
function findSquareOfSum(num){
var total = 0;
for(var i = 1; i <= num; i++){
total += i;
}
return total * total;
}
//Find the Pythogorean triplets
function findPythTriplet(num){
var found = false,
triplet = [];
for(var i =1; i < num/3; i++){
for(var j = i; j < num/2; j++){
var tmp = num - j - i;
if ( i*i + j*j === tmp * tmp ) {
found = true;
triplet.push(i);
triplet.push(j);
triplet.push(tmp);
break;
}
}
if (found)
break;
}
return triplet;
}
//Find every possible paths (Binomial)
function findPaths(gridSize){
var paths = 1;
for(var i = 0; i < gridSize; i++){
paths *= ( 2 * gridSize) - i;
paths /= i + 1;
}
return paths;
}
var sumThreeMultiples = findMultiples(3, 1000).reduce(function(a, b){
return a + b;
});
var sumFiveMultiples = findMultiples(5, 1000).reduce(function(a, b){
return a +b;
});
var productOfTriplet = findPythTriplet(1000).reduce(function(a,b){
return a * b;
});
var ant = new Ant({
size : 50,
moves : 1018
});
ant.init();
console.log("Black squares after 1018 moves of the ant : " + ant.getTotalBlackGrids());
console.log("Sum of all the multiples of 3 or 5 below 1000 : " + (sumThreeMultiples + sumFiveMultiples) );
console.log("sum of the even-valued terms of 4000000 : " + findSumEvenFib(4000000));
console.log("the largest prime factor of the number 600851475143 : " + findLargestPrimeFactor(600851475143));
console.log("the sum of the squares of the first one hundred natural numbers and the square of the sum : " + Math.abs( findSumOfSquare(10) - findSquareOfSum(10) ) );
console.log("The product of abc : " + productOfTriplet);
console.log("The possible paths of 20 x 20 : " + findPaths(20));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment