{"id":4419,"date":"2020-10-31T00:01:06","date_gmt":"2020-10-31T07:01:06","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4419"},"modified":"2020-10-31T09:01:41","modified_gmt":"2020-10-31T16:01:41","slug":"generating-prime-numbers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4419","title":{"rendered":"Generating Prime Numbers"},"content":{"rendered":"<p>Numbers with only two factors, one and themselves, are prime. One way to discover which numbers are prime in a computer program is to plow through all the factors.<br \/>\n<!--more--><br \/>\nIn <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4413\">last week&#8217;s Lesson<\/a>, I presented code that counts the number of factors for a range of values. When the number of factors is zero (excluding 1 and the value itself), the value is prime. The following code makes an improvement to the examples from last week&#8217;s Lesson by adding a <code>TRUE<\/code>\/<code>FALSE<\/code> (Boolean) variable <code>isprime<\/code> to determine prime numbers:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10_31-Lesson-a.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10_31-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint main()\r\n{\r\n    int prime,x,isprime;\r\n\r\n    prime = 2;\r\n    while(prime&lt;100)\r\n    {\r\n        isprime = TRUE;\r\n        printf(\"%d:\",prime);\r\n        for( x=2; x&lt;prime; x++ )\r\n        {\r\n            if( prime%x == 0 )\r\n            {\r\n                isprime=FALSE;\r\n                break;\r\n            }\r\n        }\r\n\r\n        if( isprime )\r\n            puts(\"Prime\");\r\n        else\r\n            puts(\"Not prime\");\r\n\r\n        prime++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>isprime<\/code> is declared as an <em>int<\/em>, not a <em>_bool<\/em>, still its use is the same. The variable is initialized to <code>TRUE<\/code> at Line 13. Line 17 tests for factors:<\/p>\n<p><code>if( prime%x == 0 )<\/code>)<\/p>\n<p>If the result of <code>prime%x<\/code> is equal to zero, a factor has been found and the value of <code>isprime<\/code> is set to <code>FALSE<\/code>, then the loop breaks (Line 20). But when a value stored in variable <code>prime<\/code> lacks factors, <code>isprime<\/code> remains <code>TRUE<\/code> and an <em>if-else<\/em> structure outputs the proper results, shown here:<\/p>\n<p><code>2:Prime<br \/>\n3:Prime<br \/>\n4:Not prime<br \/>\n5:Prime<br \/>\n6:Not prime<br \/>\n7:Prime<br \/>\n8:Not prime<br \/>\n9:Not prime<br \/>\n10:Not prime<br \/>\n11:Prime<\/p>\n<p>... <em>continues<\/em> ...<\/p>\n<p>88:Not prime<br \/>\n89:Prime<br \/>\n90:Not prime<br \/>\n91:Not prime<br \/>\n92:Not prime<br \/>\n93:Not prime<br \/>\n94:Not prime<br \/>\n95:Not prime<br \/>\n96:Not prime<br \/>\n97:Prime<br \/>\n98:Not prime<br \/>\n99:Not prime<\/code><\/p>\n<p>Of course, this list is difficult to sift through. It&#8217;s more of a subtle update to the code from last week&#8217;s Lesson. In the following update, the code outputs only found prime numbers:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_10_31-Lesson-b.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_10_31-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint main()\r\n{\r\n    int prime,x,isprime;\r\n\r\n    prime = 2;\r\n    while(prime&lt;100)\r\n    {\r\n        isprime = TRUE;\r\n        for( x=2; x&lt;prime; x++ )\r\n        {\r\n            if( prime%x == 0 )\r\n            {\r\n                isprime=FALSE;\r\n                break;\r\n            }\r\n        }\r\n\r\n        if( isprime )\r\n            printf(\" %d\",prime);\r\n\r\n        prime++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code limits output to a single <em>printf()<\/em> statement at Line 24, showing only prime numbers from 2 through 99:<\/p>\n<p><code> 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97<\/code><\/p>\n<p>I&#8217;m sure the output appeared immediately on your computer; the code doesn&#8217;t really work hard to pump out the first several prime numbers. For a thrill, change the looping value at Line 11 to 1000000 (one million) and see how long those last few values take to output.<\/p>\n<p>This code is just one way to hunt prime numbers. In next week&#8217;s Lesson I show a different approach, one that is optimized slightly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One way of coding a prime number generating program is to discover which numbers have factors of only 1 and themselves. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4419\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4419","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4419"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4419\/revisions"}],"predecessor-version":[{"id":4438,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4419\/revisions\/4438"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}