# Finding Prime Numbers using Python

**OVERVIEW:**

First, what is prime number?

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.

In this article, we will find out the prime numbers up to 1000 with two examples. First by picking any number and checking if it is prime or not and second, by generating 100 random number between 2 to 1000 and testing the number is prime.

**EXPLANATION OF CODE:**

First, import the numpy library and make an array that contain prime values those have squared value under 1000. The maximum prime number which have square value under 1000 is 31. Look the code below:

` `**import** **numpy** **as** **np**
prime_values = np.array([2,3,5,7,11,13,17,19,23,29,31])

Now define the function that tests if the number is prime by using if, elif and else statements and create an empty list to save all numbers in **values** list and prime numbers in **prime **list**.**

**If statement** checks the number present in the array of **prime_values**, If yes, it considers it as a prime number.

In **elif command**, check the given number is divisible by one or more values from the array of **prime_values**. If yes, it is considered as non-prime.

**Else** consider it is prime number. The code is shown below:

```
values = []
prime = []
```**def** isprime(value):
**if** value **in** prime_values:
print("Your number is " + str(value) + ", it is a
prime number")
values.append(value)
prime.append(value)
**elif** sum(value % (prime_values) == 0) >= 1:
print("Your number is " + str(value) + ", it is not a
prime number")
values.append(value)
**else** :
print("Your number is " + str(value) + ", it is a
prime number")
values.append(value)
prime.append(value)

**EXAMPLES:**

**1) CHECK ANY NUMBER BETWEEN 2 to 1000**

I apply the function in 75 and 41, ** **

`isprime(75)`

Your number is 75, it is not a prime number

`isprime(41)`

Your number is 41, it is a prime number

**2) GENERATING 100 RANDOM NUMBERS BETWEEN 2 to 1000**

To generate the random number, I used the for loop and inside it apply the **is_prime() **function. Additionally, I updated the list with all random numbers and prime numbers in it. The code is given below:

*# **100** random values to find prime numbers **in** it*
np.random.seed(123)
*# apply **for** loop*
**for** value **in** range(100):
value = np.random.randint(2,1001)
isprime(value)
*# **this** print **function** shows all random values generate through **for** loop *
print("all random values is given by" , np.array(values), sep = "**\n**")
*# **this** print **function** shows only prime values **in** **100** random numbers *
print("random generation of values find " + str(len(prime)) + " prime numbers, these are:", np.array(prime), sep = "**\n**")

The output of the code above shows two lists, one for all random number generated by for loop and the other shows only prime values found in the random generation of numbers.

all random values is given by

[ 75 41 512 367 384 324 990 100 744 19 597 108 125 571 216 739 98 115 640 49 75 546 944 226 113 411 341 848 255 422 610 210 70 819 825 453 4 342 41 324 598 561 506 959 178 137 875 101 382 862 182 360 867 215 632 864 413 292 995 590 682 901 108 839 578 845 420 828 396 792 719 148 486 273 413 767 160 182 584 367 373 156 722 392 784 257 246 361 973 952 969 131 557 188 359 697 539 436 982 826 307 782]

random generation of values find 18 prime numbers, these are:

[ 41 367 19 571 739 113 41 137 101 839 719 367 373 257 131 557 359 307]

**CONCLUSIONS:**

By using the above function, you can find the prime numbers up to 1000.

The source code is in the __Git hub repository__.