Print Prime Numbers – Most of us are already familiar with the Prime Numbers, And one of the basic arithmetic we learn in programming is to check these Prime Numbers with LOGIC. While calculating them, I’ve used the unique property of prime numbers that they have only 2 divisors, 1 and the number in question itself.
HOW TO PRINT PRIME NUMBERS in CMD? | Batch
So, I’ve made two batch files for calculations – but, the algorithm by which they are calculating prime numbers is slightly different. Due to this reason, one is calculating them faster than other.
Calculate Prime Numbers – Slower Algorithm:
This Batch file is simply working blindly on the same fact that a prime number has only two divisors, repeating the second nested FOR LOOP until the number is achieved itself. So, it makes the loop slower as the number (which we have to check is prime or not) gets bigger and bigger. That’s why I called this the slower algorithm.
Check Prime Numbers – Faster Algorithm:
This Batch file is based on the fact that if a number is not divisible by any of the basic numbers (i.e. 1 to 13). Then it can be considered as prime numbers.
Because all other numbers can be factorized by these basic numbers, I’m not so sure about the accuracy in all cases of this algorithm (See update at end of page), but it worked fine when tested it 12 to 15 times.
Update: I’ve Again Checked the Faster Algorithm – And, Luckily Found Some Flaws. Using the slower algorithm gives the correct answer with all integers.
Look at the Difference in the OUTPUT of PRIME NUMBERS UPTO 1000 – with both the Algorithms.
HASH Code - For Verification - (SHA-256)
Keep learning, keep sharing….TheBATeam
Be happy, spread happiness…