Lompat ke konten Lompat ke sidebar Lompat ke footer

Creating Array of N Prime Number Python

Contents
1. Creating Array of N Prime Numbers Algorithm
2. Python Script of N Prime Number Generator
3. Conclusion

According to wikipedia, prime number is natural number greater than 1 that is not product of other smaller two natural number. It is an important kind of number because of  it's dozens usages in real life.

For example, cicadas, a kind of insect, use primary number pattern of their laying egg years. They only come out of their burrows in pattern of 7, 13, 17 years interval to lay eggs. 

Array of primary numbers is often used in mathematical problems. For example, sometimes we need to create N number of primary number. It is easy if the number only consisted of ten or more number, what about 100 numbers? and N is changed accordingly?

Well, the array can be easily generated using some algorithms and Python of course.


Creating Array of N Prime Numbers Algorithm

prime_numbers

To create the algorithm, let's use other definition of prime number. Prime number is number that larger than 1 and can not be divided by smaller prime number.

Using this definition, process of generating prime number will be significantly faster. Since, a number is not need to be divided by all smaller natural number.

Here is the algorithm for generating N prime numbers.
  1. First, define N or number of prime numbers.
  2. Create a list which contains first prime number, 2.
  3. Check next natural number that can not be divided by 2, we know that it is 3.
  4. Append 3 into list, so now the list contains 2 and 3.
  5. Check next natural number, now it can not be divided by 2 and 3, append those number into the list.
  6. Repeat the processes until number of prime number inside the list is equal to N.
Ok, by using the algorithm, let's build prime number genereator program on Python.

Python Script of N Prime Number Generator

The script will be utilized loop process since it repeat several processes. Instead of using for loop, I will use while loop since the loop will be terminated after condition can not be fulfilled. In this case, the condition is number of prime number is smaller or equal to N.

This is the script of prime number generator, let's elaborate it on the next paragraph.

1   N = 10
2   prime_list = [2]
3   natural_number = 3
4   
5   while len(prime_list) <= N:
6       # Divide natural number by prime number inside list
7       for i in range(len(prime_list)):
8           if natural_number % prime_list[i] == 0:
9               break
10      
11      # Check whether natural number can be divided by last prime number
12      if natural_number % prime_list[i] != 0:
13          prime_list.append(natural_number)
14      
15      # Check for next natural number
16      natural_number += 1
17      
18  print(prime_list)

output:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

Script explanation:
  1. First, create the parameters. prime_list contains list of identified prime number, first is consisted of 2, natural_number is number that will be checked that it is prime number or not, and N is number of desired prime number.
  2. Since 2 already inside the list, so checked natural_number is started from 3.
  3. Initiate while loop, line 5, with condition number of prime_list is smaller or equal to N. The script will generate 10 prime numbers.
  4. At line 7 create new for loop to check whether the natural number can be divided by existing prime number on the list. The loop will be stopped if natural_number can be divided by existing prime number or have reach maximum loop number.
  5. To make sure that natural_number can not be divided, let's check by divide it with last prime_number before the loop break at line 12.
  6. The step make the previous loop was ended because of natural_number is a prime number, or the loop just reach it's maximum.
  7. After natural_number is definitely a prime number, append it on prime_list at line 13. Otherwise, nothing will be done.
  8. Don't forget to increase natural_number by 1 at line 16, so next natural_number can be examined. This incrementation is crucial on while loop, otherwise the loop will never stop.
  9. In the output, we can see 10 prime numbers, same as number as N.


Conclusion

Well, it's quite simple to create python script of prime numbers generator. This way, we can easily create N number of prime numbers, whether it's 10 or 1000. 

And since examined natural number only divided by smaller prime number, the program can run faster compared if the natural number is divided by all smaller natural numbers.

Posting Komentar untuk "Creating Array of N Prime Number Python"