-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
175 lines (155 loc) · 3.48 KB
/
main.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// PROBLEM 1: DOM
var btn1 = document.getElementById("btn1");
btn1.onclick = function() {
document.getElementById("answer1").innerHTML = problem1();
};
// PROBLEM 1: FUNCTION
function problem1() {
var max = document.getElementById('input1').value;
var sum = 0;
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 10000) {
return "Please specify a number between 0-10000";
}
for(var i = 1; i< max; i++){
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}
return sum;
}
// PROBLEM 2: DOM
var btn2 = document.getElementById("btn2");
btn2.onclick = function() {
document.getElementById("answer2").innerHTML = problem2();
};
// PROBLEM 2: FUNCTION
function problem2() {
var max = document.getElementById('input2').value;
var x = 0;
var y = 1;
var sum;
var total = 0;
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 5000000) {
return "Please specify a number between 0-5000000";
}
for (sum=0; sum<max; sum=x+y) {
if (sum%2 === 0) {
total += sum;
}
x=y;
y=sum;
}
return total;
}
// PROBLEM 3: DOM
var btn3 = document.getElementById("btn3");
btn3.onclick = function() {
document.getElementById("answer3").innerHTML = problem3();
};
// PROBLEM 3: FUNCTION
function problem3() {
var max = document.getElementById('input3').value;
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 5000000) {
return "Please specify a number between 0-5000000";
}
for (var i=2; i<=max; i++) {
if (!(max % i)) {
max /= i;
i--;
}
}
return i;
}
// PROBLEM 4: DOM
var btn4 = document.getElementById("btn4");
btn4.onclick = function() {
document.getElementById("answer4").innerHTML = problem4();
};
// PROBLEM 4: FUNCTION
function problem4() {
var max = document.getElementById('input4').value;
if (max === "") {
return "Uoh-ooh, you forgot to specify both numbers!";
}
if(max > 5) {
return "Please specify a number between 0-5";
}
var i, n, m, d, inf, sup, limit, number = 0;
for (i = 1; i < max; i += 1) {
number = 10 * number + 9;
}
inf = number;
sup = 10 * number + 9;
function isPalindromic(n) {
var p = 0, q = n, r;
while (n > 0) {
r = n % 10;
p = 10 * p + r;
n = Math.floor(n / 10);
}
return p === q;
}
for (n = sup * sup, m = inf * inf; n > m; n -= 1) {
if (isPalindromic(n)) {
limit = Math.ceil(Math.sqrt(n));
d = sup;
while (d >= limit) {
if (n % d === 0 && n / d > inf) {
return n;
}
d -= 1;
}
}
}
return NaN;
}
// PROBLEM 5: DOM
var btn5 = document.getElementById("btn5");
btn5.onclick = function() {
document.getElementById("answer5").innerHTML = problem5();
};
// PROBLEM 5: FUNCTION
function problem5() {
var max = document.getElementById('input5').value;
if (max === "") {
return "Uoh-ooh, you forgot to specify a number!";
}
if(max > 100) {
return "Please specify a number between 0-100";
}
var i, n = 1;
function largestPower(n, max) {
var p, e = 2, largest = n;
while ((p = Math.pow(n, e)) <= max) {
largest = p;
e += 1;
}
return largest;
}
function isPrime(n) {
var i, max = Math.ceil(Math.sqrt(n));
// since the main loop generates odd numbers only
// we can start testing primality dividing by 3
for (i = 3; i <= max; i += 2) {
if (n % i === 0) {
return false;
}
}
return true;
}
for (i = 3; i <= max; i += 2) {
if (isPrime(i)) {
n *= largestPower(i, max);
}
}
return n * largestPower(2, max);
}