aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-147/cheok-yin-fung/blog.txt5
-rw-r--r--challenge-147/cheok-yin-fung/java/LeftTruncatablePrime.java79
-rw-r--r--challenge-147/cheok-yin-fung/perl/ch-1.pl12
-rw-r--r--challenge-147/cheok-yin-fung/smalltalk/ch-1.st70
4 files changed, 161 insertions, 5 deletions
diff --git a/challenge-147/cheok-yin-fung/blog.txt b/challenge-147/cheok-yin-fung/blog.txt
index 12acf725e4..9b08363ef9 100644
--- a/challenge-147/cheok-yin-fung/blog.txt
+++ b/challenge-147/cheok-yin-fung/blog.txt
@@ -6,10 +6,11 @@ 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):
+Script done (last update: Fri Jan 14 03:25:06 2022):
1. Julia
2. Perl
-
+3. Smalltalk
+4. Java
>>>>
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/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/perl/ch-1.pl b/challenge-147/cheok-yin-fung/perl/ch-1.pl
index ff93203336..4fa2d26e7b 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 AM01:20:32
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;
@@ -23,7 +28,8 @@ sub is_prime {
sub append_arr_of_primes {
my $max = $_[0];
- my @relatively_small_primes = grep { $_ <= sqrt($max) } @prime;
+ my $sqrtmax = sqrt($max);
+ my @relatively_small_primes = grep { $_ <= $sqrtmax } @prime;
HERE: for my $can ($relatively_small_primes[-1]+1..$max) {
for my $p (@relatively_small_primes) {
next HERE if $can % $p == 0
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..a6b33d7074
--- /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"
+"Friday, 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.