can anybody write the algorithm to display all the prime numbers between 1 and 100. Try to formulate a model for picking out the prime numbers from this range and describe the input,processing and the output intended
Using sieve of eratosthenes, you just need to check the divisibility with `2, 3, 5, 7`. Here I provide two different programs in javascript which i hope you may easily convert to an algorithm :
#Method 1 (sieve of eratosthenes) ``` for (i = 2; i < 100; i++) { if (i != 2 && i % 2 == 0) continue; if (i != 3 && i % 3 == 0) continue; if (i != 5 && i % 5 == 0) continue; if (i != 7 && i % 7 == 0) continue; console.log(i); } ``` https://jsfiddle.net/ej13c8t4/1/
#Method 2(regular expressions, this is the most inefficient algorithm but also the most coolest) ``` for (i = 2; i < 100; i++) { if ("1".repeat(i).match(/^(11+?)\1+$/)) continue; console.log(i); } ``` https://jsfiddle.net/8bwepcbs/1/
ha, that's clever @ganeshie8
Join our real-time social learning platform and learn together with your friends!