├── README.md └── src ├── assets ├── essentials.jpg ├── javascript_2013.png ├── javascript_2014.png └── javascript_2015.png ├── hackerrank ├── README.md └── factorial │ ├── .DS_Store │ ├── non-optimized │ └── factorial.js │ └── optimized │ ├── .DS_Store │ └── factorial.js └── programs ├── language limitations ├── README.md └── factorial │ ├── README.md │ ├── brute_algo_non_optimized_javascript.js │ ├── brute_algo_optimized_javascript.js │ └── optimized_algo_optimized_javascript.js ├── memory ├── README.md └── matrix │ ├── README.md │ ├── brute_algo_non_optimized_javascript.js │ └── brute_algo_optimized_javascript.js └── speed ├── README.md └── prime number ├── README.md ├── brute_algo_non_optimized_javascript.js ├── brute_algo_optimized_javascript.js └── optimized_algo_optimized_javascript.js /README.md: -------------------------------------------------------------------------------- 1 | # Competitve Programming 2 | 3 | Competitive Programming is competing with other programmers while solving problems that require algorithm,math and clever language usage. 4 | 5 | ## Why JavaScript? 6 | 7 | JavaScript is one of the most popular programming languages in the world, but still it is one of the most ignored languages when it comes to competitive programming. This is mainly because of the fact that JavaScript lacks in certain areas that makes it unfavourable, though there are ways which can help overcome these limitations. 8 | 9 | Areas that can be improved: 10 | 11 | ### Speed (loops,array element lookup, switch cases, conditional) 12 | 13 | This includes programs like calculating 25000th prime number, which in JavaScript is around 10x slower than languages like c,c++ using the same algorithm 14 | 15 | ### Memory (large arrays, large no of JSON objects) 16 | 17 | This includes programs like storing a 1000 JSON objects which causes memory allocation failure, also note that we don't have database in competitions and everything needs to be done in-memory. 18 | 19 | ### Language Limitations (large numbers) 20 | 21 | This includes programs like calculating factorial of a large number(say 1000) while in JavaScript if you try to calculate factorial above 170 it gives you infinity because it exceeds language's number limit 22 | 23 | 24 | This repository will have examples for the above mentioned areas and implementations through which we can overcome these limitations. 25 | 26 | ### Repo Structure 27 | 28 | All the programs are grouped under speed and memory and are placed inside ````src/programs```` directory. Each program has following implementations: 29 | 30 | 1. Brute force algorithm and non optimized javascript. 31 | 2. Brute force algorithm and optimized javascript. 32 | 3. Optimized algorithm and optimized javascript. 33 | -------------------------------------------------------------------------------- /src/assets/essentials.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pranay92/Competitve-JavaScript/12aa979eff603a8eacd4b1009e59b1847c4b4473/src/assets/essentials.jpg -------------------------------------------------------------------------------- /src/assets/javascript_2013.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pranay92/Competitve-JavaScript/12aa979eff603a8eacd4b1009e59b1847c4b4473/src/assets/javascript_2013.png -------------------------------------------------------------------------------- /src/assets/javascript_2014.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pranay92/Competitve-JavaScript/12aa979eff603a8eacd4b1009e59b1847c4b4473/src/assets/javascript_2014.png -------------------------------------------------------------------------------- /src/assets/javascript_2015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pranay92/Competitve-JavaScript/12aa979eff603a8eacd4b1009e59b1847c4b4473/src/assets/javascript_2015.png -------------------------------------------------------------------------------- /src/hackerrank/README.md: -------------------------------------------------------------------------------- 1 | This directory is to fetch programs and use it on hackerrank directly during my talk. Refer the **programs** folder instead for program implementation and benchmarks. -------------------------------------------------------------------------------- /src/hackerrank/factorial/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pranay92/Competitve-JavaScript/12aa979eff603a8eacd4b1009e59b1847c4b4473/src/hackerrank/factorial/.DS_Store -------------------------------------------------------------------------------- /src/hackerrank/factorial/non-optimized/factorial.js: -------------------------------------------------------------------------------- 1 | function factorial(n) { 2 | if(n <= 1) { 3 | return 1; 4 | } 5 | 6 | return n * factorial(n-1); 7 | } 8 | 9 | 10 | console.log(factorial(process.argv[3] || process.argv[2])); -------------------------------------------------------------------------------- /src/hackerrank/factorial/optimized/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Pranay92/Competitve-JavaScript/12aa979eff603a8eacd4b1009e59b1847c4b4473/src/hackerrank/factorial/optimized/.DS_Store -------------------------------------------------------------------------------- /src/hackerrank/factorial/optimized/factorial.js: -------------------------------------------------------------------------------- 1 | function factorial(n) { 2 | 3 | var len, 4 | currNum, 5 | currProd, 6 | arr = [], 7 | mainArr = [], 8 | sum = '0', 9 | mainprod = n, 10 | finalValue, 11 | currLen, 12 | diff; 13 | 14 | while(n > 1) { 15 | 16 | currNum = (n - 1).toString(); 17 | len = currNum.length; 18 | currLen = len - 1; 19 | 20 | while(len--) { 21 | 22 | currProd = multiplyStrings(currNum[len],mainprod); 23 | diff = (currLen - len); 24 | 25 | while(diff--) { 26 | currProd += '0'; 27 | } 28 | 29 | arr.push(currProd); 30 | } 31 | 32 | len = arr.length; 33 | 34 | while(len--) { 35 | sum = addStrings(arr[len],sum); 36 | } 37 | 38 | mainprod = sum; 39 | sum = '0'; 40 | arr = []; 41 | n--; 42 | 43 | } 44 | 45 | return mainprod; 46 | } 47 | 48 | 49 | function multiplyStrings(mul,num) { 50 | 51 | var arr = [], 52 | len, 53 | currMultiple, 54 | lastRemainder = null; 55 | 56 | num = num.toString(); 57 | len = num.length; 58 | 59 | while(len--) { 60 | 61 | currMultiple = mul * num[len]; 62 | 63 | if(lastRemainder) { 64 | currMultiple += lastRemainder; 65 | lastRemainder = null; 66 | } 67 | 68 | currMultiple = currMultiple.toString(); 69 | 70 | if(currMultiple.length > 1 && len > 0) { 71 | lastRemainder = parseInt(currMultiple[0]); 72 | currMultiple = currMultiple[1]; 73 | } 74 | 75 | arr.push(currMultiple); 76 | 77 | } 78 | 79 | return arr.reverse().join(''); 80 | } 81 | 82 | function addStrings(a,b) { 83 | 84 | a = a.toString(); 85 | b = b.toString(); 86 | 87 | var aLen = a.length, 88 | bLen = b.length; 89 | 90 | if(aLen < bLen) { 91 | a = addLeadingZero(a, bLen - aLen); 92 | } 93 | 94 | if(bLen < aLen) { 95 | b = addLeadingZero(b,aLen - bLen); 96 | } 97 | 98 | var commonLen = a.length, 99 | mainSum = '', 100 | currentSum, 101 | lastRemainder; 102 | 103 | while(commonLen--) { 104 | currentSum = parseInt(a[commonLen]) + parseInt(b[commonLen]); 105 | if(lastRemainder) { 106 | currentSum += lastRemainder; 107 | lastRemainder = null 108 | } 109 | 110 | if(currentSum.toString().length > 1 && commonLen >= 1) { 111 | lastRemainder = parseInt(currentSum.toString()[0]); 112 | currentSum = currentSum.toString()[1]; 113 | } 114 | 115 | if(commonLen == 0) { 116 | currentSum = reverse(currentSum); 117 | } 118 | mainSum += currentSum; 119 | } 120 | 121 | mainSum = reverse(mainSum); 122 | return mainSum; 123 | } 124 | 125 | function reverse(s) { 126 | s = s.toString(); 127 | var o = ''; 128 | for (var i = s.length - 1; i >= 0; i--) { 129 | o += s[i]; 130 | } 131 | return o; 132 | } 133 | 134 | function addLeadingZero(num,len) { 135 | while(len--) { 136 | num = '0' + num; 137 | } 138 | return num; 139 | } 140 | 141 | 142 | console.log(factorial(process.argv[3] || process.argv[2])); 143 | -------------------------------------------------------------------------------- /src/programs/language limitations/README.md: -------------------------------------------------------------------------------- 1 | This modules contains programs that require something else apart from speed/memory 2 | 3 | ## Tips 4 | 5 | 1. Use strings to represent large numbers that would represent in infinity otherwise. -------------------------------------------------------------------------------- /src/programs/language limitations/factorial/README.md: -------------------------------------------------------------------------------- 1 | ## Calculating Factorial 2 | 3 | This module covers up calculating factorial of a number, for comparison we'll use 171 for all examples. There will be 3 implementations of this: 4 | 5 | #### 1.Brute Force with Non-Optimized JS 6 | 7 | This fails for any number above 170 since its above the JS number limit. 8 | 9 | #### 2.Brute Force with Optimized JS 10 | 11 | This succeeds but nothing done for optimizing speed. 12 | 13 | Execution time: 14 | 15 | ```` 16 | real 0m0.222s 17 | user 0m0.226s 18 | sys 0m0.004s 19 | ```` 20 | 21 | 22 | #### 3.Optimized Algorithm with Optimized JS 23 | 24 | This executes faster and shows correct value as well. 25 | 26 | Execution time: 27 | 28 | ```` 29 | real 0m0.213s 30 | user 0m0.194s 31 | sys 0m0.028s 32 | ```` 33 | 34 | A very little improvement on speed but I'm still working on getting this faster. Also notice that this is more of a memory problem than speed. 35 | 36 | -------------------------------------------------------------------------------- /src/programs/language limitations/factorial/brute_algo_non_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | function factorial(n) { 2 | if(n <= 1) { 3 | return 1; 4 | } 5 | 6 | return n * factorial(n-1); 7 | } 8 | 9 | 10 | console.log(factorial(10)); 11 | 12 | // factorial of any number above 170 gives infinity in JS 13 | console.log(factorial(171)); -------------------------------------------------------------------------------- /src/programs/language limitations/factorial/brute_algo_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | function factorial(n) { 2 | 3 | var len, 4 | currNum, 5 | currProd, 6 | arr = [], 7 | mainArr = [], 8 | sum = '0', 9 | mainprod = n, 10 | finalValue, 11 | currLen, 12 | diff; 13 | 14 | while(n > 1) { 15 | 16 | currNum = (n - 1).toString(); 17 | len = currNum.length; 18 | currLen = len - 1; 19 | 20 | while(len--) { 21 | 22 | currProd = multiplyStrings(currNum[len],mainprod); 23 | diff = (currLen - len); 24 | 25 | while(diff--) { 26 | currProd += '0'; 27 | } 28 | 29 | arr.push(currProd); 30 | } 31 | 32 | len = arr.length; 33 | 34 | while(len--) { 35 | sum = addStrings(arr[len],sum); 36 | } 37 | 38 | mainprod = sum; 39 | sum = '0'; 40 | arr = []; 41 | n--; 42 | 43 | } 44 | 45 | return mainprod; 46 | } 47 | 48 | 49 | function multiplyStrings(mul,num) { 50 | 51 | var arr = [], 52 | len, 53 | currMultiple, 54 | lastRemainder = null; 55 | 56 | num = num.toString(); 57 | len = num.length; 58 | 59 | while(len--) { 60 | 61 | currMultiple = mul * num[len]; 62 | 63 | if(lastRemainder) { 64 | currMultiple += lastRemainder; 65 | lastRemainder = null; 66 | } 67 | 68 | currMultiple = currMultiple.toString(); 69 | 70 | if(currMultiple.length > 1 && len > 0) { 71 | lastRemainder = parseInt(currMultiple[0]); 72 | currMultiple = currMultiple[1]; 73 | } 74 | 75 | arr.push(currMultiple); 76 | 77 | } 78 | 79 | return arr.reverse().join(''); 80 | } 81 | 82 | function addStrings(a,b) { 83 | 84 | a = a.toString(); 85 | b = b.toString(); 86 | 87 | var aLen = a.length, 88 | bLen = b.length; 89 | 90 | if(aLen < bLen) { 91 | a = addLeadingZero(a, bLen - aLen); 92 | } 93 | 94 | if(bLen < aLen) { 95 | b = addLeadingZero(b,aLen - bLen); 96 | } 97 | 98 | var commonLen = a.length, 99 | mainSum = '', 100 | currentSum, 101 | lastRemainder; 102 | 103 | while(commonLen--) { 104 | currentSum = parseInt(a[commonLen]) + parseInt(b[commonLen]); 105 | if(lastRemainder) { 106 | currentSum += lastRemainder; 107 | lastRemainder = null 108 | } 109 | 110 | if(currentSum.toString().length > 1 && commonLen >= 1) { 111 | lastRemainder = parseInt(currentSum.toString()[0]); 112 | currentSum = currentSum.toString()[1]; 113 | } 114 | 115 | if(commonLen == 0) { 116 | currentSum = reverse(currentSum); 117 | } 118 | mainSum += currentSum; 119 | } 120 | 121 | mainSum = reverse(mainSum); 122 | return mainSum; 123 | } 124 | 125 | function reverse(s) { 126 | s = s.toString(); 127 | var o = ''; 128 | for (var i = s.length - 1; i >= 0; i--) { 129 | o += s[i]; 130 | } 131 | return o; 132 | } 133 | 134 | function addLeadingZero(num,len) { 135 | while(len--) { 136 | num = '0' + num; 137 | } 138 | return num; 139 | } 140 | 141 | console.log(factorial(10)); 142 | 143 | // this does not gives infinity now :) 144 | console.log(factorial(171)); -------------------------------------------------------------------------------- /src/programs/language limitations/factorial/optimized_algo_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | /* 2 | To calculate factorial of an even number 3 | 4 | For even numbers: 5 | let n = 8 6 | n! = n + ((n + n-2) = k) * ((k + n - 4) = l) * ((l + n - 6 ) = m)... while (n-val > 2) 7 | 8! = 8 + ((8 + 8-2) = 14) * ((14 + 8 - 4) = 18) * ((18 + 8 - 6) = 20) 8 | = 8 * 14 * 18 * 20 9 | 10 | For odd numbers: 11 | n! = (n - 1)! * n where (n - 1) will be an even number and the above algorithm can be used again 12 | */ 13 | 14 | function factorial(n) { 15 | 16 | var isOdd = false; 17 | 18 | if(n % 2 !== 0) { 19 | isOdd = true; 20 | n -= 1; 21 | } 22 | 23 | var index, 24 | limit = n - 2, 25 | currNum = (n + limit), 26 | array = [currNum], 27 | len, 28 | currLen, 29 | currProd, 30 | arr = [], 31 | diff, 32 | sum = '0', 33 | mainprod = n, 34 | mainlen, 35 | numlen; 36 | 37 | while(limit > 2) { 38 | limit -= 2; 39 | currNum = (currNum + limit); 40 | array.push(currNum); 41 | } 42 | 43 | if(isOdd) { 44 | array.push(n + 1); 45 | } 46 | 47 | len = array.length; 48 | 49 | while(len--) { 50 | 51 | currNum = array[len].toString(); 52 | numlen = currNum.length; 53 | currLen = numlen - 1; 54 | 55 | while(numlen--) { 56 | 57 | currProd = multiplyStrings(currNum[numlen],mainprod); 58 | diff = (currLen - numlen); 59 | 60 | while(diff--) { 61 | currProd += '0'; 62 | } 63 | 64 | arr.push(currProd); 65 | } 66 | 67 | mainlen = arr.length; 68 | 69 | while(mainlen--) { 70 | sum = addStrings(arr[mainlen],sum); 71 | } 72 | 73 | mainprod = sum; 74 | sum = '0'; 75 | arr = []; 76 | 77 | } 78 | 79 | return mainprod; 80 | 81 | 82 | } 83 | 84 | function multiplyStrings(mul,num) { 85 | 86 | var arr = [], 87 | len, 88 | currMultiple, 89 | lastRemainder = null; 90 | 91 | num = num.toString(); 92 | len = num.length; 93 | 94 | while(len--) { 95 | 96 | currMultiple = mul * num[len]; 97 | 98 | if(lastRemainder) { 99 | currMultiple += lastRemainder; 100 | lastRemainder = null; 101 | } 102 | 103 | currMultiple = currMultiple.toString(); 104 | 105 | if(currMultiple.length > 1 && len > 0) { 106 | lastRemainder = parseInt(currMultiple[0]); 107 | currMultiple = currMultiple[1]; 108 | } 109 | 110 | arr.push(currMultiple); 111 | 112 | } 113 | 114 | return arr.reverse().join(''); 115 | } 116 | 117 | function addStrings(a,b) { 118 | 119 | a = a.toString(); 120 | b = b.toString(); 121 | 122 | var aLen = a.length, 123 | bLen = b.length; 124 | 125 | if(aLen < bLen) { 126 | a = addLeadingZero(a, bLen - aLen); 127 | } 128 | 129 | if(bLen < aLen) { 130 | b = addLeadingZero(b,aLen - bLen); 131 | } 132 | 133 | var commonLen = a.length, 134 | mainSum = '', 135 | currentSum, 136 | lastRemainder; 137 | 138 | while(commonLen--) { 139 | currentSum = parseInt(a[commonLen]) + parseInt(b[commonLen]); 140 | if(lastRemainder) { 141 | currentSum += lastRemainder; 142 | lastRemainder = null 143 | } 144 | 145 | if(currentSum.toString().length > 1 && commonLen >= 1) { 146 | lastRemainder = parseInt(currentSum.toString()[0]); 147 | currentSum = currentSum.toString()[1]; 148 | } 149 | 150 | if(commonLen == 0) { 151 | currentSum = reverse(currentSum); 152 | } 153 | mainSum += currentSum; 154 | } 155 | 156 | mainSum = reverse(mainSum); 157 | return mainSum; 158 | } 159 | 160 | function reverse(s) { 161 | s = s.toString(); 162 | var o = ''; 163 | for (var i = s.length - 1; i >= 0; i--) { 164 | o += s[i]; 165 | } 166 | return o; 167 | } 168 | 169 | function addLeadingZero(num,len) { 170 | while(len--) { 171 | num = '0' + num; 172 | } 173 | return num; 174 | } 175 | 176 | console.log(factorial(10)); 177 | 178 | // this does not gives infinity now :) 179 | console.log(factorial(171)); 180 | -------------------------------------------------------------------------------- /src/programs/memory/README.md: -------------------------------------------------------------------------------- 1 | This modules covers programs that require efficient memory usage. 2 | 3 | ## Tips for efficient memory usage. 4 | 5 | ### General 6 | 7 | 1. Prefer storing primitives instead of objects. 8 | 2. Delete objects whenever possible if any doubt of memory leaks. 9 | 10 | 11 | ### Arrays 12 | 13 | 1. Avoid using nested arrays, instead represent them as as array of primitives. 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/programs/memory/matrix/README.md: -------------------------------------------------------------------------------- 1 | ## Matrix 2 | 3 | This module covers storing values in a matrix (or a 2d array). For comparison we'll be creating a 6000 * 6000 matrix which is large enough to cause a memory allocation failed error. 4 | 5 | #### 1. Brute Force with Non-Optimized JS 6 | 7 | This fails and gives the following error: 8 | 9 | ````FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory```` 10 | 11 | #### 2. Brute Force with Optimized JS 12 | 13 | #### 3. Optimized Algorithm with Optimized JS 14 | -------------------------------------------------------------------------------- /src/programs/memory/matrix/brute_algo_non_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | 2 | function matrix() { 3 | 4 | var arr = [], 5 | i = 0, 6 | str = '', 7 | j; 8 | 9 | while(i < 6000) { 10 | 11 | j = 0; 12 | arr[i] = []; 13 | 14 | while(j < 6000) { 15 | 16 | arr[i]['push']({'value' : j,'checked' : false}); 17 | j++; 18 | 19 | } 20 | 21 | i++; 22 | } 23 | 24 | console.log('Done processing array ' + arr.length); 25 | // console.log(arr); 26 | } 27 | 28 | matrix(); -------------------------------------------------------------------------------- /src/programs/memory/matrix/brute_algo_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | 2 | function matrix() { 3 | 4 | var arr = '', 5 | i = 0, 6 | str = '', 7 | j; 8 | 9 | while(i < 6000) { 10 | 11 | j = 0; 12 | str = ''; 13 | 14 | while(j < 6000) { 15 | 16 | str += 'value:' + j + ',' + 'checked:' + false + '@'; 17 | j++; 18 | 19 | } 20 | 21 | arr += str + '#'; 22 | 23 | i++; 24 | } 25 | 26 | console.log('Done processing array'); 27 | 28 | } 29 | 30 | matrix(); 31 | 32 | -------------------------------------------------------------------------------- /src/programs/speed/README.md: -------------------------------------------------------------------------------- 1 | This module covers common problems that requires faster execution. 2 | 3 | ## Tips to optimize speed 4 | 5 | ### General 6 | 7 | 1. Initialize values whereever and whenever possible. 8 | 2. Avoid using try..catch whenever possible, if still needed wrap the code inside try block into a function and just call the function inside the try block. 9 | 3. Avoid changing function signature at run time. 10 | 11 | ### Arrays 12 | 13 | 1. Do not use sparse arrays. 14 | 2. Always have homogeneous arrays. 15 | 3. Avoid array of objects, prefer using 31 bit signed integers. Storing even a single double type value, cause re allocation of buffer memory and then converting each value in the array to be a double type. 16 | 4. If required to store heterogeneous values, use array literals and then add all the values in declaration instead of using a for loop (if possible) 17 | 18 | ### Functions 19 | 20 | 1) Use monomorphic functions 21 | 22 | ```` 23 | function Rectangle(length,breadth) { 24 | this.length = length; 25 | this.breadth = breadth; 26 | } 27 | 28 | var rect = new Rectangle(10,5); 29 | 30 | rect.area = 2 * (rect.length + rect.breadth); // this is BAD 31 | ```` 32 | 33 | 2) Use prototypes 34 | 35 | ## Resources 36 | 37 | 1. http://jsperf.com/loops 38 | 39 | -------------------------------------------------------------------------------- /src/programs/speed/prime number/README.md: -------------------------------------------------------------------------------- 1 | ## Calculating Prime Number 2 | 3 | This module covers calculating nth prime number. For demonstration n will be 25000 for all implementations. 4 | 5 | #### 1. Brute Force with Non-Optimized JS 6 | 7 | Takes too much time as compared to languages like c and c++. 8 | 9 | Execution time: 10 | 11 | ```` 12 | real 0m15.481s 13 | user 0m15.539s 14 | sys 0m0.040s 15 | ```` 16 | 17 | 18 | #### 2. Brute Force with Optimized JS 19 | 20 | Better performance but still slower than c/c++. 21 | 22 | Execution time: 23 | 24 | ```` 25 | real 0m1.591s 26 | user 0m1.591s 27 | sys 0m0.012s 28 | ```` 29 | 30 | 31 | #### 3. Optimized Algorithm with Optimized JS 32 | 33 | Almost matches c/c++ execution time. 34 | 35 | Execution time: 36 | 37 | ```` 38 | real 0m0.138s 39 | user 0m0.136s 40 | sys 0m0.004s 41 | ```` 42 | 43 | Resources: 44 | 45 | 1. https://www.youtube.com/watch?v=UJPdhx5zTaw 46 | 47 | -------------------------------------------------------------------------------- /src/programs/speed/prime number/brute_algo_non_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | function Prime(n) { 2 | 3 | var primes = [2,3,5,7], 4 | start = 8, 5 | index = 5; 6 | 7 | while(index <= n) { 8 | if(isPrime(start)) { 9 | primes.push(start); 10 | index++; 11 | } 12 | start++; 13 | } 14 | 15 | return primes[primes.length - 1]; 16 | 17 | } 18 | 19 | function isPrime(num) { 20 | 21 | var index = 2, 22 | isValid = true, 23 | limit = (num - 1); 24 | 25 | while(index <= limit) { 26 | if(num % index == 0) { 27 | isValid = false; 28 | break; 29 | } 30 | index++; 31 | } 32 | 33 | return isValid; 34 | } 35 | 36 | 37 | 38 | console.log(Prime(25000)); -------------------------------------------------------------------------------- /src/programs/speed/prime number/brute_algo_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | function Prime(n) { 2 | 3 | var primes = [2,3,5,7], 4 | start = 8, 5 | index = 5; 6 | 7 | n += 1; 8 | 9 | while(index < n) { 10 | if(isPrime(start,primes)) { 11 | primes.push(start); 12 | index++; 13 | } 14 | start++; 15 | } 16 | 17 | return primes[primes.length - 1]; 18 | 19 | } 20 | 21 | function isPrime(num,primes) { 22 | 23 | var index = 0, 24 | len = primes.length, 25 | isValid = true; 26 | 27 | while(index < len) { 28 | if(num % primes[index] == 0) { 29 | isValid = false; 30 | break; 31 | } 32 | index++; 33 | } 34 | 35 | return isValid; 36 | } 37 | 38 | 39 | 40 | console.log(Prime(25000)); -------------------------------------------------------------------------------- /src/programs/speed/prime number/optimized_algo_optimized_javascript.js: -------------------------------------------------------------------------------- 1 | function Prime(n) { 2 | 3 | var primes = [2,3,5,7], 4 | start = 8, 5 | index = 5; 6 | 7 | n += 1; 8 | 9 | while(index < n) { 10 | if(isPrime(start)) { 11 | primes.push(start); 12 | index++; 13 | } 14 | start++; 15 | } 16 | 17 | return primes[primes.length - 1]; 18 | 19 | } 20 | 21 | function isPrime(num) { 22 | 23 | var index = 2, 24 | isValid = true, 25 | sqrt = Math.ceil(Math.pow(num,1/2)); 26 | 27 | sqrt += 1; 28 | 29 | while(index < sqrt) { 30 | 31 | if(num % index == 0) { 32 | isValid = false; 33 | break; 34 | } 35 | 36 | index++; 37 | } 38 | 39 | return isValid; 40 | } 41 | 42 | 43 | 44 | console.log(Prime(25000)); --------------------------------------------------------------------------------