forked from MUSA611-CPLN692-spring2019/cpln692-week3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart4-project-euler.html
78 lines (58 loc) · 2.75 KB
/
part4-project-euler.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
/* =====================
# Lab 1, Part 4 — Stretch Goal: Project Euler
## Introduction
Practice your Javascript and math skills by solving a problem on Project
Euler. Project Euler is a series of challenging math and computer science
problems that can be solved using any language (for our class, use
Javascript).
Solve the first problem on this page https://projecteuler.net/archives
(Multiples of 3 and 5). To check your answer, create an account and you will
be able to submit your final result.
If you complete problem 1, move on to problem 2!
===================== */
/* =====================
Start code
===================== */
//Problem #1
//If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
//Find the sum of all the multiples of 3 or 5 below 1000.
//Worked with Ian Schwarzenberg
//Code support found via: https://codereview.stackexchange.com/questions/77018/project-euler-1-javascript
var emptyArray = [];//creates an empty array
var answer = 0;//establishes sum variable for use in loop
for (var i = 1; x < 1000; x++) {//establishes look from 1 to 1000
if (i % 3 === 0 || i % 5 === 0) {//if statement selecting multimpes of 3 OR 5
emptyArray.push(i);//loop to push variables that are multiple of 3 OR 5
answer += i;//sums up the variables in the array (answer = answer + i)
}
}
console.log(answer);//geneates answer of 233168
//Problem #2
//Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
//1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
//By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
//Code support found via: https://stackoverflow.com/questions/39178973/project-euler-2-fibonacci-javascript-attemp
var fibonacci = [0,1];//Establishes 2 variable array with values of 0 and 1
var i = 0;//sets starting point of i to 0
var answer = 0;//establishes sum variable for use in loop
while (fibonacci[0]+fibonacci[1] < 4000000){//establises while loop for values up to 4,000,000
i= fibonacci[0]+fibonacci[1];//sets loop to add the two previous variables
fibonacci[0]=fibonacci[1];//establishes variable 0
fibonacci[1]=i;//establishes variable 1
if(i%2 === 0){//establishes if statement for remainder set to 0 for i=2*(i/2)
answer += i;//then returns sums up the variables in the array (answer = answer + i)
}
}
console.log(answer);//geneates answer of 4613732
/* =====================
End code
===================== */
</script>
</body>
</html>