No one denied it, everyone tried to calculate prime numbers at some stage of the programming learning process. These early algorithms are often inefficient algorithms. Based on my own experience, I will talk about the evolution of prime number calculation functions from Python.

## 1- Describe – Procedural

We know prime numbers as numbers that cannot be divided into multiple numbers. This is an accurate definition. New learners of programming language first write algorithms based on the definition.

```
# Program with 1000 prime numbers
print 2
# xrange not yet ...
for i in range(3,1000):
bolundu = False
for j in range(2,i):
if i % j == 0:
divided=True
# break yok...
if divided == False:
print i
```

## 2- Describe – Functional

Programmers who write the first algorithm that finds a prime number then consider turning it into a function and using it over and over again. At the same time, several important details have been added to the algorithm.

```
def prime(how_much):
"""Function finding the prime number
Gets a number as input
Prints the prime numbers up to this number on the screen.
"""
if how_much < 2:
return
elif how_much == 2:
print 2
return
else:
print 2
for i in range(3,how_much):
divided = False
for j in range(2,i):
if i % j == 0:
divided=True
break
if divided == False:
print i
```

## 3- Returning a List from a Function

The programmer, who reads the values against the screen directly against the screen, returns a list from the function. Then he prints the list on the screen.

```
def prime(how_much):
"""Function finding the prime number
Gets a number as input
Prints the prime numbers up to this number on the screen.
"""
primes = [2]
if how_much < 2:
return None
elif how_much == 2:
return primes
else:
for i in range(3,how_much):
divided = False
for j in range(2,i):
if i % j == 0:
divided=True
break
if divided == False:
primes.append(i)
return primes
if __name__ == "__main__":
# To use join, the list must be str.
print "\n".join(map(str,prime(1000)))
"""
If map () is unknown at this stage, a list comprehension or for loop is used.
print "\n".join([str(prime) for prime in prime(1000)])
for prime in prime(1000)
print prime
"""
```

## 4- Performance Concerns

The programmer is now at the stage to take the algorithm’s performance into mind. Even numbers are no longer calculated, numbers are not divided by numbers greater than half.

```
def prime(how_much):
"""Function finding the prime number
Gets a number as input
Prints the prime numbers up to this number on the screen.
"""
primes = [2]
if how_much < 2:
return None
elif how_much == 2:
return primes
else:
for i in xrange(3,how_much,2):
divided = False
for j in xrange(3,(i/2) + 1, 2):
if i % j == 0:
divided =True
break
if divided == False:
primes.append(i)
return primes
if __name__ == "__main__":
# To use join, the list must be str.
print "\n".join(map(str,prime(1000)))
"""
If map () is unknown at this stage, a list comprehension or for loop is used.
print "\n".join([str(prime) for prime in prime(1000)])
for prime in prime(1000)
print prime
"""
```

## 5- Just Trying to Divide by Primes

The programmer remembers to find a prime number, just trying to divide it by prime numbers, combines it with the previous method.

```
def prime(how_much):
"""Function finding the prime number
Gets a number as input
Prints the prime numbers up to this number on the screen.
"""
primes = [2]
if how_much < 2:
return None
elif how_much == 2:
return primes
else:
for i in xrange(3,how_much,2):
divided = False
for j in primes:
if i % j == 0:
divided=True
break
if j > (i / 2):
break
if divided == False:
primes.append(i)
return primes
if __name__ == "__main__":
# To use join, the list must be str.
print "\n".join(map(str,prime(2000)))
"""
If map () is unknown at this stage, a list comprehension or for loop is used.
print "\n".join([str(prime) for prime in prime(1000)])
for prime in prime(1000)
print prime
"""
```

## 6- Divide by square root

The prime divisors of a number cannot be larger than the square root of that number.

```
def prime(how_much):
"""Function finding the prime number
Gets a number as input
Prints the prime numbers up to this number on the screen.
"""
primes = [2]
if how_much < 2:
return None
elif how_much == 2:
return primes
else:
for i in xrange(3,how_much,2):
divided = False
# We add + 1 in case the square root is rolling down.
limit = (i ** 0.5) + 1
for j in primes:
if i % j == 0:
divided=True
break
if j > limit:
break
if divided == False:
primes.append(i)
return primes
```

## 7- Qualification of Eratosthenes

In this method, odd numbers from 3 to the given number are added together in a list. Then, by skipping 3 by 3, the numbers hovered are equal to 0. Thus, the multiples of 3 are eliminated. Then, by skipping the first non-0 number, the numbers hovered are equal to 0. Thus, the multiples of that number are eliminated. In this way, it continues until the square root of the largest number. As we just mentioned, a number cannot have a prime divider greater than its square root. And, the square root of this number is larger than the square root of all numbers in the list. Therefore, when the square root of this number is reached, the elimination can be ended.

```
def prime(how_much):
"""Function finding the prime number
Gets a number as input
Prints the prime numbers up to this number on the screen.
"""
if how_much < 2:
return None
elif how_much == 2:
return [2]
else:
# 3,5,7...,how_much
numbers = range(3,how_much + 1, 2)
root = how_much ** 0.5
for i in xrange(0,len(numbers)):
number = numbers[i]
if number > root:
break
if number:
first_delete_index = i + number
for j in xrange(first_delete_index,len(numbers),number):
numbers[j] = 0
return [2] + [number for number in numbers if number]
if __name__ == "__main__":
# To use join, the list must be str.
print "\n".join(map(str,prime(500)))
"""
If map () is unknown at this stage, a list comprehension or for loop is used.
print "\n".join([str(prime) for prime in prime(1000)])
for prime in prime(1000)
print prime
```

To read to read my other blog posts: “https://www.flutteranywhere.com“