{"id":33404,"date":"2024-11-01T09:16:12","date_gmt":"2024-11-01T09:16:12","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33404"},"modified":"2024-11-01T11:38:50","modified_gmt":"2024-11-01T11:38:50","slug":"java-coding-test-course-finding-prime-numbers","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33404\/","title":{"rendered":"Java Coding Test Course, Finding Prime Numbers"},"content":{"rendered":"<p><body><\/p>\n<article>\n<section>\n<h2>1. Problem Definition<\/h2>\n<p>\n                Many coding test problems require mathematical concepts or algorithmic thinking.<br \/>\n                One of them is the problem of &#8220;Finding Prime Numbers.&#8221;<br \/>\n                A prime number is defined as a natural number that has only two divisors: 1 and itself.<br \/>\n                In other words, a prime number is a natural number greater than 1 that does not have any divisors other than 1 and itself.<br \/>\n                For example, 2, 3, 5, 7, 11, 13, 17, etc., are prime numbers.\n            <\/p>\n<p>\n                In this course, we will solve the problem of printing all prime numbers less than or equal to a given integer N.<br \/>\n                Specifically, the problem becomes &#8220;Print all prime numbers less than or equal to the given integer N.&#8221;\n            <\/p>\n<h2>2. Problem Example<\/h2>\n<p>\n<strong>Example Input:<\/strong> <code>20<\/code><br \/>\n<strong>Example Output:<\/strong> <code>2, 3, 5, 7, 11, 13, 17, 19<\/code>\n<\/p>\n<h2>3. Algorithm Approach<\/h2>\n<p>\n                There are several ways to find prime numbers.<br \/>\n                The simplest method is to check each number from 2 to N to determine if it is prime.<br \/>\n                However, this method is inefficient and has a time complexity of O(N^2).<br \/>\n                Therefore, we will use a more efficient method, namely the <strong>Sieve of Eratosthenes<\/strong> algorithm.\n            <\/p>\n<h3>3.1. Sieve of Eratosthenes Algorithm<\/h3>\n<p>\n                The Sieve of Eratosthenes is a classical algorithm for finding prime numbers. The basic idea of this method is as follows:\n            <\/p>\n<ol>\n<li>List all numbers from 2 to N.<\/li>\n<li>Select the smallest number, which is 2. Erase all multiples of 2.<\/li>\n<li>Select the next smallest remaining number. This number is prime.<\/li>\n<li>Erase all multiples of this number.<\/li>\n<li>Repeat this process until there are no primes left among the numbers less than or equal to N.<\/li>\n<\/ol>\n<h2>4. Java Code Implementation<\/h2>\n<p>\n                Now, let\u2019s implement the Sieve of Eratosthenes algorithm in Java.<br \/>\n                Below is the implementation code:\n            <\/p>\n<pre><code>\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class PrimeNumbers {\n    public static void main(String[] args) {\n        int N = 20; \/\/ Here, you can change the value of N to find primes in different ranges.\n        List&lt;Integer&gt; primes = findPrimes(N);\n        System.out.println(\"Primes (\" + N + \" or less): \" + primes);\n    }\n\n    public static List&lt;Integer&gt; findPrimes(int N) {\n        boolean[] isPrime = new boolean[N + 1];\n        List&lt;Integer&gt; primes = new ArrayList&lt;&gt;();\n\n        \/\/ Initialize false for 0 and 1 as they are not prime\n        for (int i = 2; i &lt;= N; i++) {\n            isPrime[i] = true;\n        }\n\n        for (int i = 2; i * i &lt;= N; i++) {\n            if (isPrime[i]) {\n                for (int j = i * i; j &lt;= N; j += i) {\n                    isPrime[j] = false; \/\/ Exclude multiples of i from prime candidates\n                }\n            }\n        }\n\n        for (int i = 2; i &lt;= N; i++) {\n            if (isPrime[i]) {\n                primes.add(i); \/\/ Add to the list of primes\n            }\n        }\n        return primes;\n    }\n}\n            <\/code><\/pre>\n<h2>5. Code Explanation<\/h2>\n<p>\n                The above code finds prime numbers through the following steps:\n            <\/p>\n<ul>\n<li><strong>Array Initialization:<\/strong> It creates a boolean array called <code>isPrime<\/code><br \/>\n                to store whether numbers from 0 to N are prime. Initially, all numbers are assumed to be prime.<\/li>\n<li><strong>Prime Determination:<\/strong> It uses nested loops to erase multiples of each number. The outer loop represents the candidate prime numbers,<br \/>\n                and the inner loop erases the multiples of the prime.<\/li>\n<li><strong>Prime List Generation:<\/strong> Finally, after determining the primes,<br \/>\n                it checks the <code>isPrime<\/code> array to add actual prime numbers to the list.<\/li>\n<\/ul>\n<h2>6. Code Execution Result Explanation<\/h2>\n<p>\n                When the above code is executed, it prints the list of prime numbers for the given value of N. For example, if N is 20, the output will be:<br \/>\n                <code>Primes (20 or less): [2, 3, 5, 7, 11, 13, 17, 19]<\/code>\n<\/p>\n<h2>7. Performance Optimization<\/h2>\n<p>\n                The Sieve of Eratosthenes algorithm has a time complexity of O(N log log N), making it very efficient.<br \/>\n                However, when N is very large, memory usage must also be considered.<br \/>\n                Therefore, here are some methods to save memory:\n            <\/p>\n<ul>\n<li>Since all even numbers except for 2 are not prime, we can search for primes only among odd numbers.<\/li>\n<li>Instead of using an array, we can use a bit vector to reduce memory usage.<\/li>\n<li>By exploiting symmetry, we only need to search up to the square root of N. That is, we can perform the work of identifying non-prime numbers only up to the square root,<br \/>\n                as the remaining numbers will not be prime, so there is no need to store them.<\/li>\n<\/ul>\n<h2>8. Conclusion<\/h2>\n<p>\n                In this course, we learned how to find prime numbers using Java.<br \/>\n                The Sieve of Eratosthenes algorithm is extremely efficient for finding prime numbers,<br \/>\n                and understanding such algorithms will be helpful in coding tests.<br \/>\n                Additionally, understanding various data structures and algorithms, as well as basic mathematical facts that can assist in solving multiple high-level algorithm problems, is important.\n            <\/p>\n<p>\n                In the next session, we will address a wider variety of algorithm problems.<br \/>\n                I hope this will help you prepare for coding tests!\n            <\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Problem Definition Many coding test problems require mathematical concepts or algorithmic thinking. One of them is the problem of &#8220;Finding Prime Numbers.&#8221; A prime number is defined as a natural number that has only two divisors: 1 and itself. In other words, a prime number is a natural number greater than 1 that does &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33404\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Finding Prime Numbers&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[139],"tags":[],"class_list":["post-33404","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/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:\/\/atmokpo.com\/w\/33404\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Problem Definition Many coding test problems require mathematical concepts or algorithmic thinking. One of them is the problem of &#8220;Finding Prime Numbers.&#8221; A prime number is defined as a natural number that has only two divisors: 1 and itself. In other words, a prime number is a natural number greater than 1 that does &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Finding Prime Numbers&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33404\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:16:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:38:50+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33404\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33404\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Finding Prime Numbers\",\"datePublished\":\"2024-11-01T09:16:12+00:00\",\"dateModified\":\"2024-11-01T11:38:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33404\/\"},\"wordCount\":589,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33404\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33404\/\",\"name\":\"Java Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:16:12+00:00\",\"dateModified\":\"2024-11-01T11:38:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33404\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33404\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33404\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Finding Prime Numbers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","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:\/\/atmokpo.com\/w\/33404\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Problem Definition Many coding test problems require mathematical concepts or algorithmic thinking. One of them is the problem of &#8220;Finding Prime Numbers.&#8221; A prime number is defined as a natural number that has only two divisors: 1 and itself. In other words, a prime number is a natural number greater than 1 that does &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Finding Prime Numbers\"","og_url":"https:\/\/atmokpo.com\/w\/33404\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:16:12+00:00","article_modified_time":"2024-11-01T11:38:50+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33404\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33404\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Finding Prime Numbers","datePublished":"2024-11-01T09:16:12+00:00","dateModified":"2024-11-01T11:38:50+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33404\/"},"wordCount":589,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33404\/","url":"https:\/\/atmokpo.com\/w\/33404\/","name":"Java Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:16:12+00:00","dateModified":"2024-11-01T11:38:50+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33404\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33404\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33404\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Finding Prime Numbers"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=33404"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33404\/revisions"}],"predecessor-version":[{"id":33405,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33404\/revisions\/33405"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}