{"id":7151,"date":"2021-09-30T10:30:46","date_gmt":"2021-09-30T05:00:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=7151"},"modified":"2021-11-22T18:35:33","modified_gmt":"2021-11-22T13:05:33","slug":"python-program-to-find-the-factors-of-a-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-factors-of-a-number\/","title":{"rendered":"Python Program to Find the Factors of a Number | C++ Program to Find Factors"},"content":{"rendered":"
Beginners and experienced programmers can rely on these Best Java Programs Examples<\/a> and code various basic and complex logics in the Java programming language with ease.<\/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 The prime number is defined as a number that has only two factors\u00a0one and itself. Composite numbers are those that contain more than two variables.<\/p>\n Given a number the task is to print the factors of the given 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 Below are the ways to find the factors of a number:<\/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 Algorithm:<\/strong><\/p>\n Divide a positive number “N” by the natural numbers 1 to “N” to find the factor. If a number is divisible by a natural number, the factor is the natural number. A number N can only have factors in the range of 1 to N.<\/p>\n Approach:<\/strong><\/p>\n This is the simplest and most straightforward way to find variables in a Python number programme. While declaring the variables, we’ll take a number. Python software that uses a for-loop to find factors of a number and print\u00a0the factors\u00a0on the screen.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n It is similar to method 1 except the syntax. Let us see how to find factors\u00a0 using for loop in c++<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n We can see that the outputs of both the programs are same<\/p>\n In these two methods the loop runs from 1 to number N.<\/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 Many of the divisors are present in pairs if we look closely. For eg, if n = 16, the divisors are (1,16), (2,8), and (3,8), (4,4) We use while loop to loop from 1 to sqrt(number)<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. 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 …<\/p>\ngiven_number=360<\/pre>\n
printing the factors of given number\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n8\r\n9\r\n10\r\n12\r\n15\r\n18\r\n20\r\n24\r\n30\r\n36\r\n40\r\n45\r\n60\r\n72\r\n90\r\n120\r\n180\r\n360<\/pre>\n
given_number=10<\/pre>\n
printing the factors of given number\r\n1\r\n2\r\n5\r\n10<\/pre>\n
Program to Find the Factors of a Number<\/h2>\n
\n
1)Using for loop to loop from 1 to N in Python<\/h3>\n
\n
# given number\r\nnumber = 360\r\n# printing the factors of given number\r\nprint(\"printing the factors of given number : \")\r\n# using for loop\r\nfor i in range(1, number+1):\r\n # checking if iterator divides the number if so then print it(because it is factor)\r\n if(number % i == 0):\r\n print(i)\r\n<\/pre>\n
printing the factors of given number : \r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n8\r\n9\r\n10\r\n12\r\n15\r\n18\r\n20\r\n24\r\n30\r\n36\r\n40\r\n45\r\n60\r\n72\r\n90\r\n120\r\n180\r\n360<\/pre>\n
2)Using for loop to loop from 1 to N in C++<\/h3>\n
#include <iostream>\r\nusing namespace std;\r\nint main()\r\n{ \/\/ given number\r\n int number = 360;\r\n \/\/ printing the factors\r\n cout << \"printing the factors of given number :\"\r\n << endl;\r\n \/\/ using for loop to loop from 1 to N\r\n for (int i = 1; i <= number; i++)\r\n \/\/ checking if iterator divides the number if so\r\n \/\/ then print it(because it is factor)\r\n if (number % i == 0)\r\n cout << i << endl;\r\n return 0;\r\n}<\/pre>\n
printing the factors of given number :\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n8\r\n9\r\n10\r\n12\r\n15\r\n18\r\n20\r\n24\r\n30\r\n36\r\n40\r\n45\r\n60\r\n72\r\n90\r\n120\r\n180\r\n360<\/pre>\n
3)Limitations of running loop from 1 to N<\/h3>\n
4)Using while loop to loop from 1 to SQRT(N) in Python<\/h3>\n
\nWe could significantly speed up our program if we took advantage of this fact.
\nHowever, if there are two equal divisors, as in the case of, we must be cautious (4, 4). In that case, we’d just print one of them.<\/p>\n# importing math module\r\nimport math\r\n# given number\r\nnumber = 360\r\n# taking a iterator and initlaizing it with 1\r\ni = 1\r\nprint(\"printing the factors of given number : \")\r\n# looping till sqrt(number) using while\r\nwhile i <= math.sqrt(number):\r\n\r\n if (number % i == 0):\r\n\r\n # If both thee divisors are equal then print only one divisor\r\n if (number \/ i == i):\r\n print(i)\r\n else:\r\n # else print both the divisors\r\n print(i)\r\n print(number\/\/i)\r\n # increment the iterator by 1\r\n i += 1\r\n<\/pre>\n
printing the factors of given number : \r\n1\r\n360\r\n2\r\n180\r\n3\r\n120\r\n4\r\n90\r\n5\r\n72\r\n6\r\n60\r\n8\r\n45\r\n9\r\n40\r\n10\r\n36\r\n12\r\n30\r\n15\r\n24\r\n18\r\n20<\/pre>\n
\n