aboutsummaryrefslogtreecommitdiff
path: root/challenge-147
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-13 11:11:43 +0000
committerGitHub <noreply@github.com>2022-01-13 11:11:43 +0000
commit3aacddfca66051cc3f62174daa60698d0c9fa282 (patch)
tree5c399a5a3182528ea3211c6efa0833477f51eb10 /challenge-147
parent3aaab5cee8ebf950882b5561c07f00dab83dc4bf (diff)
parent33336454923f69e42c139f2ec03b35f6fb6abfa2 (diff)
downloadperlweeklychallenge-club-3aacddfca66051cc3f62174daa60698d0c9fa282.tar.gz
perlweeklychallenge-club-3aacddfca66051cc3f62174daa60698d0c9fa282.tar.bz2
perlweeklychallenge-club-3aacddfca66051cc3f62174daa60698d0c9fa282.zip
Merge pull request #5515 from E7-87-83/newt
Task 1 in many languages
Diffstat (limited to 'challenge-147')
-rw-r--r--challenge-147/cheok-yin-fung/README2
-rw-r--r--challenge-147/cheok-yin-fung/blog.txt15
-rw-r--r--challenge-147/cheok-yin-fung/julia/ch-1.jl67
-rw-r--r--challenge-147/cheok-yin-fung/perl/ch-1.pl60
4 files changed, 143 insertions, 1 deletions
diff --git a/challenge-147/cheok-yin-fung/README b/challenge-147/cheok-yin-fung/README
index 4413b13ada..05c1883e53 100644
--- a/challenge-147/cheok-yin-fung/README
+++ b/challenge-147/cheok-yin-fung/README
@@ -1 +1 @@
-Solutions by Cheok-Yin Fung.
+ Solutions by Cheok-Yin Fung.
diff --git a/challenge-147/cheok-yin-fung/blog.txt b/challenge-147/cheok-yin-fung/blog.txt
new file mode 100644
index 0000000000..12acf725e4
--- /dev/null
+++ b/challenge-147/cheok-yin-fung/blog.txt
@@ -0,0 +1,15 @@
+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
+
diff --git a/challenge-147/cheok-yin-fung/julia/ch-1.jl b/challenge-147/cheok-yin-fung/julia/ch-1.jl
new file mode 100644
index 0000000000..166fd13389
--- /dev/null
+++ b/challenge-147/cheok-yin-fung/julia/ch-1.jl
@@ -0,0 +1,67 @@
+# The Weekly Challenge 147
+# Task 1 Truncatable Prime
+# Thursday, January 13, 2022 PM03:18:00
+
+
+left_trun_primes = [2, 3, 5, 7]
+primes = [2, 3, 5, 7]
+index_ltp = [1, 5]
+
+
+
+lastelement(array) = array[length(array)]
+x_no_less_than_sqrt_y(x, y) = x ≤ √y
+
+
+function grep(func, array)
+ ans = []
+ for variable in array
+ if func(variable)
+ push!(ans, variable)
+ end
+ end
+ return Tuple(ans)
+end
+
+
+function is_prime(int)
+ no_less_than_sqrt(x) = x_no_less_than_sqrt_y(x, int)
+ relatively_small_primes = grep(no_less_than_sqrt , primes)
+ for p in relatively_small_primes
+ if int % p == 0
+ return false
+ end
+ end
+ return true
+end
+
+
+function append_arr_of_ltp(digits)
+ k = 0
+ for d = 1:9
+ for ind = index_ltp[digits-1]:(index_ltp[digits]-1)
+ new_num = parse(Int64, string(d) * string(left_trun_primes[ind]))
+ if is_prime(new_num)
+ push!(left_trun_primes, new_num)
+ k += 1
+ end
+ end
+ end
+ push!(index_ltp, lastelement(index_ltp) + k + 1)
+end
+
+
+function append_arr_of_primes(min,max)
+ for num = min:max
+ if is_prime(num)
+ push!(primes, num)
+ end
+ end
+end
+
+
+
+append_arr_of_ltp(2)
+append_arr_of_primes(10,sqrt(999))
+append_arr_of_ltp(3)
+println(left_trun_primes[1:20])
diff --git a/challenge-147/cheok-yin-fung/perl/ch-1.pl b/challenge-147/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..ff93203336
--- /dev/null
+++ b/challenge-147/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,60 @@
+# The Weekly Challenge 147
+# Task 1 Truncatable Prime
+# Thursday, January 13, 2022 PM04:18:51 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++) {
+ return 0 if $t % $prime[$k] == 0;
+ }
+ return 1;
+}
+
+
+
+sub append_arr_of_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
+ }
+ push @prime, $can;
+ }
+}
+
+
+
+sub append_arr_of_ltp {
+ my $target_size = $_[0];
+ if ($target_size <= (scalar @ltp + scalar @recent_ltp)) {
+ push @ltp, @recent_ltp;
+ return;
+ }
+ for my $d (1..9) {
+ for my $num (@recent_ltp) {
+ my $new_num = $d . $num;
+ push @new_ltp, $new_num if is_prime($new_num);
+ }
+ }
+ push @ltp, @recent_ltp;
+ @recent_ltp = @new_ltp;
+ @new_ltp = ();
+ append_arr_of_ltp($target_size);
+}
+
+
+
+append_arr_of_primes(1000);
+append_arr_of_ltp(20);
+say $_ for @ltp[0..19];
+