{"id":926,"date":"2017-04-22T21:16:11","date_gmt":"2017-04-23T05:16:11","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=926"},"modified":"2019-09-03T17:31:22","modified_gmt":"2019-09-04T01:31:22","slug":"find-if-an-integer-is-a-prime-number","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=926","title":{"rendered":"Find if an integer is a prime number"},"content":{"rendered":"<p>The algorithm can be improved further by observing that all primes are of the form 6k \u00b1 1, with the exception of 2 and 3. This is because all integers can be expressed as (6k + i) for some integer k and for i = -1, 0, 1, 2, 3, or 4; 2 divides (6k + 0), (6k + 2), (6k + 4); and 3 divides (6k + 3). So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form 6k \u00b1 1.<\/p>\n<pre>function isPrime($n)\r\n{\r\n    \/\/ Corner cases\r\n    if ($n &lt;= 1)  return false;\r\n    if ($n &lt;= 3)  return true;\r\n\r\n    \/\/ This is checked so that we can skip\r\n    \/\/ middle five numbers in below loop\r\n    if ($n % 2 == 0 || $n % 3 == 0) return false;\r\n\r\n    for ($i=5; $i*$i&lt;=$n; $i=$i+6) {\r\n        if ($n % $i == 0 || $n % ($i+2) == 0)\r\n           return false;\r\n    }\r\n\r\n    return true;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The algorithm can be improved further by observing that all primes are of the form 6k \u00b1 1, with the exception of 2 and 3. This is because all integers can be expressed as (6k + i) for some integer k and for i = -1, 0, 1, 2, 3, or 4; 2 divides (6k &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=926\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Find if an integer is a prime number<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-926","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=926"}],"version-history":[{"count":3,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/926\/revisions"}],"predecessor-version":[{"id":1064,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/926\/revisions\/1064"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}