Python

Time Comparison of Different Approaches in Python5 min read

Certain methods to generate prime numbers are more efficient. To show this, let’s study time the performance difference between the approaches generating prime numbers up to a given number.

Approach 1: For Loops

This is a function that generates a list of prime numbers up to a given number.

Here’s a brief explanation of how the function works:

1. The function takes a single argument, “givenNumber”, which is the number up to which the prime numbers should be generated.
2. The “primes” list is initialized to an empty list and will be used to store the prime numbers.
3. The outer for loop iterates over the range of numbers from 2 to “givenNumber” + 1.
4. The “isPrime” variable is initialized to True for each number in the range and is used to determine whether the number is prime or not.
5. The inner for loop iterates over the range of numbers from 2 to the current number in the outer loop. If any of these numbers divide the current number evenly (i.e., the remainder is 0), then “isPrime” is set to False.
6. If “isPrime” is still True after the inner loop finishes executing, then the current number is prime and it is appended to the “primes” list.
7. When the outer loop finishes executing, the function returns the “primes” list.

Approach 2: For Loops with Break

This is a function that generates a list of prime numbers up to a given number.

This function is similar to the “approach1” function, with the exception of a “break” statement being added to the inner for loop.

Here’s a brief explanation of how the function works:

1. The function takes a single argument, “givenNumber”, which is the number up to which the prime numbers should be generated.
2. The “primes” list is initialized to an empty list and will be used to store the prime numbers.
3. The outer for loop iterates over the range of numbers from 2 to “givenNumber” + 1.
4. The “isPrime” variable is initialized to True for each number in the range and is used to determine whether the number is prime or not.
5. The inner for loop iterates over the range of numbers from 2 to the current number in the outer loop. If any of these numbers divide the current number evenly (i.e., the remainder is 0), then “isPrime” is set to False and the loop is broken.
6. If “isPrime” is still True after the inner loop finishes executing, then the current number is prime and it is appended to the “primes” list.
7. When the outer loop finishes executing, the function returns the “primes” list.

Approach 3: For Loop, Break, and Square Root

This is a function that generates a list of prime numbers up to a given number.

This function is similar to the “approach2” function, with the exception of the range of numbers being used in the inner for loop.

Here’s a brief explanation of how the function works:

1. The function takes a single argument, “givenNumber”, which is the number up to which the prime numbers should be generated.
2. The “primes” list is initialized to an empty list and will be used to store the prime numbers.
3. The outer for loop iterates over the range of numbers from 2 to “givenNumber” + 1.
4. The “isPrime” variable is initialized to True for each number in the range and is used to determine whether the number is prime or not.
5. The inner for loop iterates over the range of numbers from 2 to the square root of the current number in the outer loop, plus 1. If any of these numbers divide the current number evenly (i.e., the remainder is 0), then “isPrime” is set to False and the loop is broken.
6. If “isPrime” is still True after the inner loop finishes executing, then the current number is prime and it is appended to the “primes” list.
7. When the outer loop finishes executing, the function returns the “primes” list.

By limiting the range of numbers in the inner loop to the square root of the current number, the function can more efficiently determine whether the number is prime or not. This is because if a number “n” has a divisor greater than the square root of “n”, then it must also have a divisor less than the square root of “n”. Therefore, we only need to check for divisors up to the square root of “n”.

The performance difference can be measured using the the `timeit` library which allows you to time your Python code. In this case, we are measuring the time it take find prime numbers up to 500 for each approach. The code below runs the code for each approach 10000 times and outputs the overall time it took in seconds.

Clearly Approach 3 is the fastest.

Concluding Remarks

The three approaches are definitely not the only ways to calculate prime numbers, but hopefully the various ways of identifying prime numbers helped you. Prime numbers are important in number theory and cryptographic methods like the rsa algorithm.