Prime numbers

--Originally published at Migue´s Blog

A prime number, or also called prime, is a positive integer greater than 1, that number has no positive integer divisors other than 1 and itself, it means that it can’t be factored.Prime numbers.

This is a list of the first prime numbers.

numeros-primos

Yesterday I made a program to know if a number is prime.

To make this program I saw the tutorials located in the page sololearn.com.  This page is free and in this page you can complete tutorials to learn about how to program in phtyon, c++ and java.

Captura de pantalla de 2016-08-21 23-43-33

In my program I used while loops and if conditionals.

The first while loop, while z==1Captura de pantalla de 2016-08-22 10-44-44, is used to make an infinite program until the user press the combination ctrl+c that cancels the infinite loop and closes the program, or you can click on the “X” in your window Captura de pantalla de 2016-08-22 10-42-16.

This line,Captura de pantalla de 2016-08-22 10-46-56 prints the instructions to close the program in the screen for the users that don’t know how to cancel a loop in the terminal.

Captura de pantalla de 2016-08-22 10-50-52In the fourth line with the function “input” the user introduce  a value with the keyboard and the function “int”, integer, converts the value that the user introduced to an integer value that python can use to do mathematical operations, and the value is saved on the variable named “x”

In the fifth line the variable “a” gets a value of 2. This variable will help us to make the next while loop and to finish it when we reach a result for the number.

Captura de pantalla de 2016-08-22 11-11-15This lines exclude the numbers 0, 1 and the negative numbers from the loop because they are not prime numbers and if they enter to the loop can cause some problems when the program is evaluating the number that we introduced.

Captura de pantalla de 2016-08-22 11-04-51In this final while

Captura de pantalla de 2016-08-25 10-16-14
Captura de pantalla de 2016-08-21 23-45-12
the program make some divisions to know if the number that the user introduced is prime or not.

The variable “a” is the divisor, in the end of the loop the program add 1 to the divisor to avoid an infinite loop.

If in one division the remainder is 0 the number is not a prime number, the program prints that it isn’t a prime number and with the function break ends the loop.

But if the remainder never was 0 and the divisor reached the value of “X” it means that X only has 1 divisor and it is a prime number, and the program prints that “X” is a prime number.

Finally this line Captura de pantalla de 2016-08-25 10-16-14 returns to the first loop to chose another number and do the same things with the new number.

This is the program running in the Ubuntu’s terminal.

Captura de pantalla de 2016-08-21 23-45-12