Monday, July 20, 2015

Day 120: Reviewing Euler Problem Solution(s) and Uploading to Github

So, as noted yesterday, here was my solution to Euler Problem 3:

function maxP(a) {
var b = a - 1;
var IsPrime = true;
while (b !== 1) {
  if (a % b === 0)
    IsPrime = false;
    b = b - 1;
}
if (IsPrime === true) {
  console.log("Prime");
  console.log(a);
 }
}

function euler3(x) {
  for (i = x; i > 1; i--) {
    if (x % i === 0 && maxP(i) === true) {
    }
  }
}

var stringNumber = prompt("Give me a number, and I will give you the prime factors, starting with the largest!");
var parsedNumber = parseInt(stringNumber);
var x = parsedNumber;

euler3(x);


Then, here's a really neat solution by a user named Marina Drigo:

function findPrime(input){
    var prime = 0;
    var x = input;
    var div = 2;

    while (x > 1)
    {
        while (x % div === 0){
            prime = x;
            x = x / div;
        }

        div++;
    }
    return prime;
};

var input = 600851475143;
var result = findPrime(input);
console.log(result);


That code works amazingly well, and I want to understand why...

O.k., so, I started today with looking at my code for the problem and making it run in reverse compared to how I had it, which is to say, I made it run from 1 to the required number, instead of from the required number down to 1.  This had the effect of getting to the prime factors much faster (because the prime factors for a number are clustered closer to 0 than they are to the number itself).  I also capped the resulting prime factors at numbers below 10,000.  So the code will give all the prime factors of a number, as long as the prime factors are below 10,000.  Here's the improved code:

function maxP(a) {
var b = a - 1;
var IsPrime = true;
while (b !== 1) {
  if (a % b === 0)
    IsPrime = false;
    b = b - 1;
}
if (IsPrime === true) {
  console.log("Prime");
  console.log(a);
 }
}

function euler3(x) {
  for (i = 2; i <= 10000; i++) {
    if (x % i === 0 && maxP(i) === true) {
    }
  }
}

var stringNumber = prompt("Give me any number, and I will give you the prime factors of that number, from smallest to largest, as long as they are below 10,000!");
var parsedNumber = parseInt(stringNumber);
var x = parsedNumber;

euler3(x);


It works great!  It actually finishes the calculation very quickly, as long as you don't enter a number that has a prime factor greater than 10,000.  :)

O.k., so now I'm gathering up my Euler Problem answer for problems 1 through 3, polishing them up, and getting them ready to be uploaded to github...

So while working with the Command Line (for Github purposes) I came across the old issue I had with the Command Line not wanting to access a folder.  I inserted a _ instead of a space between the two words of the folder name, and that worked, I was able to access the folder.  This has happened before, and I need to get into the habit of naming all my folders with a _ instead of a space.

All right, I created the readme file and pulled it down to my local folder.  I then submitted my solutions to the Odin Project, and the pull request is pending approval.  Great day so far! 

At this point, I've got to choose whether co continue with the book and finish it or continue with the next part of the Odin Project.  So far, the book has been the best book on coding I've ever gone through, it has brought a lot of value to the table, thanks to it, I was able to finally solve Euler Problem 3.  So, I'm going to continue my progress on the book, which, at a rate of 4 or 5 chapters a day, should take me about 10 more days to complete. 

O.k., so after doing the work above and loading the work to Github (and submitting a pull request), I moved on to Chapter 39 Switch Statements: How to Start Them.


SUMMARY OF CODING SKILLS

Total Treehouse Points: 5,385

Treehouse Points by Subject Matter (Miscellaneous not included): 
HTML:                                663 
CSS:                                1,599 
Design:                            1,193 
Development Tools:            747 
Javascript:                      1,120

Treehouse Ranking (%): "You have more total points than 94% of all students."

Treehouse Badge(s) Earned Today:



Treehouse Courses Completed:
How to Make a Website
HTML
CSS Foundations
CSS Layout Techniques
Aesthetic Foundations
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Git Basics
Introduction to Programming
Javascript Basics

Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy) 

Books Read or in Progress:

Completed: "Head First HTML and CSS," by E. Robson & E. Freeman
In Progress: "Eloquent JavaScript," by Marijn Haverbeke (On pg 27)
In Progress: "Head First Javascript," by Eric Freeman and Elisabeth Robson (On pg 56)
In Progress: "A Smarter Way to Learn Javascript," by Mark Myers (on pg 89)

My Progress on The Odin Project:
1.  Introduction to Web Development             100% Complete
2.  Web Development 101                                29% Complete
3.  Ruby Programming                                       0% Complete
4.  Ruby on Rails                                               0% Complete
5.  HTML5 and CSS3                                           0% Complete
6.  Javascript and JQuery                                  0% Complete
7.  Getting Hired as a Web Developer                 0% Complete

Hours Spent Coding Today: 3
Total Hours Coding: 569

No comments:

Post a Comment