Skip to content

Commit

Permalink
commented 1-3
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas4g committed Mar 11, 2012
1 parent d83716c commit 0091a8d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions problem1.js
@@ -1,3 +1,9 @@
//OBJECTIVE:
//Find the sum of all the multiples
//of the given values below the limit

//TODO: rewrite using recursion awesomeness

function solve(multiples, limit) {
var i=0, sum=0, j;
for(;i<limit;i++) {
Expand Down
7 changes: 6 additions & 1 deletion problem2.js
@@ -1,5 +1,10 @@
//OBJECTIVE:
//Find the sum of the even-valued
//Fibbonacci terms below the given limit
//This is pretty straighforward, so I'm
//not commenting it. :)
function solve(limit) {
var sum=2,i=1,j=fib=2, k=0;
var sum=2,i=1,j=fib=2;
while(fib<limit) {
fib=i+j;
i=j;
Expand Down
29 changes: 26 additions & 3 deletions problem3.js
@@ -1,14 +1,37 @@
// factor with recursion
// time to solve < .5s
//OBJECTIVE:
//Find the largest prime factor of the number "num"
function factor(num, index) {
//if the index is null
//(because it's our first run on this number)
//set it to 2
if(!index) index = 2;
//the important one here is result=num
//i.e, the function returns num by default
//if no prime factors are found
var lim = Math.ceil(num/2)+1, result=num;
//if we're still iterating, basically...
if(index < lim) {
//if index is a factor at all
if(num%index === 0) {
//then find its greatest prime factor
//recursion!
//since the factor function returns the
//num passed by default if no prime
//factors are found, we'll eventually
//reach a point where x === factor(x)
//at that point, we've found the
//greatest prime factor, so return it
return factor(num/index);
}
result = factor(num, index+1);
//if we're still here, keep going
//i.e, set the result to either
//the value found in the preceding return
//statement, or to the default value (num)
//oh, and increment index so we actually keep
//going. :)
result = factor(num, ++index);
}
//yay, we're done! :)
return result;
}

Expand Down

0 comments on commit 0091a8d

Please sign in to comment.