{"id":1172,"date":"2020-12-15T08:21:32","date_gmt":"2020-12-15T08:21:32","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1172"},"modified":"2020-12-15T08:21:34","modified_gmt":"2020-12-15T08:21:34","slug":"nth-prime","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/12\/15\/nth-prime\/","title":{"rendered":"Nth Prime"},"content":{"rendered":"\n<p>Prime number is an age old problem. I am trying to solve it via streams.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class PrimeCalculator {\n\n\n    public int nth(int nth) {\n        if (nth &lt;= 0)\n            throw new IllegalArgumentException();\n\n        \/\/ Will skip the nth - 1\n        \/\/ 2 to n-1\n        return IntStream.iterate(2, i -> i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();\n    }\n\n    public static boolean isPrime(int x) {\n        \/\/ till half of x\n        return IntStream.rangeClosed(2, (int) (x\/2) ).dropWhile(i -> x % i != 0).findFirst().isEmpty();\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Explanation<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public int nth(int nth) {\n        if (nth &lt;= 0)\n            throw new IllegalArgumentException();\n\n        \/\/ Will skip the nth - 1\n        \/\/ 2 to n-1\n        return IntStream.iterate(2, i -> i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();\n    }<\/code><\/pre>\n\n\n\n<p>The method <code>nth(int)<\/code> allows you to return the nth prime starting at <code>2, 3, 5....<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the <code>nth<\/code> is negative or l.t.e 0 return an illegal argument exception.<\/li><li>Else, Iterate 2 onwards<\/li><li>Filter all the elements that are prime.<\/li><li>Skip all <code>n - 1<\/code> elements. <\/li><li>Find first and return it as int.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>static &lt;T> Stream&lt;T> iterate(T seed,\n                             UnaryOperator&lt;T> f)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Returns an infinite sequential ordered\u00a0<code>Stream<\/code>\u00a0produced by iterative application of a function\u00a0<code>f<\/code>\u00a0to an initial element\u00a0<code>seed<\/code>.<\/p><\/blockquote><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>Stream&lt;T> skip(long n)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-luminous-vivid-orange-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>Skip is a stateful intermediate operation.<\/p><\/blockquote><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>`Skip()` Returns a stream consisting of the remaining elements of this stream after discarding the first\u00a0<code>n<\/code>\u00a0elements of the stream. If this stream contains fewer than\u00a0<code>n<\/code>\u00a0elements then an empty stream will be returned.<\/p><cite>from java docs<\/cite><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>Optional&lt;T> findFirst()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-pullquote has-background has-vivid-green-cyan-background-color is-style-solid-color\"><blockquote class=\"has-text-color has-white-color\"><p>findFirst is a short-circuiting terminal operation<\/p><cite>java docs<\/cite><\/blockquote><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>An intermediate operation is short-circuiting if, when presented with infinite input, it may produce a finite stream as a result. A terminal operation is short-circuiting if, when presented with infinite input, it may terminate in finite time.<\/p><cite>java docs<\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Helpers<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/stream\/Stream.html<\/li><li>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/stream\/Stream.html#iterate-T-java.util.function.UnaryOperator-<\/li><li>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/stream\/Stream.html#skip-long-<\/li><li>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/stream\/package-summary.html#StreamOps<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Prime number is an age old problem. I am trying to solve it via streams. Explanation The method nth(int) allows you to return the nth prime starting at 2, 3, 5&#8230;. If the nth is negative or l.t.e 0 return an illegal argument exception. Else, Iterate 2 onwards Filter all the elements that are prime. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1174,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[120,34],"tags":[121,128,122],"class_list":["post-1172","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-java","category-technical","tag-java","tag-prime","tag-streams","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1172"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1172\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1174"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}