diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-15 12:40:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-15 12:40:15 +0000 |
| commit | 982d91afb74958cc97ae49086c5404d716dfb3b9 (patch) | |
| tree | c9c26917f82d74dab654a080b640b1a539514e90 /challenge-147 | |
| parent | f3ea837cd783aa09b1e2eed1ef3b20eb681921d1 (diff) | |
| parent | e88e2fd6d5f3ee6595319a8f2fc1e7f475fb622d (diff) | |
| download | perlweeklychallenge-club-982d91afb74958cc97ae49086c5404d716dfb3b9.tar.gz perlweeklychallenge-club-982d91afb74958cc97ae49086c5404d716dfb3b9.tar.bz2 perlweeklychallenge-club-982d91afb74958cc97ae49086c5404d716dfb3b9.zip | |
Merge pull request #5519 from E7-87-83/newt
Week 147 Task 1 in 8 languages
Diffstat (limited to 'challenge-147')
| -rw-r--r-- | challenge-147/cheok-yin-fung/awk/ch-1.awk | 46 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/bash/ch-1.sh | 57 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/blog.txt | 16 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/cpp/ch-1.cpp | 71 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/java/LeftTruncatablePrime.java | 79 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/node/ch-1.js | 60 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/perl/ch-1.pl | 27 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/smalltalk/ch-1.st | 70 |
8 files changed, 400 insertions, 26 deletions
diff --git a/challenge-147/cheok-yin-fung/awk/ch-1.awk b/challenge-147/cheok-yin-fung/awk/ch-1.awk new file mode 100644 index 0000000000..94c1ac860e --- /dev/null +++ b/challenge-147/cheok-yin-fung/awk/ch-1.awk @@ -0,0 +1,46 @@ +# The Weekly Challenge 147 +# Task 1 Truncatable Prime +# Instructions: Print the first 20 left truncatable primes. +# Usage: awk -f 'ch-1.awk' + +BEGIN { + target_size = 20 + prime[0] = 2 + prime[1] = 3 + prime[2] = 5 + prime[3] = 7 + + i = 4 + for (n=10; n < 1000; n++) { + loop = 1 + for (k=2; k < n/3 && loop == 1; k++) { + if (n % k == 0) + loop = 0 + } + if (loop == 1) { + prime[i] = n + i++ + } + } + + i=0 + for (n in prime) { + loop = 0 + for (k in prime) { + if (prime[n] % 10 == prime[k] && prime[k] < 10) { + loop++ + } + if (prime[n] % 100 == prime[k] && prime[k] < 100) { + loop++ + } + } + if (loop == 2) { + print prime[n] + i++ + } + if (i > target_size) { + exit + } + } + +} diff --git a/challenge-147/cheok-yin-fung/bash/ch-1.sh b/challenge-147/cheok-yin-fung/bash/ch-1.sh new file mode 100644 index 0000000000..efcf12a5ff --- /dev/null +++ b/challenge-147/cheok-yin-fung/bash/ch-1.sh @@ -0,0 +1,57 @@ +# The Weekly Challenge 147 +# Task 1 Truncatable Prime +# Instructions: Print the first 20 left truncatable primes. +# Usage: $ chmod +x ch-1.sh +# $ ./ch-1.sh + +target_size=20 +prime=(2 3 5 7) + +i=4 +for ((n=10; n<1000; n=$n+1)); +do + loop=1 + for ((k=2;k<=n/3; k=$k+1)); + do + if [[ $n%$k -eq 0 ]]; + then + loop=0 + break + fi + done + if [[ $loop -eq 1 ]]; + then + prime[$((i))]=$n + i=$i+1 + fi +done + +i=0 +n=0 +while [ $((prime[$n])) -le 900 ] # 997 or above will return error + # ^^^ a bit cheating here +do + loop=0 + k=0 + for ((k=0; $((prime[$k]))<=996; k=$k+1)); + do + if [[ $((prime[$n]))%10 -eq $((prime[$k])) ]]; + then + loop=$loop+1 + fi + if [[ $((prime[$n]))%100 -eq $((prime[$k])) ]]; + then + loop=$loop+1 + fi + done + if [[ $loop -eq 2 ]]; + then + echo $((prime[$n])) + i=$i+1 + fi + n=$n+1 + if [[ $i -gt $target_size ]] + then + exit + fi +done diff --git a/challenge-147/cheok-yin-fung/blog.txt b/challenge-147/cheok-yin-fung/blog.txt index 12acf725e4..1509a3b82a 100644 --- a/challenge-147/cheok-yin-fung/blog.txt +++ b/challenge-147/cheok-yin-fung/blog.txt @@ -1,15 +1 @@ -This is the beginning of the semester of my PG dip in IT; and I have finished all exams and assignments from the previous semester. Somehow today I want to try to play with programming languages. While Mohammad has said, having fun is important in learning, now I try to do the Task 1 in different programming languages which I am fluent at least at the "sightseeing purpose", within 4 days. - -Target list: -awk bash C++ -Java Node.js Julia -LISP PHP Perl Smalltalk -Befunge-93 (if I have that mentality) - -Script done (last update: Thursday, January 13, 2022 08:34:23 UTC): -1. Julia -2. Perl - ->>>> -The above content will be edited and put on https://e7-87-83.github.io/coding/challenge_147.html - +https://e7-87-83.github.io/coding/challenge_147.html diff --git a/challenge-147/cheok-yin-fung/cpp/ch-1.cpp b/challenge-147/cheok-yin-fung/cpp/ch-1.cpp new file mode 100644 index 0000000000..dbfdff0aaa --- /dev/null +++ b/challenge-147/cheok-yin-fung/cpp/ch-1.cpp @@ -0,0 +1,71 @@ +// The Weekly Challenge 147 +// Task 1 Truncatable Prime +// g++ ch-1.cpp -o ch-1.o +// Friday, January 14, 2022 PM01:15:37 HKT + +#include <vector> +#include <cmath> +#include <algorithm> +#include <iostream> + +using namespace std; +vector<int> ltp = {}; +vector<int> recent_ltp = {2, 3, 5, 7}; +vector<int> new_ltp = {}; +vector<int> primes = {2, 3, 5, 7}; + +bool is_prime(int t) { + for (int k = 0; primes.at(k) <= sqrt(t); k++) { + if (t % primes.at(k) == 0) + return false; + } + return true; +} + + + +void append_primes(int max) { + for (int can = primes.back()+1; can <= max; can++) { + bool good = true; + for (int k = 0; primes.at(k) <= sqrt(can) & good; k++ ) { + if (can % primes.at(k) == 0) + good = false; + } + if (good) + primes.push_back(can); + } +} + + + +void append_ltp(int target_size) { + if (target_size <= ltp.size() + recent_ltp.size() ) { + ltp.insert( ltp.end(), recent_ltp.begin(), recent_ltp.end() ); + return; + } + for (int d = 1; d <= 9; d++) { + for (int r = 0; r < recent_ltp.size(); r++ ) { + char str[20]; + int num = recent_ltp.at(r); + sprintf(str, "%d%d", d, num); + int new_num = atoi(str); + if (is_prime(new_num)) + new_ltp.push_back(new_num); + } + } + ltp.insert( ltp.end(), recent_ltp.begin(), recent_ltp.end() ); + recent_ltp = new_ltp; + new_ltp = {}; + append_ltp(target_size); +} + + + +int main() { + append_primes(1000); + append_ltp(20); + for (int i = 0; i < 20; i++) { + cout << ltp.at(i) << endl; + } + return 0; +} diff --git a/challenge-147/cheok-yin-fung/java/LeftTruncatablePrime.java b/challenge-147/cheok-yin-fung/java/LeftTruncatablePrime.java new file mode 100644 index 0000000000..57c6971213 --- /dev/null +++ b/challenge-147/cheok-yin-fung/java/LeftTruncatablePrime.java @@ -0,0 +1,79 @@ +// The Weekly Challenge 147 +// Task 1 Truncatable Prime +// Friday, January 14, 2022 AM11:26:14 HKT +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Arrays; +@SuppressWarnings("unchecked") + +public class LeftTruncatablePrime +{ + public static List<Integer> Primes; + public static List<Integer> LTPrimes; + public static List<Integer> recentLTPrimes; + public static List<Integer> newLTPrimes; + + public static void main(String... args) + { + Integer[] singleDigitPrimes = {2, 3, 5, 7}; + Primes = new ArrayList<Integer>(); + Collections.addAll(Primes, singleDigitPrimes); + + LTPrimes = new ArrayList<Integer>(); + recentLTPrimes = new ArrayList<Integer>(); + Collections.addAll(recentLTPrimes, singleDigitPrimes); + newLTPrimes = new ArrayList<Integer>(); + appendPrimes(1000); + appendLTPrimes(20); + + for (int i = 0; i < 20; i++) + System.out.println(LTPrimes.get(i)); + } + + public static boolean isPrime(Integer x) + { + Integer p = 0; + for (int i = 0; p <= Math.sqrt(x) ; i++ ) + { + p = Primes.get(i); + if (x % p == 0) + return false; + } + return true; + } + + public static void appendPrimes(Integer max) + { + Integer i = Primes.get(Primes.size()-1)+1; + while (Primes.get(Primes.size()-1) < max) + { + if (isPrime(i)) + Primes.add(i); + i++; + } + } + + + public static void appendLTPrimes(Integer targetSize) + { + if (targetSize <= LTPrimes.size() + recentLTPrimes.size()) + { + LTPrimes.addAll(recentLTPrimes); + return; + } + for (int d = 1; d <= 9; d++) { + for (Integer num : recentLTPrimes) + { + int newNum = Integer.parseInt(d + "" + num); + if (isPrime(newNum)) { + newLTPrimes.add(newNum); + } + } + } + LTPrimes.addAll(recentLTPrimes); + recentLTPrimes = (List)((ArrayList)newLTPrimes).clone(); + newLTPrimes.clear(); + appendLTPrimes(targetSize); + } +} diff --git a/challenge-147/cheok-yin-fung/node/ch-1.js b/challenge-147/cheok-yin-fung/node/ch-1.js new file mode 100644 index 0000000000..c3a75a954d --- /dev/null +++ b/challenge-147/cheok-yin-fung/node/ch-1.js @@ -0,0 +1,60 @@ +// The Weekly Challenge 147 +// Task 1 Truncatable Prime +// Friday, January 14, 2022 PM07:31:14 HKT + +let ltp = new Array(); +let recent_ltp = new Array(2,3,5,7); +let new_ltp = new Array(); + +let prime = new Array(2,3,5,7); + + + +function is_prime(t) { + for (let k = 0; prime[k] <= Math.sqrt(t); k++) { + if (t % prime[k] == 0) + return false; + } + return true; +} + + + +function append_primes(max) { + HERE: for (can = prime[prime.length-1]+1; can <= max ; can++) { + for (let k=0; prime[k] <= Math.sqrt(can) ;k++) { + if (can % prime[k] == 0) { + continue HERE; + } + } + prime.push(can); + } + +} + + + +function append_ltp(target_size) { + if (target_size <= ltp.length + recent_ltp.length) { + ltp = ltp.concat(recent_ltp); + return; + } + for (d = 1; d<=9; d++) { + for (num of recent_ltp) { + new_num = parseInt(d + '' + num); + if (is_prime(new_num)) { + new_ltp.push(new_num); + } + } + } + ltp = ltp.concat(recent_ltp); + recent_ltp = new_ltp; + new_ltp = new Array(); + append_ltp(target_size); +} + + + +append_primes(1000); +append_ltp(20); +console.log(ltp.slice(0, 20)); diff --git a/challenge-147/cheok-yin-fung/perl/ch-1.pl b/challenge-147/cheok-yin-fung/perl/ch-1.pl index ff93203336..02212489b5 100644 --- a/challenge-147/cheok-yin-fung/perl/ch-1.pl +++ b/challenge-147/cheok-yin-fung/perl/ch-1.pl @@ -1,19 +1,24 @@ # The Weekly Challenge 147 # Task 1 Truncatable Prime -# Thursday, January 13, 2022 PM04:18:51 HKT +# version 0: Thursday, January 13, 2022 PM04:18:51 HKT +# version 1: Friday, January 14, 2022 PM12:34 HKT use v5.12.0; use warnings; + + my @ltp = (); my @recent_ltp = (2,3,5,7); my @new_ltp = (); my @prime = (2,3,5,7); + + sub is_prime { my $t = $_[0]; - for (my $k = 0; defined($prime[$k]) && $prime[$k] <= sqrt($t) ;$k++) { + for (my $k = 0; $prime[$k] <= sqrt($t); $k++) { return 0 if $t % $prime[$k] == 0; } return 1; @@ -21,12 +26,12 @@ sub is_prime { -sub append_arr_of_primes { +sub append_primes { my $max = $_[0]; - my @relatively_small_primes = grep { $_ <= sqrt($max) } @prime; - HERE: for my $can ($relatively_small_primes[-1]+1..$max) { - for my $p (@relatively_small_primes) { - next HERE if $can % $p == 0 + HERE: for my $can ($prime[-1]+1..$max) { + for my $p (@prime) { + next HERE if $can % $p == 0; + last if $p > sqrt($can); } push @prime, $can; } @@ -34,7 +39,7 @@ sub append_arr_of_primes { -sub append_arr_of_ltp { +sub append_ltp { my $target_size = $_[0]; if ($target_size <= (scalar @ltp + scalar @recent_ltp)) { push @ltp, @recent_ltp; @@ -49,12 +54,12 @@ sub append_arr_of_ltp { push @ltp, @recent_ltp; @recent_ltp = @new_ltp; @new_ltp = (); - append_arr_of_ltp($target_size); + append_ltp($target_size); } -append_arr_of_primes(1000); -append_arr_of_ltp(20); +append_primes(1000); +append_ltp(20); say $_ for @ltp[0..19]; diff --git a/challenge-147/cheok-yin-fung/smalltalk/ch-1.st b/challenge-147/cheok-yin-fung/smalltalk/ch-1.st new file mode 100644 index 0000000000..4ba4e1d005 --- /dev/null +++ b/challenge-147/cheok-yin-fung/smalltalk/ch-1.st @@ -0,0 +1,70 @@ +"GNU Smalltalk 3.2.5" +"The Weekly Challenge 147" +"Task 1 Truncatable Prime" +"Usage: gst -S ch-1.st" +"Thursday, January 13, 2022 PM20:30:44 UTC" + +Smalltalk at: #Primes put: nil. + +Ltp := OrderedCollection new. + +RecentLtp := OrderedCollection with: 2 with: 3 with: 5 with: 7. + +NewLtp := OrderedCollection new. + +my_Primes := OrderedCollection with: 2 with: 3 with: 5 with: 7. + +Smalltalk at: #Primes put: my_Primes. + + + +Number extend [ + inc [ + ^(self+1) + ] + dec [ + ^(self-1) + ] + sqrt [ "Why there is no built-in float square root at GNU Smalltalk?" + |smallNum| + smallNum := 1. + [smallNum squared > self] whileFalse: [smallNum := smallNum inc]. + ^(smallNum dec) + ] + isPrime [ + |b i| + i := 1. + [ (b := self \\ (Primes at: i) ~= 0) & (((Primes at: i) > self sqrt) not) ] whileTrue: [i := i inc]. + ^b + ] +] + + + +p := 10. + +[p < 1000] whileTrue: [ + (p isPrime) ifTrue: [my_Primes add: p. Smalltalk at: #Primes put: my_Primes.]. + p := p inc +]. + + + +[Ltp size + RecentLtp size >= 20] whileFalse: [ + 1 to: 9 do: [ :D | + RecentLtp do: [ :Num | + |NewNum| + NewNum := ((D asString, Num asString) asInteger). + (NewNum isPrime) ifTrue: [NewLtp add: NewNum] + ]. + ]. + Ltp addAll: RecentLtp. + RecentLtp := NewLtp. + NewLtp := OrderedCollection new. +]. + +Ltp addAll: RecentLtp. + +(Ltp copyFrom: 1 to: 20) printNl. + +ObjectMemory quit. |
