Skip to content

Commit b702029

Browse files
author
Gonzalo Diaz
committed
First solutions found in ES6
0 parents  commit b702029

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

problem0001.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 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.
3+
* Find the sum of all the multiples of 3 or 5 below 1000.
4+
*/
5+
6+
let total = 0;
7+
let top = 1000;
8+
9+
for (let i= 0; i < top; i++)
10+
{
11+
if(i % 3 == 0 || i % 5 ==0)
12+
{
13+
console.log(`i = ${i}`);
14+
total = total+i;
15+
}
16+
}
17+
18+
console.log(`TOTAL = ${total}`);

problem0002.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// 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:
2+
3+
// 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
4+
5+
// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
6+
7+
let i = 0;
8+
let fibo = 0;
9+
let last1 = 1;
10+
let last2 = 0;
11+
let evenSum = 0;
12+
do {
13+
14+
// last2 + last1 = fibo
15+
fibo = last2 + last1;
16+
console.log(`${i}: ${last2} + ${last1} = ${fibo}`)
17+
18+
if(fibo % 2 === 0)
19+
{
20+
evenSum = evenSum + fibo;
21+
}
22+
23+
// next keys:
24+
last2 = last1;
25+
last1 = fibo;
26+
27+
i++;
28+
} while ( fibo < 4000000 )
29+
30+
console.log(`RESULT = ${evenSum}`)

problem0003.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// problem0003.js
2+
3+
// The prime factors of 13195 are 5, 7, 13 and 29.
4+
5+
// What is the largest prime factor of the number 600851475143 ?
6+
7+
8+
//////
9+
10+
// divisors calculated = 71,839,1471,6857,59569,104441,486847,1234169,5753023,10086647,87625999,408464633,716151937,8462696833
11+
12+
//////
13+
14+
let target = 600851475143
15+
let top = target
16+
let i;
17+
18+
let n = 100;
19+
20+
// 1 => 10 / 1 => 10
21+
// 2 => 10 / 2 => 5
22+
// 3 => 10 / 3 => X
23+
// 3 => 10 / 4 => X
24+
25+
let divs = [];
26+
27+
// fast divisors finding loop
28+
for (i=1; i < top; i++)
29+
{
30+
if(target % i === 0 && i != 1 && i != target) {
31+
32+
divs.push(i);
33+
divs.push(target / i)
34+
35+
top = target / i;
36+
console.log(`adding ${i}`)
37+
}
38+
}
39+
40+
// sort divisors
41+
divs.sort((a, b) => a - b);
42+
// print all divisors
43+
console.log(`All divisors of ${target}: ${divs}`)
44+
45+
// Prime checker
46+
function isPrime(n) {
47+
for(let i=2; i < n; i++) {
48+
if (n % i == 0) {
49+
return false;
50+
}
51+
}
52+
53+
return true;
54+
}
55+
56+
let middle;
57+
if (divs.length % 2 === 0)
58+
{
59+
middle = (divs.length/2) -1;
60+
} else {
61+
middle = (divs.length) - 1;
62+
}
63+
64+
// check half divisors, each is Prime? wich is largest?
65+
let maxPrimeFactor;
66+
67+
i = middle;
68+
do
69+
{
70+
let prime = isPrime(divs[i]);
71+
72+
console.log(`${divs[i]} is Prime? ${prime}`)
73+
74+
if(prime)
75+
maxPrimeFactor = divs[i];
76+
i--
77+
} while(i >= 0 && !maxPrimeFactor)
78+
79+
console.log(`${maxPrimeFactor}`)

0 commit comments

Comments
 (0)