{"id":419,"date":"2017-08-07T13:33:43","date_gmt":"2017-08-07T13:33:43","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=419"},"modified":"2017-08-07T13:33:51","modified_gmt":"2017-08-07T13:33:51","slug":"factors-of-a-number-using-python","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2017\/08\/07\/factors-of-a-number-using-python\/","title":{"rendered":"Factors of a number using python"},"content":{"rendered":"<p>This exercise is to find all the factors of a given number. Remember that a factor divides evenly into a number without leaving a remainder. We can use the modulo operator (%) to check for any remainder. The program will check all integers from 2 up to but not including the original number. To loop through those numbers we will use the <code>for<\/code> loop combined with the <code>range()<\/code> function.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nnumber = int(input(&quot;Enter the number?&quot;))\r\nfor i in range(2,number):\r\n    remainder = number % i\r\n    quotient = number \/\/ i\r\n    print(number,&quot;=&quot;,i,&quot;x&quot;,quotient,&quot;+ remainder&quot;,remainder)\r\n    if remainder == 0:\r\n        print(&quot;remainder is zero so&quot;,i,&quot;is a factor&quot;)\r\n    else:\r\n        print(&quot;remainder is not zero so&quot;,i,&quot;is not a factor&quot;)\r\n<\/pre>\n<p>Using <code>print()<\/code> in this way shows clearly the flow of the program. However, two lines of code for each potential factor is a lot to read through. To reduce the number of lines of code, <code>print()<\/code> lets us specify a different ending than a new line. If we specify end=&#8217; &#8216; then print() will end the line with a space rather than a new line significantly reducing the number of lines of output.<\/p>\n<p>The following program ignores output when the number is not a factor, and displays all the factors on the same line, with a final message whether the original number is prime or composite. Instead of asking for the number to be entered, the program finds all factors for all numbers between 2 and 99 inclusive.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfor number in range(2,100):\r\n    print(number,'factors:', end=' ')\r\n    prime=True\r\n    for i in range(2,number):\r\n        if number%i == 0:\r\n            print(i, end=' ')\r\n            prime=False\r\n    if prime:\r\n        print('prime')\r\n    else:\r\n        print('composite')\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This exercise is to find all the factors of a given number. Remember that a factor divides evenly into a number without leaving a remainder. We can use the modulo operator (%) to check for any remainder. The program will check all integers from 2 up to but not including the original number. To loop [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-419","post","type-post","status-publish","format-standard","hentry","category-coderdojo"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/comments?post=419"}],"version-history":[{"count":1,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/419\/revisions"}],"predecessor-version":[{"id":420,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/419\/revisions\/420"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}