Visual Studio Code
Visual Studio Code (also called VS Code) is one of the most popular free IDEs, published by Microsoft. We can say it is a free version of Visual Studio. It can be used with MinGW Linux Simulation (with GNU C/C++ Compiler) to develop C++ applications running on the command console. It is the most well-known open-source code editor for a wide variety of languages, and it can act as an IDE with the right extensions. This developer-environment tool also offers multiplatform support and is excellent for developers seeking customization and a high degree of flexibility. However, Visual Studio Code is built in Electron and can consumer more resources than other native IDEs.
Prime numbers are interesting area to research. A prime number, it is also called prime shortly , is a natural number (a positive integer) greater than 1 that is not a product of two smaller natural numbers. If a number is not prime then it is called as a composed number. There are several mathematical questions regarding prime numbers are still unsolved. Finding them and relations and their effects on some other functions, graphics really interesting in mathematical phenomena.
Counting Prime Numbers
Here is a VS Code example with GCC Compiler to find number of prime numbers in a given range. We are also testing calculation time five times.
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 |
#include <stdio.h> #include <ostream> #include <math.h> #include <time.h> using namespace std; #define max_n 1000000 //--------------------------------------------------------------------------- bool is_prime(unsigned int x) { if(x<=1) return(false); unsigned int q = floor(sqrt((float)x)); for(unsigned int i=2; i<=q; i++) { if((x%i)==0) return(false); } return(true); } //--------------------------------------------------------------------------- int count_primes(unsigned int maxN) { unsigned int res=0; for(int i=2; i<=maxN; i++) { if(is_prime(i)) res++; } return(res); } //--------------------------------------------------------------------------- int main() { unsigned int count; clock_t start,end; for(int k=0; k<5; k++) { start = clock(); count = count_primes(max_n); end = clock(); cout << "Number of primes between 0 to " << max_n << "= " << count << endl; cout << "Elapsed time:" << end-start << " ms" ; } return(0); } |
Here is the screenshot of codes with the outputs from the terminal