Calculating Primes

Today I am going to talk about calculating primes.

Not the new Transformer movie in which Optimus Prime has to help the humans save the world.
I am talking about prime numbers in which the number is only divisable by one and itself.

The Algorithm that I am going to introduce is a brute force method for calculating prime numbers.
It is great for comparing the computing power of two machines by looking at overall execution times.

This routine consists in dividing n by each integer m which is greater than 1 and less than or equal
to the square root of n. If the result of any of these divisions is an integer, then n is not a prime;
otherwise, it is a prime.

I am going to use both the “Application Logging” and “Write Text File” classes that were discussed earlier to solve this problem.

I always like to create a high level program algorithm before starting any coding. I usually write it in psuedo code so that is program like in nature.

Calculating Primes Algorithm:


The table below has the Visual Basic (VB) Scripts used in the solution and the output files. I ran the program to calculate all the prime numbers less than or equal to 2.5 Million. The program executed in 91 seconds on a Dell i5 Laptop running Windows 7 – 32 bit operating system and found 183,072 prime numbers.

mod-write-text-file.vbs Write Text File Class
mod-app-logging.vbs Application Logging Class
prg-calc-primes-v1.vbs Calculate Primes Program
primes.cmd Primes Batch File
prg-calc-primes-v1.log Primes Log File
primes Primes Result File





This is a screen shot of the calculating primes VB Script excuting in a MS DOS command shell.






As you can see, the second time I ran the program it took 95 seconds. Just to show you that background processes like email, word, and etc. can have sligth performance impacts of heavy computational programs.

Related posts

One Thought to “Calculating Primes”

  1. During my college years I never saw work as good as this. You display a lot of intelligence and detail within this article and I hope you continue to write these. This is very good.

Leave a Comment