{"id":200,"date":"2025-05-28T12:43:49","date_gmt":"2025-05-28T07:13:49","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=200"},"modified":"2025-07-07T19:55:01","modified_gmt":"2025-07-07T14:25:01","slug":"python-program-print-divisors-factors-integer","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/python-program-print-divisors-factors-integer\/","title":{"rendered":"Python Program to Print Divisors\/Factors of an Integer"},"content":{"rendered":"\n<p>Hi everyone! In this article, we&#8217;ll explain the Python program to print divisors\/factors of an integer. Helping you understand better via examples, approach, code, dry run, edge cases &amp; time and space complexity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-examples\">Examples<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Example 1:\n\n  Input: x = 10\n  Output: 1 2 5 10\n\nExample 2:\n\n  Input: x = 20\n  Output: 1 2 4 5 10 20\n\nExample 3:\n\n  Input: x = 7\n  Output: 1 7\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: x = <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">10<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: x = <\/span><span style=\"color: #B5CEA8\">20<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">4<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">10<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">20<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">Example <\/span><span style=\"color: #B5CEA8\">3<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Input: x = <\/span><span style=\"color: #B5CEA8\">7<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">  Output: <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #B5CEA8\">7<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-1brute-force-solution-of-python-program-to-print-divisors-or-factors-of-an-integer\">1. BRUTE FORCE SOLUTION of Python Program to Print Divisors\/Factors of an Integer<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-problem-statement\">Problem Statement:<\/h3>\n\n\n\n<p><strong>Objective:<\/strong> We need to find all the factors of a given integer.<\/p>\n\n\n\n<p><strong>Expected Input and Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> An integer &#8220;num&#8221;<\/li>\n\n\n\n<li><strong>Output:<\/strong> A list of integers that are factors of &#8220;num&#8221;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-intuition-and-approach\">Intuition and Approach:<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong> A factor of a number &#8220;num&#8221; is any integer &#8220;i&#8221; such that num % i == 0. By iterating through all integers from 1 to &#8220;num&#8221;, we can check each integer to see if it divides &#8220;num&#8221; without a remainder and collect all such integers.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize an empty list to store the factors<\/li>\n\n\n\n<li>Loop through all integers from 1 to &#8220;num&#8221; inclusive<\/li>\n\n\n\n<li>For each integer &#8220;i&#8221;, check if num % i == 0<\/li>\n\n\n\n<li>If the condition is true, add &#8220;i&#8221; to the list of factors<\/li>\n\n\n\n<li>Return the list of factors after the loop completes<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code\">Code:<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\ndef factors(num: int) -&gt; List[int]:\n    factors = []\n    for i in range(1, num + 1):\n        if num % i == 0:\n            factors.append(i)\n\n    return factors\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> typing <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">factors<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    factors = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, num + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num % i == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            factors.append(i)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> factors<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize List:<\/strong> An empty list factors is initialized to store the factors of &#8220;num&#8221;.<\/li>\n\n\n\n<li><strong>Loop Through Integers:<\/strong> A for loop iterates from 1 to &#8220;num&#8221; inclusive.<\/li>\n\n\n\n<li><strong>Check Divisibility:<\/strong> For each integer &#8220;i&#8221;, check if it divides &#8220;num&#8221; without leaving a remainder using num % i == 0.<\/li>\n\n\n\n<li><strong>Add Factor:<\/strong> If &#8220;i&#8221; is a factor, append it to the factors list.<\/li>\n\n\n\n<li><strong>Return List:<\/strong> After the loop completes, return the factors list containing all the factors of &#8220;num&#8221;.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run:<\/h3>\n\n\n\n<p>Let&#8217;s walk through a step-by-step execution with a sample input explained through images.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-1024x576.png\" alt=\"Dry Run of Brute Force Solution of the Python Program to Print Divisors or Factors of an Integer\" class=\"wp-image-202\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-potential-edge-cases\">Potential Edge Cases:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>num = 1:<\/strong> The function should return [1] because 1 is a factor of itself.<\/li>\n\n\n\n<li><strong>num = 0:<\/strong> By mathematical convention, every integer is a factor of 0, but usually, the function might return an empty list or handle it specially since 0 % any number is 0.<\/li>\n\n\n\n<li><strong>num &lt; 0:<\/strong> The function currently doesn&#8217;t handle negative numbers. If negative numbers are considered, the factors should be the absolute values.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-handling-edge-cases\">Handling Edge Cases:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For num = 1, the function correctly returns [1].<\/li>\n\n\n\n<li>For num = 0, the function currently returns [] as it doesn&#8217;t explicitly handle 0.<\/li>\n\n\n\n<li>For negative numbers, the function should handle the absolute value or reject negative inputs depending on the requirements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-time-and-space-complexity\">Time and Space Complexity:<\/h3>\n\n\n\n<p><strong>Time Complexity:<\/strong> The time complexity is <strong>&#8220;O(n)&#8221;<\/strong> because the function iterates from 1 to &#8220;num&#8221; and performs a modulo operation for each iteration.<\/p>\n\n\n\n<p><strong>Space Complexity:<\/strong> The space complexity is <strong>&#8220;O(k)&#8221;<\/strong>, where &#8220;k&#8221; is the number of factors of &#8220;num&#8221;, as it stores the factors in a list.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read: <strong><a href=\"https:\/\/codeanddebug.in\/blog\/python-program-to-check-armstrong-number\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Python Program to Check Armstrong Number<\/span><\/mark><\/a><\/strong>.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-2better-solution-of-python-program-to-print-divisors-or-factors-of-an-integer\">2. BETTER SOLUTION of Python Program to Print Divisors or Factors of an Integer<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10-problem-statement\">Problem Statement:<\/h3>\n\n\n\n<p><strong>Objective:<\/strong> We need to find all the factors of a given integer.<\/p>\n\n\n\n<p><strong>Expected Input and Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> An integer &#8220;num&#8221;<\/li>\n\n\n\n<li><strong>Output:<\/strong> A list of integers that are factors of &#8220;num&#8221;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-intuition-and-approach\">Intuition and Approach:<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong> A factor of a number &#8220;num&#8221; is any integer &#8220;i&#8221; such that num % i == 0. To optimize the search, we only need to check integers up to num \/\/ 2 because any number larger than num \/\/ 2 (except &#8220;num&#8221; itself) cannot be a factor.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize an empty list to store the factors<\/li>\n\n\n\n<li>Loop through all integers from 1 to num \/\/ 2 inclusive<\/li>\n\n\n\n<li>For each integer &#8220;i&#8221;, check if num % i == 0<\/li>\n\n\n\n<li>If the condition is true, add &#8220;i&#8221; to the list of factors<\/li>\n\n\n\n<li>After the loop, add &#8220;num&#8221; itself to the list since it is always a factor<\/li>\n\n\n\n<li>Return the list of factors<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-code\">Code:<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\ndef factors(num: int) -&gt; List[int]:\n    factors = []\n    for i in range(1, num \/\/ 2 + 1):\n        if num % i == 0:\n            factors.append(i)\n    factors.append(num)\n    return factors\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> typing <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">factors<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    factors = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, num \/\/ <\/span><span style=\"color: #B5CEA8\">2<\/span><span style=\"color: #D4D4D4\"> + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num % i == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            factors.append(i)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    factors.append(num)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> factors<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize List:<\/strong> An empty list factors is initialized to store the factors of &#8220;num&#8221;.<\/li>\n\n\n\n<li><strong>Loop Through Integers:<\/strong> A for loop iterates from 1 to num \/\/ 2 inclusive.<\/li>\n\n\n\n<li><strong>Check Divisibility:<\/strong> For each integer &#8220;i&#8221;, check if it divides &#8220;num&#8221; without leaving a remainder using num % i == 0.<\/li>\n\n\n\n<li><strong>Add Factor:<\/strong> If &#8220;i&#8221; is a factor, append it to the factors list.<\/li>\n\n\n\n<li><strong>Add the Number Itself:<\/strong> After the loop, add &#8220;num&#8221; itself to the list of factors.<\/li>\n\n\n\n<li><strong>Return List:<\/strong> Return the factors list containing all the factors of num&#8221;.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-dry-run\">Dry Run:<\/h3>\n\n\n\n<p>Let&#8217;s walk through a step-by-step execution with a sample input explained through images.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-1024x576.png\" alt=\"Dry Run of Better Solution of the Python Program to Print Divisors or Factors of an Integer\" class=\"wp-image-203\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-better-solution.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-potential-edge-cases\">Potential Edge Cases:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>num = 1:<\/strong> The function should return [1] because 1 is a factor of itself.<\/li>\n\n\n\n<li><strong>num = 0:<\/strong> By mathematical convention, every integer is a factor of 0, but usually, the function might return an empty list or handle it specially since 0 % any number is 0.<\/li>\n\n\n\n<li><strong>num &lt; 0:<\/strong> The function currently doesn&#8217;t handle negative numbers. If negative numbers are considered, the factors should be the absolute values.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-handling-edge-cases\">Handling Edge Cases:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For num = 1, the function correctly returns [1].<\/li>\n\n\n\n<li>For num = 0, the function currently returns [] as it doesn&#8217;t explicitly handle 0.<\/li>\n\n\n\n<li>For negative numbers, the function should handle the absolute value or reject negative inputs depending on the requirements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-time-and-space-complexity\">Time and Space Complexity:<\/h3>\n\n\n\n<p><strong>Time Complexity:<\/strong> The time complexity is <strong>&#8220;O(n\/2)&#8221;<\/strong> or <strong>&#8220;O(n)&#8221;<\/strong>, where &#8220;n&#8221; is the input number. This is because the function iterates from 1 to num \/\/ 2 and performs a modulo operation for each iteration.<\/p>\n\n\n\n<p><strong>Space Complexity:<\/strong> The space complexity is <strong>&#8220;O(k)&#8221;<\/strong>, where &#8220;k&#8221; is the number of factors of &#8220;num&#8221;, as it stores the factors in a list.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Also read: <strong><a href=\"https:\/\/codeanddebug.in\/blog\/palindrome-number-program-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Palindrome Number Program in Python<\/span><\/mark><\/a><\/strong> | Leetcode #9.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-3optimal-solution-of-python-program-to-print-divisors-or-factors-of-an-integer\">3.OPTIMAL SOLUTION of Python Program to Print Divisors or Factors of an Integer<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"18-problem-statement\">Problem Statement:<\/h3>\n\n\n\n<p><strong>Objective:<\/strong> We need to find all the factors of a given integer.<\/p>\n\n\n\n<p><strong>Expected Input and Output:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input:<\/strong> An integer &#8220;num&#8221;.<\/li>\n\n\n\n<li><strong>Output:<\/strong> A list of integers that are factors of &#8220;num&#8221;.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"19-intuition-and-approach\">Intuition and Approach:<\/h3>\n\n\n\n<p><strong>Intuition:<\/strong> A factor of a number &#8220;num&#8221; is any integer &#8220;i&#8221; such that num % i == 0. Instead of iterating through all numbers up to &#8220;num&#8221;, we can optimize the search by only iterating up to the square root of &#8220;num&#8221;. For each divisor &#8220;i&#8221; found in this range, both &#8220;i&#8221; and num \/\/ i are factors of &#8220;num&#8221;.<\/p>\n\n\n\n<p><strong>Approach:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize an empty list to store the factors<\/li>\n\n\n\n<li>Loop through all integers from 1 to the square root of &#8220;num&#8221; inclusive<\/li>\n\n\n\n<li>For each integer &#8220;i&#8221;, check if num % i == 0<\/li>\n\n\n\n<li>If &#8220;i&#8221; is a factor, add &#8220;i&#8221; to the list<\/li>\n\n\n\n<li>If num \/\/ i is not equal to &#8220;i&#8221;, add num \/\/ i to the list to avoid duplicates<\/li>\n\n\n\n<li>Optionally, sort the list of factors before returning it<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"20-code-\"><strong>Code:<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\nfrom math import sqrt\n\n\ndef factors(num: int) -&gt; List[int]:\n    factors = []\n    for i in range(1, int(sqrt(num)) + 1):\n        if num % i == 0:\n            factors.append(i)\n            if num \/\/ i != i:\n                factors.append(num \/\/ i)\n    factors.sort()  # Do this only if you want in sorted order\n    return factors\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> typing <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> List<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C586C0\">from<\/span><span style=\"color: #D4D4D4\"> math <\/span><span style=\"color: #C586C0\">import<\/span><span style=\"color: #D4D4D4\"> sqrt<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">factors<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">num<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    factors = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> i <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">(sqrt(num)) + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num % i == <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            factors.append(i)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> num \/\/ i != i:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                factors.append(num \/\/ i)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    factors.sort()  <\/span><span style=\"color: #6A9955\"># Do this only if you want in sorted order<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> factors<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Import Required Modules:<\/strong> Import the &#8220;List&#8221; type from &#8220;typing&#8221; and the &#8220;sqrt&#8221; function from &#8220;math&#8221;.<\/li>\n\n\n\n<li><strong>Initialize List:<\/strong> An empty list factors is initialized to store the factors of &#8220;num&#8221;.<\/li>\n\n\n\n<li><strong>Loop Through Integers:<\/strong> A for loop iterates from 1 to the integer value of the square root of &#8220;num&#8221; inclusive.<\/li>\n\n\n\n<li><strong>Check Divisibility:<\/strong> For each integer &#8220;i&#8221;, check if it divides &#8220;num&#8221; without leaving a remainder using num % i == 0.<\/li>\n\n\n\n<li><strong>Add Factor:<\/strong> If &#8220;i&#8221; is a factor, append it to the factors list.<\/li>\n\n\n\n<li><strong>Add Corresponding Factor:<\/strong> If num \/\/ i is not equal to &#8220;i&#8221;, append num \/\/ i to the factors list to avoid duplicates.<\/li>\n\n\n\n<li><strong>Sort Factors:<\/strong> Sort the factors list if sorted order is desired.<\/li>\n\n\n\n<li><strong>Return List:<\/strong> Return the factors list containing all the factors of &#8220;num&#8221;.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"21-dry-run\">Dry Run:<\/h3>\n\n\n\n<p>Let&#8217;s walk through a step-by-step execution with a sample input explained through images.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-1024x576.png\" alt=\"Dry Run of Optimal Solution of Python Program to Print Divisors or Factors of an Integer\" class=\"wp-image-204\" srcset=\"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-1024x576.png 1024w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-300x169.png 300w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-768x432.png 768w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-1536x864.png 1536w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-150x84.png 150w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-450x253.png 450w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution-1200x675.png 1200w, https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/dry-run-of-find-factors-of-a-number-optimal-solution.png 1600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22-potential-edge-cases\">Potential Edge Cases:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>num = 1:<\/strong> The function should return [1] because 1 is a factor of itself.<\/li>\n\n\n\n<li><strong>num = 0:<\/strong> By mathematical convention, every integer is a factor of 0, but usually, the function might return an empty list or handle it specially since 0 % any number is 0.<\/li>\n\n\n\n<li><strong>num &lt; 0:<\/strong> The function currently doesn&#8217;t handle negative numbers. If negative numbers are considered, the factors should be the absolute values.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"23-handling-edge-cases\">Handling Edge Cases:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For num = 1, the function correctly returns [1].<\/li>\n\n\n\n<li>For num = 0, the function currently returns [] as it doesn&#8217;t explicitly handle 0.<\/li>\n\n\n\n<li>For negative numbers, the function should handle the absolute value or reject negative inputs depending on the requirements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"24-time-and-space-complexity\">Time and Space Complexity:<\/h3>\n\n\n\n<p><strong>Time Complexity:<\/strong> The time complexity is <strong>&#8220;O(n)&#8221;<\/strong> because the function iterates from 1 to the square root of &#8220;num&#8221; and performs a modulo operation for each iteration.<\/p>\n\n\n\n<p><strong>Space Complexity:<\/strong> The space complexity is <strong>&#8220;O(k)&#8221;<\/strong>, where &#8220;k&#8221; is the number of factors of &#8220;num&#8221;, as it stores the factors in a list.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Join Our Python with DSA Course<\/strong><\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hi everyone! In this article, we&#8217;ll explain the Python program to print divisors\/factors of an integer. Helping you understand better via examples, approach, code, dry run, edge cases &amp; time and space complexity. Examples 1. BRUTE FORCE SOLUTION of Python Program to Print Divisors\/Factors of an Integer Problem Statement: Objective: We need to find all<\/p>\n","protected":false},"author":1,"featured_media":205,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,4],"tags":[8,10],"class_list":{"0":"post-200","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-beginner","9":"tag-easy","10":"tag-math"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/05\/factors-of-a-number-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=200"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/200\/revisions\/544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/205"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}