{"id":20247,"date":"2022-11-09T04:02:23","date_gmt":"2022-11-08T22:32:23","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/"},"modified":"2022-11-09T04:02:23","modified_gmt":"2022-11-08T22:32:23","slug":"solved-printing-n-pairs-of-prime-numbers-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/","title":{"rendered":"[Solved] Printing n pairs of prime numbers, C++"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-41209050\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"41209050\" data-parentid=\"41208984\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here is the top-level simple pseudo-code for such a beast:<\/p>\n<pre><code>def printTwinPrimes(count):\n    currNum = 3\n    while count &gt; 0:\n        if isPrime(currNum) and isPrime(currNum + 2):\n            print currnum, currnum + 2\n            count = count - 1\n        currNum = currNum + 2\n<\/code><\/pre>\n<p>It simply starts at <code>3<\/code> (since we know <code>2,4<\/code> is impossible as a twin-prime pair because <code>4<\/code> is composite). For each possibility, it checks whether it constitutes a twin-prime pair and prints it if so.<\/p>\n<p>So all you need to do (other than translating that into <em>real<\/em> code) is to create <code>isPrime()<\/code>, for which there are countless examples on the net.<\/p>\n<p>For completeness, here&#8217;s a simple one, by no means the most efficient but adequate for beginners:<\/p>\n<pre><code>def isPrime(num):\n    if num &lt; 2:\n        return false\n    root = 2\n    while root * root &lt;= num:\n        if num % root == 0:\n            return false\n        root = root + 1\n    return true\n<\/code><\/pre>\n<p>Though you <em>could<\/em> make that more efficient by using the fact that all primes other than two or three are of the form <code>6n\u00b11, n &gt;= 1<\/code><sup>(a)<\/sup>:<\/p>\n<pre><code>def isPrime(num):\n    if num &lt; 2: return false\n    if num == 2 or num == 3: return true\n    if num % 2 == 0 or num % 3 == 0: return false\n    if num % 6 is neither 1 nor 5: return false\n\n    root = 5\n    adder = 2                   # initial adder 2, 5 -&gt; 7\n    while root * root &lt;= num:\n        if num % root == 0:\n            return false\n        root = root + adder     # checks 5, 7, 11, 13, 17, 19, ...\n        adder = 6 - adder       # because alternate 2, 4 to give 6n\u00b11\n    return true\n<\/code><\/pre>\n<hr>\n<p>In fact, you can use this divisibility trick to see if an arbitraily large number stored as a string is likely to be a prime. You just have to check if the number below it or above it is divisible by six. If not, the number is definitely not a prime. If so, more (slower) checks will be needed to fully ascertain primality.<\/p>\n<p>A number is divisible by six only if it is divisible by both two and three. It&#8217;s easy to tell the former, even numbers end with an even digit.<\/p>\n<p>But it&#8217;s also reasonably easy to tell if it&#8217;s divisible by <em>three<\/em> since, in that case, the sum of the individual digits will <em>also<\/em> be divisible by three. For example, lets&#8217; use <code>31415926535902718281828459<\/code>.<\/p>\n<p>The sum of all those digits is <code>118<\/code>. How do we tell if <em>that&#8217;s<\/em> a multiple of three? Why, using exactly the same trick recursively:<\/p>\n<pre><code>118: 1 + 1 + 8 = 10\n 10: 1 + 0     = 1\n<\/code><\/pre>\n<p>Once you&#8217;re down to a single digit, it&#8217;ll be <code>0<\/code>, <code>3<\/code>, <code>6<\/code>, or <code>9<\/code> if the original number was a multiple of three. Any other digit means it wasn&#8217;t (such as in this case).<\/p>\n<hr>\n<p><sup>(a)<\/sup> If you divide any non-negative number by six and the remainder is 0, 2 or 4, then it&#8217;s even and therefore non-prime (2 is the exception case here):<\/p>\n<pre><code>6n + 0 = 2(3n + 0), an even number.\n6n + 2 = 2(3n + 1), an even number.\n6n + 4 = 2(3n + 2), an even number.\n<\/code><\/pre>\n<p>If the remainder is 3, then it is divisible by 3 and therefore non-prime (3 is the exception case here):<\/p>\n<pre><code>6n + 3 = 3(2n + 1), a multiple of three.\n<\/code><\/pre>\n<p>That leaves just the remainders 1 and 5, and those numbers are all of that form <code>6n\u00b11<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Printing n pairs of prime numbers, C++ <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here is the top-level simple pseudo-code for such a beast: def printTwinPrimes(count): currNum = 3 while count &gt; 0: if isPrime(currNum) and isPrime(currNum + 2): print currnum, currnum + 2 count = count &#8211; 1 currNum = currNum + 2 It simply starts at 3 (since we know 2,4 is impossible as a twin-prime &#8230; <a title=\"[Solved] Printing n pairs of prime numbers, C++\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\" aria-label=\"More on [Solved] Printing n pairs of prime numbers, C++\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-20247","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Printing n pairs of prime numbers, C++ - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Printing n pairs of prime numbers, C++ - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here is the top-level simple pseudo-code for such a beast: def printTwinPrimes(count): currNum = 3 while count &gt; 0: if isPrime(currNum) and isPrime(currNum + 2): print currnum, currnum + 2 count = count - 1 currNum = currNum + 2 It simply starts at 3 (since we know 2,4 is impossible as a twin-prime ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-08T22:32:23+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Printing n pairs of prime numbers, C++\",\"datePublished\":\"2022-11-08T22:32:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\"},\"wordCount\":368,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\",\"name\":\"[Solved] Printing n pairs of prime numbers, C++ - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-08T22:32:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Printing n pairs of prime numbers, C++\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Printing n pairs of prime numbers, C++ - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Printing n pairs of prime numbers, C++ - JassWeb","og_description":"[ad_1] Here is the top-level simple pseudo-code for such a beast: def printTwinPrimes(count): currNum = 3 while count &gt; 0: if isPrime(currNum) and isPrime(currNum + 2): print currnum, currnum + 2 count = count - 1 currNum = currNum + 2 It simply starts at 3 (since we know 2,4 is impossible as a twin-prime ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/","og_site_name":"JassWeb","article_published_time":"2022-11-08T22:32:23+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Printing n pairs of prime numbers, C++","datePublished":"2022-11-08T22:32:23+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/"},"wordCount":368,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/","url":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/","name":"[Solved] Printing n pairs of prime numbers, C++ - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-08T22:32:23+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-printing-n-pairs-of-prime-numbers-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Printing n pairs of prime numbers, C++"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=20247"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/20247\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=20247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=20247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=20247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}