diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-01-21 18:00:17 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-01-21 18:00:17 +0000 |
| commit | e7b355008ec9f641223eb4165cc1d0452216040d (patch) | |
| tree | 99a649201480db1d2e28282627255b317826f2db | |
| parent | ef6299c2c9becccf7164d42468ed3112f613f9c7 (diff) | |
| download | perlweeklychallenge-club-e7b355008ec9f641223eb4165cc1d0452216040d.tar.gz perlweeklychallenge-club-e7b355008ec9f641223eb4165cc1d0452216040d.tar.bz2 perlweeklychallenge-club-e7b355008ec9f641223eb4165cc1d0452216040d.zip | |
- Added solutions by Eric Cheung.
- Added solutions by Richar Park.
- Added solutions by PokGoPun.
- Added solutions by Neils van Dijke.
- Added solutions by Mark Anderson.
- Added solutions by E. Choroba.
- Added solutions by Luca Ferrari.
- Added solutions by Peter Campbell Smith.
- Added solutions by Conor Hoekstra.
- Added solutions by David Ferrone.
- Added solutions by Robbie Hatley.
- Added solutions by W. Luis Mochan.
- Added solutions by Feng Chang.
- Added solutions by Roger Bell_West.
| -rw-r--r-- | challenge-305/conor-hoekstra/bqn/ch-1.bqn (renamed from challenge-305/conor-hoekstra/ch-1.bqn) | 0 | ||||
| -rw-r--r-- | challenge-305/conor-hoekstra/bqn/ch-2.bqn (renamed from challenge-305/conor-hoekstra/ch-2.bqn) | 0 | ||||
| -rwxr-xr-x | challenge-305/eric-cheung/python/ch-1.py | 16 | ||||
| -rwxr-xr-x | challenge-305/eric-cheung/python/ch-2.py | 34 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-1.txt (renamed from challenge-306/luca-ferrari/blog-1.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-10.txt (renamed from challenge-306/luca-ferrari/blog-10.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-2.txt (renamed from challenge-306/luca-ferrari/blog-2.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-3.txt (renamed from challenge-306/luca-ferrari/blog-3.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-4.txt (renamed from challenge-306/luca-ferrari/blog-4.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-5.txt (renamed from challenge-306/luca-ferrari/blog-5.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-6.txt (renamed from challenge-306/luca-ferrari/blog-6.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-7.txt (renamed from challenge-306/luca-ferrari/blog-7.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-8.txt (renamed from challenge-306/luca-ferrari/blog-8.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/blog-9.txt (renamed from challenge-306/luca-ferrari/blog-9.txt) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/pljava/pom.xml (renamed from challenge-306/luca-ferrari/pljava/pom.xml) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/pljava/src/main/java/Task1.java (renamed from challenge-306/luca-ferrari/pljava/src/main/java/Task1.java) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/pljava/src/main/java/Task2.java (renamed from challenge-306/luca-ferrari/pljava/src/main/java/Task2.java) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/plperl/ch-1.plperl (renamed from challenge-306/luca-ferrari/plperl/ch-1.plperl) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/plperl/ch-2.plperl (renamed from challenge-306/luca-ferrari/plperl/ch-2.plperl) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/plpgsql/ch-1.sql (renamed from challenge-306/luca-ferrari/plpgsql/ch-1.sql) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/plpgsql/ch-2.sql (renamed from challenge-306/luca-ferrari/plpgsql/ch-2.sql) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/python/ch-1.py (renamed from challenge-306/luca-ferrari/python/ch-1.py) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/python/ch-2.py (renamed from challenge-306/luca-ferrari/python/ch-2.py) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/raku/ch-1.raku (renamed from challenge-306/luca-ferrari/raku/ch-1.raku) | 0 | ||||
| -rw-r--r-- | challenge-305/luca-ferrari/raku/ch-2.raku (renamed from challenge-306/luca-ferrari/raku/ch-2.raku) | 0 | ||||
| -rwxr-xr-x | challenge-305/perlboy1967/perl/ch-1.pl (renamed from challenge-305/perlboy1967/perl/ch1.pl) | 0 | ||||
| -rwxr-xr-x | challenge-305/perlboy1967/perl/ch-2.pl (renamed from challenge-305/perlboy1967/perl/ch2.pl) | 0 | ||||
| -rw-r--r-- | challenge-305/richard-park/apl/AlienDictionary.aplf | 7 | ||||
| -rw-r--r-- | challenge-305/richard-park/apl/BinaryPrefix.aplf | 6 | ||||
| -rw-r--r-- | challenge-305/richard-park/apl/W305_APL.ipynb | 450 | ||||
| -rw-r--r-- | challenge-305/zapwai/perl/ch-1.pl (renamed from challenge-305/zapwai/ch-1.pl) | 0 | ||||
| -rw-r--r-- | challenge-305/zapwai/perl/ch-2.pl (renamed from challenge-305/zapwai/ch-2.pl) | 0 | ||||
| -rw-r--r-- | stats/pwc-challenge-304.json | 483 | ||||
| -rw-r--r-- | stats/pwc-current.json | 473 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2019.json | 612 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2020.json | 376 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2021.json | 758 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2022.json | 398 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2023.json | 396 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2024.json | 416 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-2025.json | 113 | ||||
| -rw-r--r-- | stats/pwc-language-breakdown-summary.json | 52 | ||||
| -rw-r--r-- | stats/pwc-leaders.json | 478 | ||||
| -rw-r--r-- | stats/pwc-summary-1-30.json | 100 | ||||
| -rw-r--r-- | stats/pwc-summary-121-150.json | 44 | ||||
| -rw-r--r-- | stats/pwc-summary-151-180.json | 112 | ||||
| -rw-r--r-- | stats/pwc-summary-181-210.json | 110 | ||||
| -rw-r--r-- | stats/pwc-summary-211-240.json | 38 | ||||
| -rw-r--r-- | stats/pwc-summary-241-270.json | 42 | ||||
| -rw-r--r-- | stats/pwc-summary-271-300.json | 96 | ||||
| -rw-r--r-- | stats/pwc-summary-301-330.json | 34 | ||||
| -rw-r--r-- | stats/pwc-summary-31-60.json | 112 | ||||
| -rw-r--r-- | stats/pwc-summary-61-90.json | 114 | ||||
| -rw-r--r-- | stats/pwc-summary-91-120.json | 30 | ||||
| -rw-r--r-- | stats/pwc-summary.json | 710 | ||||
| -rw-r--r-- | stats/pwc-yearly-language-summary.json | 154 |
56 files changed, 3759 insertions, 3005 deletions
diff --git a/challenge-305/conor-hoekstra/ch-1.bqn b/challenge-305/conor-hoekstra/bqn/ch-1.bqn index 311c35c960..311c35c960 100644 --- a/challenge-305/conor-hoekstra/ch-1.bqn +++ b/challenge-305/conor-hoekstra/bqn/ch-1.bqn diff --git a/challenge-305/conor-hoekstra/ch-2.bqn b/challenge-305/conor-hoekstra/bqn/ch-2.bqn index 67b1ac5aa0..67b1ac5aa0 100644 --- a/challenge-305/conor-hoekstra/ch-2.bqn +++ b/challenge-305/conor-hoekstra/bqn/ch-2.bqn diff --git a/challenge-305/eric-cheung/python/ch-1.py b/challenge-305/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..39344cda93 --- /dev/null +++ b/challenge-305/eric-cheung/python/ch-1.py @@ -0,0 +1,16 @@ +
+from sympy import isprime
+
+## arrBinary = [1, 0, 1] ## Example 1
+## arrBinary = [1, 1, 0] ## Example 2
+arrBinary = [1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1] ## Example 3
+
+arrStrBinary = [str(nElem) for nElem in arrBinary]
+arrOutput = [False]
+
+## for nIndx in range(1, len(arrStrBinary)):
+ ## arrOutput.append(isprime(int("".join(arrStrBinary[ : nIndx + 1]), 2)))
+
+arrOutput = arrOutput + [isprime(int("".join(arrStrBinary[ : nIndx + 1]), 2)) for nIndx in range(1, len(arrStrBinary))]
+
+print (arrOutput)
diff --git a/challenge-305/eric-cheung/python/ch-2.py b/challenge-305/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..91237db3ca --- /dev/null +++ b/challenge-305/eric-cheung/python/ch-2.py @@ -0,0 +1,34 @@ +
+def IsStrGreater(strInput_01, strInput_02):
+ global arrAlien
+ if strInput_01 == strInput_02:
+ return False
+
+ nMinLen = min(len(strInput_01), len(strInput_02))
+
+ if strInput_01[:nMinLen] == strInput_02[:nMinLen]:
+ return len(strInput_01) > len(strInput_02)
+
+ for nIndx in range(nMinLen):
+ if strInput_01[nIndx] == strInput_02[nIndx]:
+ continue
+ return arrAlien.index(strInput_01[nIndx]) > arrAlien.index(strInput_02[nIndx])
+
+## Example 1
+## arrWords = ["perl", "python", "raku"]
+## arrAlien = ["h", "l", "a", "b", "y", "d", "e", "f", "g", "i", "r", "k", "m", "n", "o", "p", "q", "j", "s", "t", "u", "v", "w", "x", "c", "z"]
+
+## Example 2
+arrWords = ["the", "weekly", "challenge"]
+arrAlien = ["c", "o", "r", "l", "d", "a", "b", "t", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "s", "w", "u", "v", "x", "y", "z"]
+
+for nRowLoop in range(len(arrWords) - 1):
+ for nColLoop in range(nRowLoop + 1, len(arrWords)):
+ if not IsStrGreater(arrWords[nRowLoop], arrWords[nColLoop]):
+ continue
+
+ strTemp = arrWords[nRowLoop]
+ arrWords[nRowLoop] = arrWords[nColLoop]
+ arrWords[nColLoop] = strTemp
+
+print (arrWords)
diff --git a/challenge-306/luca-ferrari/blog-1.txt b/challenge-305/luca-ferrari/blog-1.txt index a11f262a59..a11f262a59 100644 --- a/challenge-306/luca-ferrari/blog-1.txt +++ b/challenge-305/luca-ferrari/blog-1.txt diff --git a/challenge-306/luca-ferrari/blog-10.txt b/challenge-305/luca-ferrari/blog-10.txt index efa146d6f9..efa146d6f9 100644 --- a/challenge-306/luca-ferrari/blog-10.txt +++ b/challenge-305/luca-ferrari/blog-10.txt diff --git a/challenge-306/luca-ferrari/blog-2.txt b/challenge-305/luca-ferrari/blog-2.txt index 4b511d6565..4b511d6565 100644 --- a/challenge-306/luca-ferrari/blog-2.txt +++ b/challenge-305/luca-ferrari/blog-2.txt diff --git a/challenge-306/luca-ferrari/blog-3.txt b/challenge-305/luca-ferrari/blog-3.txt index ff43738b0f..ff43738b0f 100644 --- a/challenge-306/luca-ferrari/blog-3.txt +++ b/challenge-305/luca-ferrari/blog-3.txt diff --git a/challenge-306/luca-ferrari/blog-4.txt b/challenge-305/luca-ferrari/blog-4.txt index 94da3eec7e..94da3eec7e 100644 --- a/challenge-306/luca-ferrari/blog-4.txt +++ b/challenge-305/luca-ferrari/blog-4.txt diff --git a/challenge-306/luca-ferrari/blog-5.txt b/challenge-305/luca-ferrari/blog-5.txt index f1f5420190..f1f5420190 100644 --- a/challenge-306/luca-ferrari/blog-5.txt +++ b/challenge-305/luca-ferrari/blog-5.txt diff --git a/challenge-306/luca-ferrari/blog-6.txt b/challenge-305/luca-ferrari/blog-6.txt index a3c426291c..a3c426291c 100644 --- a/challenge-306/luca-ferrari/blog-6.txt +++ b/challenge-305/luca-ferrari/blog-6.txt diff --git a/challenge-306/luca-ferrari/blog-7.txt b/challenge-305/luca-ferrari/blog-7.txt index 140cfd2d06..140cfd2d06 100644 --- a/challenge-306/luca-ferrari/blog-7.txt +++ b/challenge-305/luca-ferrari/blog-7.txt diff --git a/challenge-306/luca-ferrari/blog-8.txt b/challenge-305/luca-ferrari/blog-8.txt index 330e58a7c1..330e58a7c1 100644 --- a/challenge-306/luca-ferrari/blog-8.txt +++ b/challenge-305/luca-ferrari/blog-8.txt diff --git a/challenge-306/luca-ferrari/blog-9.txt b/challenge-305/luca-ferrari/blog-9.txt index 5c4a0e081f..5c4a0e081f 100644 --- a/challenge-306/luca-ferrari/blog-9.txt +++ b/challenge-305/luca-ferrari/blog-9.txt diff --git a/challenge-306/luca-ferrari/pljava/pom.xml b/challenge-305/luca-ferrari/pljava/pom.xml index 99c9085d7e..99c9085d7e 100644 --- a/challenge-306/luca-ferrari/pljava/pom.xml +++ b/challenge-305/luca-ferrari/pljava/pom.xml diff --git a/challenge-306/luca-ferrari/pljava/src/main/java/Task1.java b/challenge-305/luca-ferrari/pljava/src/main/java/Task1.java index d2e48bcd52..d2e48bcd52 100644 --- a/challenge-306/luca-ferrari/pljava/src/main/java/Task1.java +++ b/challenge-305/luca-ferrari/pljava/src/main/java/Task1.java diff --git a/challenge-306/luca-ferrari/pljava/src/main/java/Task2.java b/challenge-305/luca-ferrari/pljava/src/main/java/Task2.java index bd365bdaba..bd365bdaba 100644 --- a/challenge-306/luca-ferrari/pljava/src/main/java/Task2.java +++ b/challenge-305/luca-ferrari/pljava/src/main/java/Task2.java diff --git a/challenge-306/luca-ferrari/plperl/ch-1.plperl b/challenge-305/luca-ferrari/plperl/ch-1.plperl index ef52151fbe..ef52151fbe 100644 --- a/challenge-306/luca-ferrari/plperl/ch-1.plperl +++ b/challenge-305/luca-ferrari/plperl/ch-1.plperl diff --git a/challenge-306/luca-ferrari/plperl/ch-2.plperl b/challenge-305/luca-ferrari/plperl/ch-2.plperl index b684e6ed87..b684e6ed87 100644 --- a/challenge-306/luca-ferrari/plperl/ch-2.plperl +++ b/challenge-305/luca-ferrari/plperl/ch-2.plperl diff --git a/challenge-306/luca-ferrari/plpgsql/ch-1.sql b/challenge-305/luca-ferrari/plpgsql/ch-1.sql index 1739abe155..1739abe155 100644 --- a/challenge-306/luca-ferrari/plpgsql/ch-1.sql +++ b/challenge-305/luca-ferrari/plpgsql/ch-1.sql diff --git a/challenge-306/luca-ferrari/plpgsql/ch-2.sql b/challenge-305/luca-ferrari/plpgsql/ch-2.sql index c973eb42f6..c973eb42f6 100644 --- a/challenge-306/luca-ferrari/plpgsql/ch-2.sql +++ b/challenge-305/luca-ferrari/plpgsql/ch-2.sql diff --git a/challenge-306/luca-ferrari/python/ch-1.py b/challenge-305/luca-ferrari/python/ch-1.py index 29b961e251..29b961e251 100644 --- a/challenge-306/luca-ferrari/python/ch-1.py +++ b/challenge-305/luca-ferrari/python/ch-1.py diff --git a/challenge-306/luca-ferrari/python/ch-2.py b/challenge-305/luca-ferrari/python/ch-2.py index 5f98742484..5f98742484 100644 --- a/challenge-306/luca-ferrari/python/ch-2.py +++ b/challenge-305/luca-ferrari/python/ch-2.py diff --git a/challenge-306/luca-ferrari/raku/ch-1.raku b/challenge-305/luca-ferrari/raku/ch-1.raku index 565ee6744e..565ee6744e 100644 --- a/challenge-306/luca-ferrari/raku/ch-1.raku +++ b/challenge-305/luca-ferrari/raku/ch-1.raku diff --git a/challenge-306/luca-ferrari/raku/ch-2.raku b/challenge-305/luca-ferrari/raku/ch-2.raku index 5fc73464f6..5fc73464f6 100644 --- a/challenge-306/luca-ferrari/raku/ch-2.raku +++ b/challenge-305/luca-ferrari/raku/ch-2.raku diff --git a/challenge-305/perlboy1967/perl/ch1.pl b/challenge-305/perlboy1967/perl/ch-1.pl index fe30952f58..fe30952f58 100755 --- a/challenge-305/perlboy1967/perl/ch1.pl +++ b/challenge-305/perlboy1967/perl/ch-1.pl diff --git a/challenge-305/perlboy1967/perl/ch2.pl b/challenge-305/perlboy1967/perl/ch-2.pl index ca3e7d4e52..ca3e7d4e52 100755 --- a/challenge-305/perlboy1967/perl/ch2.pl +++ b/challenge-305/perlboy1967/perl/ch-2.pl diff --git a/challenge-305/richard-park/apl/AlienDictionary.aplf b/challenge-305/richard-park/apl/AlienDictionary.aplf new file mode 100644 index 0000000000..aaff84bd23 --- /dev/null +++ b/challenge-305/richard-park/apl/AlienDictionary.aplf @@ -0,0 +1,7 @@ +AlienDictionary←{ +⍝ ⍵: List of strings +⍝ ⍺: Character dictionary :: simple character vector +⍝ ←: ⍵ sorted lexicographically according to ⍺ + ⍵[⍺⍋↑⍵] + } + diff --git a/challenge-305/richard-park/apl/BinaryPrefix.aplf b/challenge-305/richard-park/apl/BinaryPrefix.aplf new file mode 100644 index 0000000000..c4cb1be3fe --- /dev/null +++ b/challenge-305/richard-park/apl/BinaryPrefix.aplf @@ -0,0 +1,6 @@ + BinaryPrefix←{ +⍝ ⍵: Boolean vector +⍝ ←: Boolean vector in which 1s mark the ends of prefixes that are +⍝ prime numbers in decimal + 2=+⌿0=(⍳⌈/n)∘.|n←2⊥i⌽(i←⍳≢⍵)(↑⍤0 1)⍵ + } diff --git a/challenge-305/richard-park/apl/W305_APL.ipynb b/challenge-305/richard-park/apl/W305_APL.ipynb new file mode 100644 index 0000000000..aa78155342 --- /dev/null +++ b/challenge-305/richard-park/apl/W305_APL.ipynb @@ -0,0 +1,450 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "84744954-b16a-4923-81d4-07be002b5f09", + "metadata": {}, + "source": [ + "# Perl Weekly Challenge 305\n", + "These are solutions to [Perl Weekly Challenge 305](https://theweeklychallenge.org/blog/perl-weekly-challenge-305/) in [Dyalog APL](https://www.dyalog.com/)." + ] + }, + { + "cell_type": "markdown", + "id": "2829f2d3-9515-4b16-ace4-682f6aa1f044", + "metadata": {}, + "source": [ + "## Task 1: Binary Prefix\n", + "Take a Boolean vector as input and return a Boolean vector where a 1 indicates that the partial binary number up to that element is a prime number.\n", + "\n", + "For example, `BinaryPrefix 1 0 1` returns `0 1 1` because:\n", + "- $1$ is not prime\n", + "- $2=b_2(1 0)$ is prime\n", + "- $5=b_2(1 0 1)$ is prime" + ] + }, + { + "cell_type": "markdown", + "id": "f2d288c4-392a-49db-8028-9779676bb709", + "metadata": {}, + "source": [ + "### Creating prefixes\n", + "There are several valid approaches. The simplest is a **catenate-scan** `,\\⍵`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "e23962d8-3008-4cab-9f58-ae94e726515e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">Was ON\n", + "</pre>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + }, + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">┌─┬───┬─────┬───────┬─────────┬───────────┬─────────────┐\n", + "│1│1 0│1 0 1│1 0 1 1│1 0 1 1 0│1 0 1 1 0 1│1 0 1 1 0 1 1│\n", + "└─┴───┴─────┴───────┴─────────┴───────────┴─────────────┘\n", + "</pre>" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "]box on\n", + ",\\1 0 1 1 0 1 1" + ] + }, + { + "cell_type": "markdown", + "id": "d99ebccb-5a07-46a7-a887-bd4ef3d22a01", + "metadata": {}, + "source": [ + "We can then convert each number with the base conversion primitive." + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "6345a2a6-6168-485a-b0d6-b71bd3b35444", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">1 0 1 1 0 1 1\n", + "1 2 5 11 22 45 91\n", + "</pre>" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b←1 0 1 1 0 1 1\n", + "↑(b)(2⊥¨,\\b)" + ] + }, + { + "cell_type": "markdown", + "id": "43c244c5-7a53-4419-88ca-9cc03e1f6ace", + "metadata": {}, + "source": [ + "It can be more efficient to build a flat array that works directly with primitive functions in APL, rather than looping using the each `F¨` operator. In the following expression, we build a flat Boolean matrix of prefixes using [the rank operator](https://course.dyalog.com/cells-and-axes/#the-rank-operator). The base conversion primitive function can then work on the entire array at once rather than having to loop over individual prefixes. This is significantly faster than `2⊥¨,\\b`." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "a10a5ebb-e7b6-437a-8927-6fd555118141", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">1 2 5 11 22 45 91\n", + "</pre>" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2⊥i⌽(i←⍳≢b)(↑⍤0 1)b" + ] + }, + { + "cell_type": "markdown", + "id": "72d3032f-7bbd-4d9f-8e89-f5e393c44d3a", + "metadata": {}, + "source": [ + "### Checking Primes\n", + "A direct approach is to use the \"unique divisors\" idiom." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "43d605b8-bd1e-4589-8bfc-bae3d7e5278b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">┌─┬───┬───┬────┬─────────┬─────────────┬─────────┐\n", + "│1│1 2│1 5│1 11│1 2 11 22│1 3 5 9 15 45│1 7 13 91│\n", + "└─┴───┴───┴────┴─────────┴─────────────┴─────────┘\n", + "</pre>" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "(∪⊢∨⍳)¨1 2 5 11 22 45 91" + ] + }, + { + "cell_type": "markdown", + "id": "b878cdaa-94e0-422c-b740-266c301634a3", + "metadata": {}, + "source": [ + "We can then count the unique divisors and check which have exactly two, which is the definition of being a prime number." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "a035c3f8-7b4b-4dc8-bfdc-791774daa4b0", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">0 1 1 1 0 0 0\n", + "</pre>" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "2=(≢∘∪⊢∨⍳)¨1 2 5 11 22 45 91" + ] + }, + { + "cell_type": "markdown", + "id": "24f817ed-67ea-4f48-aa64-f206228ba8f9", + "metadata": {}, + "source": [ + "The `(∪⊢∨⍳)` train is cute and somewhat says exactly what we want: \"unique divisors of ⍵\". However, it is quite inefficient to find all divisors between every number and the list of integers up to that number.\n", + "\n", + "Instead, let's find where all integers up to the largest in our list are evenly divided with the modulo function." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "2e91cf5a-a8fd-4850-acfb-a5eb0f20ac7b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<pre class=\"language-APL\">0 1 1 1 0 0 0\n", + "</pre>" + ] + }, + "execution_count": 46, + "metadata": {} |
