{"id":9264,"date":"2021-09-30T11:00:03","date_gmt":"2021-09-30T05:30:03","guid":{"rendered":"https:\/\/python-programs.com\/?p=9264"},"modified":"2021-11-22T18:35:32","modified_gmt":"2021-11-22T13:05:32","slug":"python-program-to-compute-prime-factors-of-an-integer","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-compute-prime-factors-of-an-integer\/","title":{"rendered":"Python Program to Compute Prime Factors of an Integer"},"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 In this post, we’ll look at a Python program that prints out all the prime factors of a given number. A number is considered to be a prime factor of another number if it is a prime number and perfectly divides the given number. In this section, we will look at what a prime factor is, how to discover a prime factor, and the python program.<\/p>\n A number’s prime factors are the prime numbers that, when multiplied together, give the number. Two conditions can be used to determine a number’s prime factor:<\/p>\n The digit should be a prime number.<\/p>\n The number should perfectly divide the number.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to compute prime factors of an integer in python some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n 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 In this post, we’ll look at a Python program that prints out all the prime factors of a given number. A number is considered to be a prime factor of another …<\/p>\ngiven number =240<\/pre>\n
The prime factors of the given number are : \r\n2\r\n3\r\n5<\/pre>\n
given number =33<\/pre>\n
Enter some random number = 33\r\nThe prime factors of the given number are : \r\n3\r\n11<\/pre>\n
Program to Compute Prime Factors of an Integer in Python<\/h2>\n
\n
Method #1: Using while loop (Static Input)<\/h3>\n
\n
# given number\r\nnumb = 240\r\n# Printing the prime factors\r\nprint(\"The prime factors of the given number are : \")\r\nvalue = 1\r\nwhile(value <= numb):\r\n k = 0\r\n if(numb % value == 0):\r\n j = 1\r\n while(j <= value):\r\n if(value % j == 0):\r\n k = k+1\r\n j = j+1\r\n if(k == 2):\r\n # printing the prime factor\r\n print(value)\r\n # incremeent the value by 1\r\n value = value+1\r\n<\/pre>\n
The prime factors of the given number are : \r\n2\r\n3\r\n5<\/pre>\n
\n
Method #2: Using while loop (User Input)<\/h3>\n
\n
# Scan the number using int(input()) function.\r\nnumb = int(input(\"Enter some random number = \"))\r\n# Printing the prime factors\r\nprint(\"The prime factors of the given number are : \")\r\nvalue = 1\r\nwhile(value <= numb):\r\n k = 0\r\n if(numb % value == 0):\r\n j = 1\r\n while(j <= value):\r\n if(value % j == 0):\r\n k = k+1\r\n j = j+1\r\n if(k == 2):\r\n # printing the prime factor\r\n print(value)\r\n # incremeent the value by 1\r\n value = value+1\r\n<\/pre>\n
Enter some random number = 220\r\nThe prime factors of the given number are : \r\n2\r\n5\r\n11<\/pre>\n
Method #3:Efficient Method (User input)<\/h3>\n
\n
import math\r\n# Scan the number using int(input()) function.\r\nnumb = int(input(\"Enter some random number = \"))\r\n# Printing the prime factors\r\nprint(\"The prime factors of the given number are : \")\r\nwhile numb % 2 == 0:\r\n print(2),\r\n numb = numb \/ 2\r\n\r\n # n became odd\r\nfor i in range(3,int(math.sqrt(numb))+1,2):\r\n while (numb % i == 0):\r\n print (int(i))\r\n numb = numb \/ i\r\n \r\nif numb > 2:\r\n print (int(numb))\r\n<\/pre>\n
Enter some random number = 33\r\nThe prime factors of the given number are : \r\n3\r\n11<\/pre>\n
\n
\n