-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.js
187 lines (129 loc) · 3.17 KB
/
Index.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
174
175
176
177
178
179
180
181
182
183
184
185
//Exercise 1 Multiples of 3 or 5
let sum = 0
for (let i = 0; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) {
sum += i
}
}
console.log(sum)
//Exercise 2 Even Fibonacci numbers
function evenFibSum(limit) {
if (limit < 2)
return 0;
let evenFib1 = 0, evenFib2 = 2;
let sum = evenFib1 + evenFib2;
while (evenFib2 <= limit) {
let evenFib3 = 4 * evenFib2 + evenFib1;
if (evenFib3 > limit)
break;
evenFib1 = evenFib2;
evenFib2 = evenFib3;
sum += evenFib2;
}
return sum;
}
let limit = 4000000;
//Exercise 3 Largest prime factor
var divisor = 2;
var number = 600851475143;
while (number > 1) {
if (number % divisor === 0) {
number /= divisor;
} else {
divisor++;
}
}
console.log(divisor);
//Exercise 4 highest palindrome of 3 digit product
let convertedNumber = n.toString()
let reverseString = convertedNumber.split('').reverse().join('')
return reverseString === convertedNumber ? true : false
while (num1 < 1000) {
for (let i = num2; i < 1000; i++) {
let prod = num1 * i
if (checkPalindrome(prod)) {
palindromes.push(prod)
nums.push([num1, i])
}
}
num1++
}
let palindromes2 = palindromes.sort(function (a, b) { return a - b; })
console.log(palindromes2[palindromes2.length - 1])
console.log(nums[nums.length - 1])
//Exercise 5, get lowest number divisible by 1-20
function gcd(a, b) {
if (a % b != 0)
return gcd(b, a % b);
else
return b;
}
function lcm(n) {
let ans = 1;
for (let i = 1; i <= n; i++)
ans = (ans * i) / (gcd(ans, i));
return ans;
}
console.log(lcm(20))
//Exercise 6 Sum square difference
const sumOfSquares = n => {
let result = 0
for (let i = 1; i <= n; i++) {
result += i ** 2
}
return result
}
const squareOfSums = n => {
let result2 = 0
for (let i = 1; i <= n; i++) {
result2 += i
}
let resultSquared = result2 ** 2
return resultSquared
}
const finalResult = n => {
return squareOfSums(n) - sumOfSquares(n)
}
console.log(finalResult(100))
//Exercise 7 10001st prime
let arrOfPrimes = []
const isPrimeNumber = n => {
for (let i = 2; i < n; i++) {
if (n % i === 0) return false;
}
return n > 1;
}
const getPrimes = () => {
for (let i = 2; arrOfPrimes.length < 10001; i++) {
isPrimeNumber(i) && arrOfPrimes.push(i)
}
return arrOfPrimes[arrOfPrimes.length - 1]
}
console.log(getPrimes())
//Exercise 9 find pythagorean triples which add to 1000
let a;
let c;
for (var b = 1; b < 1000; b += 1) {
a = (500000 - 1000 * b) / (1000 - b);
if (Math.floor(a) === a) {
c = 1000 - a - b;
break;
}
}
console.log(a * b * c);
//Exercise 10 sum of primes under 2000000
const isPrime = num => {
for (let i = 2, s = Math.sqrt(num); i <= s; i++)
if (num % i === 0) return false;
return num > 1;
}
const getPrimeSum = () => {
let arrOfPrimes2 = []
let acc = 0
for (let n = 2; n < 2000000; n++) {
if (isPrime(n) == true) acc += n
}
console.log(acc)
return acc
}
console.log(getPrimeSum())