{"id":7193,"date":"2021-09-30T10:30:12","date_gmt":"2021-09-30T05:00:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=7193"},"modified":"2021-11-22T18:35:34","modified_gmt":"2021-11-22T13:05:34","slug":"python-program-to-check-a-number-is-prime-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-a-number-is-prime-or-not\/","title":{"rendered":"Python Program to Check a Number is Prime or Not"},"content":{"rendered":"
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output<\/a><\/p>\n Factors of a number:<\/strong><\/p>\n When two whole numbers are multiplied, the result is a product. The factors of the product are the numbers we multiply.<\/p>\n In mathematics, a factor is a number or algebraic expression that equally divides another number or expression, leaving no remainder.<\/p>\n Prime Number:<\/strong><\/p>\n A prime number is a positive integer greater than 1 that has no other variables except 1 and the number itself. Since they have no other variables, the numbers 2, 3, 5, 7, and so on are prime numbers.<\/p>\n Given a number , the task is to check whether the given number is prime or not.<\/p>\n Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check whether the given number is prime or not:<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n To see if there are any positive divisors other than 1 and number itself, we divide the input number by all the numbers in the range of 2 to (N\u00a0\u2013 1).<\/p>\n If a divisor is found, the message “number is not a prime number” is displayed\u00a0otherwise, the message “number is a prime number” is displayed.<\/p>\n We iterate from 2 to N-1 using for loop.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n We checked whether a number is prime or not in this program . Prime numbers are not\u00a0\u00a0those that are smaller than or equal to one. As a result, we only go forward if the number is greater than one.<\/p>\n We check whether a number is divisible exactly by any number between 2 and number – 1. We set temp to True and break\u00a0the loop if we find a factor in that range, indicating that the number is not prime.<\/p>\n We check whether temp is True or False outside of the loop.<\/p>\n Number is not a prime number if it is True. To check if number is prime, we used a for…else argument.<\/p>\n It is based on the logic that the for loop’s else statement runs if and only if the for loop is not broken out. Only when no variables are found is the condition satisfied, indicating that the given number is prime.<\/p>\n As a result, we print that the number is prime in the else clause.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n In these two methods the loop runs from 2 to number N-1.<\/p>\n Hence we can say that the time complexity of above methods are O(n).<\/p>\n What if the number is very large?<\/p>\n Like 10^18 the above methods takes nearly 31 years to execute.<\/p>\n Then How to avoid this?<\/p>\n We can see that the factors of the numbers exist from 1 to N\/2 except number itself.<\/p>\n But this also takes nearly 15 yrs to execute.<\/p>\n So to above this we loop till square root of N in next method which gives Time Complexity O(Sqrt(n)).<\/p>\n We will improve our program by reducing the number range in which we search for factors.<\/p>\n Our search range in the above program is 2 to number – 1.<\/p>\n The set, range(2,num\/2) or range(2,math.floor(math.sqrt(number)) should have been used. The latter range is based on the requirement that a composite number’s factor be less than its square root. Otherwise, it’s a prime number.<\/p>\n In this function, we use Python’s math library to calculate an integer, max_divisor, that is the square root of the number and get its floor value. We iterate from 2 to n-1 in the last example. However, we reduce the divisors by half in this case, as shown. To get the floor and sqrt functions, you’ll need to import the math module. Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output Factors of a number: When two whole numbers are multiplied, the result is a product. The factors of the product are the numbers we multiply. In mathematics, a factor is a …<\/p>\nnumber =5<\/pre>\n
The given number 5 is prime number<\/pre>\n
number =8<\/pre>\n
The given number 8 is not prime number<\/pre>\n
number =2<\/pre>\n
The given number 2 is not prime number<\/pre>\n
Program to Check Prime Number in Python Programming<\/h2>\n
\n
1)Using for loop to loop from 2 to N-1 using flag or temp variable<\/h3>\n
# given number\r\nnumber = 5\r\n# intializing a temporary variable with False\r\ntemp = False\r\n# We will check if the number is greater than 1 or not\r\n# since prime numbers starts from 2\r\nif number > 1:\r\n # checking the divisors of the number\r\n for i in range(2, number):\r\n if (number % i) == 0:\r\n # if any divisor is found then set temp to true since it is not prime number\r\n temp = True\r\n # Break the loop if it is not prime\r\n break\r\nif(temp):\r\n print(\"The given number\", number, \"is not prime number\")\r\nelse:\r\n print(\"The given number\", number, \"is prime number\")\r\n<\/pre>\n
The given number 5 is prime number<\/pre>\n
\nIf False, the given number is a prime number.<\/p>\n2)Using For Else Statement<\/h3>\n
# given number\r\nnumber = 5\r\n\r\n# We will check if the number is greater than 1 or not\r\n# since prime numbers starts from 2\r\nif number > 1:\r\n # checking the divisors of the number\r\n for i in range(2, number):\r\n if (number % i) == 0:\r\n # if any divisor is found then print it is not prime\r\n print(\"The given number\", number, \"is not prime number\")\r\n # Break the loop if it is not prime\r\n break\r\n else:\r\n print(\"The given number\", number, \"is prime number\")\r\n# if the number is less than 1 then print it is not prime number\r\nelse:\r\n print(\"The given number\", number, \"is not prime number\")\r\n<\/pre>\n
The given number 5 is prime number<\/pre>\n
3)Limitations of above methods in terms of Time Complexity<\/h3>\n
4)Solution Approach for Efficient Approach<\/h3>\n
5)Implementation of Efficient Approach<\/h3>\n
\nApproach:<\/strong><\/p>\n\n
# importing math module\r\nimport math\r\n# function which returns True if the number is prime else not\r\n\r\n\r\ndef checkPrimeNumber(number):\r\n if number < 1:\r\n return False\r\n # checking the divisors of the number\r\n max_divisor = math.floor(math.sqrt(number))\r\n for i in range(2, max_divisor+1):\r\n if number % i == 0:\r\n # if any divisor is found then return False\r\n return False\r\n # if no factors are found then return True\r\n return True\r\n\r\n\r\n# given number\r\nnumber = 5\r\n# passing number to checkPrimeNumber function\r\nif(checkPrimeNumber(number)):\r\n print(\"The given number\", number, \"is prime number\")\r\nelse:\r\n print(\"The given number\", number, \"is not prime number\")\r\n<\/pre>\n
The given number 5 is prime number<\/pre>\n
\n