Member-only story
Any individual attending a programming interview is likely to be asked to solve mathematical questions using code. One common question is to write the code to determine whether a number is prime. A positive integer greater than 1 which does not have other factors except 1 and the number itself is called a prime number. The numbers 2, 3, 5, 7, etc. are prime numbers as they do not have any other factors. To find a prime number in Python, you have to iterate the value from start to end using a for loop and for every number, if it is greater than 1, check if it divides n. If we find any other number which divides, print that value.
Below is code that I have written in Python to determine if a number is prime. A description of what I have coded is:-
- Define the variable mult_num, which is an empty list.
- Prompt the user to enter an integer.
- Create a for loop that will iterate from 2 to the integer entered in increments of 1. If the number modulo the iterated integer equals 0 then append the integrated integer to mult_num.
- Print out the array, mult-num.
- If the length of mult_num is equal to zero then the number is prime, otherwise it is not prime.