Sunday, July 19, 2015

Day 119: Euler Problem 3

Yesterday, I worked on functions and then went back to Euler Problem 3.  Today I'm back to working on solving that problem.  Solved 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);


The code above makes a prompt, you enter the number, and then it returns the prime factors, starting with the highest.  The problem is that it takes a LONG time to do so, but it works.  I looked at three other student solutions, two did not work properly, but the third works fantastic, so I'm posting it here, it's 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);


I'm going to spend tomorrow studying this solution to understand it's simplicity.
 
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:2
Total Hours Coding: 566

No comments:

Post a Comment