-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first day of homework on Project Euler
- Loading branch information
Andrew Magliozzi
committed
Feb 14, 2013
1 parent
7ec334f
commit fb4a2e6
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function sum(array){ | ||
var val = 0 | ||
for (i = 0; i < array.length; i++){ | ||
val = val + array[i] | ||
} | ||
return val | ||
} | ||
|
||
|
||
function Euler3and5(highnumber){ | ||
var multiples = [] | ||
for (i = highnumber; i > 0; i--){ | ||
if (i % 3 === 0 || i % 5 === 0){ | ||
multiples.push(i) | ||
} | ||
} | ||
return multiples | ||
} | ||
|
||
sum(Euler3and5(1000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
function Euler3and5(highnumber){ | ||
var sum = 0 | ||
for (i = 0; i <= highnumber ; i++){ | ||
if (i % 3 === 0 || i % 5 === 0){ | ||
sum = sum + i | ||
} | ||
} | ||
return sum | ||
} | ||
|
||
Euler3and5(1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// write a function that takes a number and returns true if it is a multiple of 3 or 5 | ||
|
||
function Euler3or5(number){ | ||
if (number % 3 === 0 || number % 5 === 0){ | ||
return true | ||
} | ||
else | ||
return false | ||
} | ||
|
||
Euler3or5(4) | ||
|
||
|
||
// even more elegant version: | ||
|
||
function Euler3or5(number){ | ||
return (number % 3 === 0 || number % 5 === 0) | ||
} | ||
|
||
Euler3or5(7) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function evenfib(maxvalue){ | ||
var a = [1, 1] | ||
var b = 0 | ||
for (i = 1; a[i] < 4000000; i++){ | ||
b = a[i-1] + a[i] | ||
a.push(b) | ||
} | ||
var total = 0 | ||
for (i = 0; i < a.length; i++){ | ||
if (a[i] % 2 === 0) | ||
total = total + a[i]; | ||
} | ||
return total | ||
} | ||
evenfib(46) | ||
|
||
// returns 4,613,732 | ||
|
||
|
||
// alternate method done with Daf below... | ||
|
||
function evenfib(limit){ | ||
var a = 1 | ||
var b = 1 | ||
var discard = 0 | ||
var total = 1 | ||
while(b < limit){ | ||
discard = a | ||
a = b | ||
b = a + discard | ||
if (b % 2 === 0){ | ||
total = total + b | ||
} | ||
} | ||
return total | ||
} | ||
evenfib(4000000) | ||
|
||
// returns 4,613,732 |