-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSum_All_Primes.js
More file actions
139 lines (109 loc) · 3.43 KB
/
Copy pathSum_All_Primes.js
File metadata and controls
139 lines (109 loc) · 3.43 KB
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
// My solution for https://www.freecodecamp.com/challenges/sum-all-primes
function sumPrimes(num) {
if (num <= 1) return 0;
if (num === 2) return 2;
var primes = [2];
var total = 2;
for (var i=3; i <= num; i += 2) {
if (isPrime(i)) {
total += i;
primes.push(i);
}
}
return total;
function isPrime(p) {
if (p === 2 || p === 3 || p === 5 || p === 7) return true;
if (p % 2 === 0 || p % 3 === 0 || p % 5 === 0 || p % 7 === 0) return false;
// the above code eliminates a large set of numbers.
var sqrt = Math.ceil(Math.sqrt(p));
for (var j = 0; j < primes.length && primes[j] <= sqrt; j++) {
if (p % primes[j]===0) return false;
}
return true;
}
}
console.time();
console.log(sumPrimes(500000));
console.timeEnd();
// default: 40.899ms
/* Notes: (In no order)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
// added above because one of my earlier solutions I was going to try using this and found the code sample for primes
http://www.stoimen.com/blog/2012/05/08/computer-algorithms-determine-if-a-number-is-prime/
http://stackoverflow.com/questions/1032427/efficient-storage-of-prime-numbers
http://stackoverflow.com/questions/1801391/what-is-the-best-algorithm-for-checking-if-a-number-is-prime
https://www.topcoder.com/community/data-science/data-science-tutorials/mathematics-for-topcoders/
https://en.wikipedia.org/wiki/Primality_test
https://en.wikipedia.org/wiki/AKS_primality_test
Sieve of Eratosthenes has complexity O(n * (log n) * (log log n)) and requires O(n) memory
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithm_complexity_and_implementation
https://en.wikipedia.org/wiki/Sieve_of_Atkin
*/
/* My first solution is below:
function sumPrimes(num) {
var primes = [2,3,5,7];
var total = 17;
function isPrime(num) {
for (var i = 3; i < primes.length; i++) {
if (num % primes[i] === 0) {
return false;
}
}
return true;
}
for (var i = 3; i <= num; (i = i + 2) ) {
if (i % 2 === 0 || i % 3 === 0 || i % 5 === 0 || i % 7 === 0) {
continue; // code to shortcut the function call.
} else {
if (isPrime(i)) {
total += i;
primes.push(i);
}
}
}
return total;
}
console.time();
sumPrimes(500000);
console.timeEnd();
// default: 3491.986ms
*/
/*
I'm putting this down here.
I just want to keep it as a personal record
of some of the silly stuff I tried that didn't work.
var maxCalculated = 8, primes = [[2,2] , [3,5] , [5,10] , [7,17]];
function sumPrimes(num) {
if (num > maxCalculated) {
maxCalculated = num;
} else {
let index = primes.findIndex(function (el, index, array) { return el[0] > num; });
return primes[index-1][1];
}
}
function isPrime(num) {
if (num < 2 || num % 2 === 0 || num % 3 === 0 || num % 5 === 0 || num % 7 === 0) {
return false;
}
for (var i = 4; i < primes.length; i++) {
if (primes[i][0] % num === 0) {
return false;
}
}
return true;
}
sumPrimes(10);
var primes = [];
function sumPrimes(num) {
if (num > primes[primes.length-1]) {
return "If true stuff";
} else {
var index = primes.findIndex(function (el, index, array) {
return el > num;
});
return primes[index-1];
}
}
sumPrimes(7);
*/