{"id":9035,"date":"2022-10-07T22:32:53","date_gmt":"2022-10-07T21:32:53","guid":{"rendered":"https:\/\/stevepedwards.today\/DebianAdmin\/?p=9035"},"modified":"2022-10-10T21:44:00","modified_gmt":"2022-10-10T20:44:00","slug":"equivalent-prime-numbers-programs-for-java-python-c-and-jscript-testing","status":"publish","type":"post","link":"https:\/\/stevepedwards.today\/DebianAdmin\/equivalent-prime-numbers-programs-for-java-python-c-and-jscript-testing\/","title":{"rendered":"Equivalent Prime Numbers Programs for Java, Python, C and JScript Testing"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9035\" class=\"pvc_stats all  \" data-element-id=\"9035\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>I did a post a long way back on Primes and Erastothenes Sieve not knowing how \"equivalent\" the code is between languages, but now I have 4 version that seem identical as can be.<\/p>\n<p>I intend to test them speedwise on my much newer, super powerful Dell Inspiron 16 7610, that sports 16 processors and 16GB RAM.<\/p>\n<p>A very basic test for each in VScode was surprising. The main issue being disimilar functioning of the way python runs without an if\/break\/else line, as it really doesn't like the iteration process the others handle ok - it takes about 7+ minutes to run this way once at 100k primes or above:<\/p>\n<pre class=\"lang:default decode:true \">upper = 100\r\nfor i in range(2, upper + 1):\r\n    a = 0\r\n    for j in range(2, i):\r\n\r\n        if (i % j) == 0:\r\n            a = 1\r\n            #         break\r\n            # else:\r\n    if (a == 0):\r\n        print(i, end=' ')\r\nprint(\" Done\")<\/pre>\n<p>From hitting the run button for each against a clock 2nd hand, so including any VSCode compilation that needs doing, gave the following results to print primes up to 100,000:<\/p>\n<p>C: 8 seconds<\/p>\n<pre class=\"lang:default decode:true\">#include &lt;stdio.h&gt;\r\nint main()\r\n{\r\n    int i, j, a, input;\r\n    for (i = 2; i &lt; 100000; i = i + 1)\r\n    {\r\n        a = 0;\r\n        for (j = 2; j &lt; i; j = j + 1)\r\n            if (i % j == 0)\r\n                a = 1;\r\n        if (a == 0)\r\n            printf(\"%d \", (i));\r\n    }\r\n    printf(\" Done\\n\");\r\n    return 0;\r\n}<\/pre>\n<p>JavaScript: (node command line) 11\u00a0 seconds<\/p>\n<pre class=\"lang:default decode:true\">for (i = 2; i &lt; 100000; i = i + 1) {\r\n  a = 0;\r\n  for (j = 2; j &lt; i; j = j + 1) if (i % j == 0) a = 1;\r\n  if (a == 0) console.log(i);\r\n}\r\n<\/pre>\n<p>Python: 22 seconds<\/p>\n<pre class=\"lang:default decode:true\">upper = 100000\r\nfor i in range(2, upper + 1):\r\n    for j in range(2, i):\r\n        if (i % j) == 0:\r\n            break\r\n    else:\r\n        print(i, end=' ')\r\nprint(\" Done\")\r\n<\/pre>\n<p>Java: 8 seconds<\/p>\n<pre class=\"lang:default decode:true \">public class primes {\r\n    public static void main(String args[]) {\r\n        int i, j, a;\r\n        int limit = 100000;\r\n        for (i = 2; i &lt;= limit; i = i + 1) {\r\n            a = 0;\r\n            for (j = 2; j &lt; i; j = j + 1)\r\n                if (i % j == 0)\r\n                    a = 1;\r\n            if (a == 0)\r\n                System.out.print(\" \" + i);\r\n        }\r\n        System.out.print(\" Done\\n\");\r\n    }\r\n}<\/pre>\n<p>These programs are very inefficient because they re-run the division by 2 Prime check as each new prime is found, so waste more and more cycles as the amount of primes found increases.<\/p>\n<p>A more efficient algorithmn is Erastothenes Sieve, explained here:<\/p>\n<p><a href=\"https:\/\/pi3.sites.sheffield.ac.uk\/tutorials\/week-3-prime-numbers\">https:\/\/pi3.sites.sheffield.ac.uk\/tutorials\/week-3-prime-numbers<\/a><\/p>\n<p>Python redeems itself with primes up to 1M printed in 13 seconds.using the Sieve method, not having to start the divisor loop from 2 each time, only continuing on from where the last prime was found - massive saving of wasted re-iterations.<\/p>\n<p>Now, I just have to transpose a Sieve version for the other langs...if my rudimentary programming abilities allow...in the meantime here's some non equivalent Sieve times up to 1M primes for Java and JS and Python.<\/p>\n<p>C: 4 secs<\/p>\n<p>999983<br \/>\nThere were 78498 primes up to 1000000<\/p>\n<pre class=\"lang:default decode:true \">#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\n#define N 100000\r\n\r\nint main()\r\n{\r\n    int num[N], i, j;\r\n    int limit = sqrt(N);\r\n\r\n    for (i = 0; i &lt; N; i++)\r\n        num[i] = i + 1;\r\n\r\n    for (i = 1; i &lt;= limit; i++)\r\n    {\r\n        if (num[i] != 0)\r\n        {\r\n            for (j = pow(num[i], 2); j &lt;= N; j = j + num[i])\r\n            {\r\n                num[j - 1] = 0;\r\n            }\r\n        }\r\n    }\r\n\r\n    printf(\"Sieve of Eratosthenes Method\\n\");\r\n    printf(\"To find Prime numbers from 2 to %d\\n\\n\", N);\r\n    for (i = 1; i &lt; N; i++)\r\n    {\r\n        if (num[i] != 0)\r\n            printf(\"%d \", num[i]);\r\n    }\r\n\r\n    printf(\"\\n\");\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Python:\u00a0 16 sec<\/p>\n<pre class=\"lang:default decode:true\">def apply_sieve(n):\r\n    a = [1]*(n+1)  \r\n    a[0] = 0  \r\n    a[1] = 0  \r\n    p = 2       \r\n    pmax = int(round(n**0.5)) + 1\r\n    while p &lt; pmax:\r\n        while a[p] == 0:\r\n            p += 1\r\n        j = 2*p\r\n        while j &lt; n:\r\n            a[j] = 0\r\n            j += p\r\n        p += 1\r\n    return [p for p in range(n+1) if a[p] == 1]\r\nN = 1000000 \r\nprimes = apply_sieve(N)\r\nprint(primes)\r\n<\/pre>\n<p>JS:\u00a0 21 secs<\/p>\n<pre class=\"lang:default decode:true\">const num = 100000;\r\nconst findPrimes = (num = 10) =&gt; {\r\n  const numArr = new Array(num + 1);\r\n  numArr.fill(true);\r\n  numArr[0] = numArr[1] = false;\r\n  for (let i = 2; i &lt;= Math.sqrt(num); i++) {\r\n    for (let j = 2; i * j &lt;= num; j++) {\r\n      numArr[i * j] = false;\r\n    }\r\n  }\r\n  return numArr.reduce((acc, val, ind) =&gt; {\r\n    if (val) {\r\n      return acc.concat(ind);\r\n    } else {\r\n      return acc;\r\n    }\r\n  }, []);\r\n};\r\nconsole.log(findPrimes(num));\r\n<\/pre>\n<p><span style=\"color: #ff0000;\">541,<\/span><br \/>\n<span style=\"color: #ff0000;\">... 78398 more items<\/span><\/p>\n<p>Java: 6 secs<\/p>\n<pre class=\"lang:default decode:true \">\/**\r\n * Implementation of the sieve of Eratosthenes for finding all the primes up to\r\n * a given number (MAX in this case).\r\n * From the command line:\r\n *   Step 1 (compile): javac PrimesSieve.java\r\n *   Step 2 (run):     java PrimesSieve\r\n *\/\r\npublic class PrimeSieve {\r\n    public static void main(String[] args) {\r\n      final int MAX = 1_000_000;\r\n      \/\/ Create an array of boolean values indicating whether a number is prime.\r\n      \/\/ Start by assuming all numbers are prime by setting them to true. \r\n      boolean[] primes = new boolean[MAX];\r\n      for (int i=0; i&lt;primes.length; i++) {\r\n        primes[i] = true;\r\n      }\r\n  \r\n      \/\/ Loop through a portion of the array (up to the square root of MAX). If\r\n      \/\/ it's a prime, ensure all multiples of it are set to false, as they\r\n      \/\/ clearly cannot be prime.\r\n      for (int i=2; i&lt;Math.sqrt(MAX)+1; i++) {\r\n        if (primes[i-1]) {\r\n          for (int j=(int) Math.pow(i,2); j&lt;=MAX; j+=i) {\r\n            primes[j - 1] = false;\r\n          }\r\n        }\r\n      }\r\n  \r\n      \/\/ Output the results\r\n      int count = 0;\r\n      for (int i = 2; i &lt; MAX; i++) {\r\n        if (primes[i - 1]) {\r\n          System.out.println(i);\r\n          count++;\r\n        }\r\n      }\r\n      System.out.printf(\"There were %d primes up to %d\", count, MAX);\r\n    }\r\n  }<\/pre>\n<p><span style=\"color: #ff0000;\">999983<\/span><br \/>\n<span style=\"color: #ff0000;\">There were 78498 primes up to 1000000<\/span><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9035\" class=\"pvc_stats all  \" data-element-id=\"9035\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>I did a post a long way back on Primes and Erastothenes Sieve not knowing how \"equivalent\" the code is between languages, but now I have 4 version that seem identical as can be. I intend to test them speedwise on my much newer, super powerful Dell Inspiron 16 7610, that sports 16 processors and <a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/equivalent-prime-numbers-programs-for-java-python-c-and-jscript-testing\/\" class=\"more-link\">...<span class=\"screen-reader-text\">\u00a0 Equivalent Prime Numbers Programs for Java, Python, C and JScript Testing<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-9035","post","type-post","status-publish","format-standard","hentry","category-post"],"a3_pvc":{"activated":true,"total_views":2,"today_views":0},"_links":{"self":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/9035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/comments?post=9035"}],"version-history":[{"count":34,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/9035\/revisions"}],"predecessor-version":[{"id":9082,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/9035\/revisions\/9082"}],"wp:attachment":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/media?parent=9035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/categories?post=9035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/tags?post=9035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}