{"id":187,"date":"2023-02-08T04:05:55","date_gmt":"2023-02-08T04:05:55","guid":{"rendered":"https:\/\/jassweb.com\/new22\/solved-determine-if-a-number-is-prime-closed\/"},"modified":"2023-02-08T04:05:55","modified_gmt":"2023-02-08T04:05:55","slug":"solved-determine-if-a-number-is-prime-closed-2","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/","title":{"rendered":"[Solved] Determine if a number is prime [closed]"},"content":{"rendered":"<h2> Introduction <\/h2>\n<p>[ad_1]<\/p>\n<p>A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Determining if a number is prime is an important problem in mathematics, and there are several methods for solving it. In this article, we will discuss some of the most common methods for determining if a number is prime, including trial division, the Sieve of Eratosthenes, and the Miller-Rabin primality test. We will also discuss the advantages and disadvantages of each method.<\/p>\n<h2> Solution<\/h2>\n<p><\/p>\n<p>\/\/This function will take a number as an argument and return true if it is a prime number, and false if it is not.<\/p>\n<p>function isPrime(num) {<br \/>\n  \/\/check if num is less than 2, if so, it is not a prime number<br \/>\n  if (num < 2) {\n    return false;\n  }\n  \/\/check if num is divisible by any number between 2 and num-1\n  for (let i = 2; i < num; i++) {\n    if (num % i === 0) {\n      return false;\n    }\n  }\n  \/\/if num is not divisible by any number between 2 and num-1, it is a prime number\n  return true;\n} <\/p>\n<p><\/p>\n<div class=\"entry-content\" itemprop=\"text\">\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/p>\n<p><\/p>\n<div id=\"answer-40452946\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"40452946\" data-parentid=\"40452905\" data-score=\"2\" 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>You are missing a bracket. If you keep your code clean it will solve the problem. <\/p>\n<p>Also in your if statement you are checking if <code>number % 2 != 0<\/code>. If it does not equal to 0 then it could potentially be a prime number. So I changed it to <code>number % 2 == 0<\/code> to fix the problem.<\/p>\n<p>The other mistake is that you kept printing even after you knew that the number is not prime. <\/p>\n<pre><code>public static void isPrime(int number)\n    {\n        for ( int i = 2; i&lt;Math.sqrt(number) + 1; i ++)\n        {\n            if ( number%i==0 ) {\/\/If number is not a prime\n                \/\/Will print that the number is not a prime once\n                System.out.println(number +\" is not a prime number.\");\n                return ; \/\/Will exit the method\n            }\n        }\n        \/\/If you reach this point then the number is prime.\n        System.out.println(number +\" is a prime number.\");\n    }\n<\/code><\/pre>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p> <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p>\n<\/div>\n<\/div>\n<p>solved Determine if a number is prime [closed] <\/p>\n<p><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-1088640234840270\" crossorigin=\"anonymous\"><\/script><br \/>\n<script><\/p>\n<p><\/script><\/div>\n<p>[ad_2]<\/p>\n<pre>\n#include &lt;stdio.h&gt;\n\nint isPrime(int n) \n{ \n    \/\/ Corner cases \n    if (n &lt;= 1) \n        return 0; \n    if (n &lt;= 3) \n        return 1; \n  \n    \/\/ This is checked so that we can skip \n    \/\/ middle five numbers in below loop \n    if (n % 2 == 0 || n % 3 == 0) \n        return 0; \n  \n    for (int i = 5; i * i &lt;= n; i = i + 6) \n        if (n % i == 0 || n % (i + 2) == 0) \n            return 0; \n  \n    return 1; \n} \n  \n\/\/ Driver Program to test above function \nint main() \n{ \n    int n;\n    printf(\"Enter a number: \");\n    scanf(\"%d\", &n);\n    if (isPrime(n)) \n        printf(\"%d is a prime number\", n); \n    else\n        printf(\"%d is not a prime number\", n); \n  \n    return 0; \n} \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Introduction [ad_1] A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Determining if a number is prime is an important problem in mathematics, and there are several methods for solving it. In this article, we will discuss some of the most common methods for &#8230; <a title=\"[Solved] Determine if a number is prime [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\" aria-label=\"More on [Solved] Determine if a number is prime [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323],"class_list":["post-187","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Determine if a number is prime [closed] - 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-determine-if-a-number-is-prime-closed-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Determine if a number is prime [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"Introduction [ad_1] A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Determining if a number is prime is an important problem in mathematics, and there are several methods for solving it. In this article, we will discuss some of the most common methods for ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-08T04:05:55+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-determine-if-a-number-is-prime-closed-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Determine if a number is prime [closed]\",\"datePublished\":\"2023-02-08T04:05:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\"},\"wordCount\":251,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\",\"name\":\"[Solved] Determine if a number is prime [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-08T04:05:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Determine if a number is prime [closed]\"}]},{\"@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] Determine if a number is prime [closed] - 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-determine-if-a-number-is-prime-closed-2\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Determine if a number is prime [closed] - JassWeb","og_description":"Introduction [ad_1] A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Determining if a number is prime is an important problem in mathematics, and there are several methods for solving it. In this article, we will discuss some of the most common methods for ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/","og_site_name":"JassWeb","article_published_time":"2023-02-08T04:05:55+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-determine-if-a-number-is-prime-closed-2\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Determine if a number is prime [closed]","datePublished":"2023-02-08T04:05:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/"},"wordCount":251,"commentCount":0,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/","url":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/","name":"[Solved] Determine if a number is prime [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-08T04:05:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-determine-if-a-number-is-prime-closed-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Determine if a number is prime [closed]"}]},{"@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\/187","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=187"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}