{"id":33678,"date":"2024-11-01T09:19:15","date_gmt":"2024-11-01T09:19:15","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33678"},"modified":"2024-11-01T11:47:09","modified_gmt":"2024-11-01T11:47:09","slug":"python-coding-test-course-finding-prime-numbers","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33678\/","title":{"rendered":"Python Coding Test Course, Finding Prime Numbers"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Problem Description<\/h2>\n<p>The task is to find all prime numbers that are less than or equal to a given number N. A prime number is an integer that has only two divisors: 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.<\/p>\n<h2>Problem Definition<\/h2>\n<p>Write a program to find prime numbers that satisfy the following conditions.<\/p>\n<pre>\n        Function name: find_primes\n        Input: Integer N (2 \u2264 N \u2264 10^6)\n        Output: Returns a list of all prime numbers less than or equal to N\n    <\/pre>\n<h2>2. Approach<\/h2>\n<p>One of the most commonly used methods to find prime numbers is an algorithm known as the &#8216;Sieve of Eratosthenes&#8217;. This algorithm is an efficient way to find all prime numbers within a given range, and it proceeds through the following steps.<\/p>\n<ol>\n<li>Create a list containing integers from 2 to N.<\/li>\n<li>Remove all multiples of 2 from the list.<\/li>\n<li>Repeat the same process for the next remaining number. (In other words, remove all multiples of the current number)<\/li>\n<li>Repeat this until all numbers up to N have been processed.<\/li>\n<li>The numbers that remain until the end are prime numbers.<\/li>\n<\/ol>\n<h2>3. Algorithm Implementation<\/h2>\n<p>Now, let\u2019s implement the Sieve of Eratosthenes algorithm described above in Python. Below is the complete code.<\/p>\n<pre>\ndef find_primes(N):\n    # Exclude 0 and 1 as they are not prime numbers.\n    if N < 2:\n        return []\n    \n    # Initialize the list for finding primes\n    sieve = [True] * (N + 1)  # Initialize all numbers as potential primes\n    sieve[0], sieve[1] = False, False  # 0 and 1 are not primes\n\n    for i in range(2, int(N**0.5) + 1):  # Proceed from 2 up to sqrt(N)\n        if sieve[i]:  # If i is prime\n            for j in range(i * i, N + 1, i):  # Set multiples of i to False\n                sieve[j] = False\n\n    # Create a list of primes\n    primes = [i for i, is_prime in enumerate(sieve) if is_prime]\n    return primes\n\n# Test\nN = 100\nprint(find_primes(N))  # Output: [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]\n    <\/pre>\n<h2>4. Code Explanation<\/h2>\n<p>The code works as follows:<\/p>\n<ol>\n<li>First, a list called 'sieve' is created, and an array of size N+1 is initialized to True. In this array, the index represents the numbers, and a value of True indicates that the corresponding number is prime.<\/li>\n<li>Since 0 and 1 are not prime numbers, these two indices are set to False.<\/li>\n<li>A loop is executed from 2 to sqrt(N). In this process, the current number i is checked for primality (sieve[i] is True). If it is prime, all multiples of i are marked as False.<\/li>\n<li>After all checks are completed, the indices that still have True values are collected into a list and returned as prime numbers.<\/li>\n<\/ol>\n<h2>5. Performance Analysis<\/h2>\n<p>The Sieve of Eratosthenes algorithm has a complexity of O(n log log n), which is quite efficient. Therefore, it can find prime numbers quickly even for the given condition of N \u2264 10^6.<\/p>\n<h2>6. Conclusion<\/h2>\n<p>The problem of finding prime numbers can be solved using various algorithms, but the Sieve of Eratosthenes is one of the most efficient and intuitive methods among them. By utilizing this algorithm, you can solve various problems, so be sure to master it!<\/p>\n<h2>7. Additional References<\/h2>\n<p>If you want to study more in-depth algorithms, the following resources are recommended:<\/p>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Sieve_of_Eratosthenes\">Wikipedia - Sieve of Eratosthenes<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/sieve-of-eratosthenes-using-python\/\">GeeksforGeeks - Sieve of Eratosthenes in Python<\/a><\/li>\n<li><a href=\"https:\/\/www.hackerrank.com\/\">HackerRank - Various Algorithm Challenges<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Problem Description The task is to find all prime numbers that are less than or equal to a given number N. A prime number is an integer that has only two divisors: 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers. Problem Definition Write a program to find &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33678\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python 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":[145],"tags":[],"class_list":["post-33678","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python 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\/33678\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Problem Description The task is to find all prime numbers that are less than or equal to a given number N. A prime number is an integer that has only two divisors: 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers. Problem Definition Write a program to find &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Finding Prime Numbers&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33678\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:19:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:09+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33678\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33678\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Finding Prime Numbers\",\"datePublished\":\"2024-11-01T09:19:15+00:00\",\"dateModified\":\"2024-11-01T11:47:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33678\/\"},\"wordCount\":403,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33678\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33678\/\",\"name\":\"Python Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:19:15+00:00\",\"dateModified\":\"2024-11-01T11:47:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33678\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33678\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33678\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python 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":"Python 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\/33678\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Problem Description The task is to find all prime numbers that are less than or equal to a given number N. A prime number is an integer that has only two divisors: 1 and itself. For example, 2, 3, 5, 7, 11, and 13 are prime numbers. Problem Definition Write a program to find &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Finding Prime Numbers\"","og_url":"https:\/\/atmokpo.com\/w\/33678\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:19:15+00:00","article_modified_time":"2024-11-01T11:47:09+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33678\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33678\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Finding Prime Numbers","datePublished":"2024-11-01T09:19:15+00:00","dateModified":"2024-11-01T11:47:09+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33678\/"},"wordCount":403,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33678\/","url":"https:\/\/atmokpo.com\/w\/33678\/","name":"Python Coding Test Course, Finding Prime Numbers - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:19:15+00:00","dateModified":"2024-11-01T11:47:09+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33678\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33678\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33678\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python 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\/33678","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=33678"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33678\/revisions"}],"predecessor-version":[{"id":33679,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33678\/revisions\/33679"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}