diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-13 16:37:08 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2022-01-13 16:37:08 +0800 |
| commit | c2713a3b64e57e84181576dcbf37700b29e8ff65 (patch) | |
| tree | 3c71339e5a8eb19a2357ec8febaf0949cc47b6cc | |
| parent | b85886348bc2ee30ff37ed53895ba5a9f30dd017 (diff) | |
| download | perlweeklychallenge-club-c2713a3b64e57e84181576dcbf37700b29e8ff65.tar.gz perlweeklychallenge-club-c2713a3b64e57e84181576dcbf37700b29e8ff65.tar.bz2 perlweeklychallenge-club-c2713a3b64e57e84181576dcbf37700b29e8ff65.zip | |
CY's challenge: Task 1 in 10 languages; Progress: Day 1 Afternoon
| -rw-r--r-- | challenge-147/cheok-yin-fung/README.md | 13 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/julia/ch-1.jl | 67 | ||||
| -rw-r--r-- | challenge-147/cheok-yin-fung/perl/ch-1.pl | 60 |
3 files changed, 140 insertions, 0 deletions
diff --git a/challenge-147/cheok-yin-fung/README.md b/challenge-147/cheok-yin-fung/README.md new file mode 100644 index 0000000000..fb4db4e925 --- /dev/null +++ b/challenge-147/cheok-yin-fung/README.md @@ -0,0 +1,13 @@ +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 + + 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]; + |
