diff options
| -rw-r--r-- | challenge-102/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-102/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-102/adam-russell/perl/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-102/adam-russell/perl/ch-2.pl | 22 | ||||
| -rw-r--r-- | challenge-102/adam-russell/prolog/ch-1.p | 34 | ||||
| -rw-r--r-- | challenge-102/adam-russell/prolog/ch-2.p | 29 |
6 files changed, 151 insertions, 0 deletions
diff --git a/challenge-102/adam-russell/blog.txt b/challenge-102/adam-russell/blog.txt new file mode 100644 index 0000000000..3b012b2385 --- /dev/null +++ b/challenge-102/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/03/07
\ No newline at end of file diff --git a/challenge-102/adam-russell/blog1.txt b/challenge-102/adam-russell/blog1.txt new file mode 100644 index 0000000000..4503e3ca0c --- /dev/null +++ b/challenge-102/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/03/07
\ No newline at end of file diff --git a/challenge-102/adam-russell/perl/ch-1.pl b/challenge-102/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..a207875027 --- /dev/null +++ b/challenge-102/adam-russell/perl/ch-1.pl @@ -0,0 +1,64 @@ +use strict; +use warnings; +## +# You are given a positive integer $N. +# Write a script to generate all Rare Numbers of size $N, if any exist. +## +use Thread; +use constant THREAD_COUNT => 4; + +sub rare_number_check{ + my($lower, $upper) = @_; + my @rares; + { + my $r = $lower; + my $r1 = reverse($r); + if($r > $r1){ + my $rs = sqrt($r + $r1); + my $r1s = sqrt($r - $r1); + if($rs !~ m/\./ && $r1s !~ m/\./){ + push @rares, $lower; + } + } + $lower++; + redo unless $lower > $upper; + } + return \@rares; +} + +sub rare_number{ + my($n) = @_; + my @rares; + my $lower = "1" . 0 x ($n - 1); + my $upper = "1" . 9 x ($n - 1); + my $increment = $lower; + { + my @threads; + for(1 .. THREAD_COUNT){ + my $t = Thread->new(\&rare_number_check, $lower, $upper); + push @threads, $t; + $lower = $upper + 1; + $upper = $lower + $increment - 1; + last if(length($upper) == ($n + 1)); + } + foreach my $t (@threads){ + my $rares = $t->join(); + push @rares, @{$rares}; + } + redo unless(length($upper) == ($n + 1)); + } + return \@rares; +} + +MAIN:{ + my($N); + $N=2; + my $rares = rare_number($N); + print "$N digits: " . join(" ", @{$rares}) . "\n"; + $N=6; + $rares = rare_number($N); + print "$N digits: " . join(" ", @{$rares}) . "\n"; + $N=9; + $rares = rare_number($N); + print "$N digits: " . join(" ", @{$rares}) . "\n"; +} diff --git a/challenge-102/adam-russell/perl/ch-2.pl b/challenge-102/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..62722ff918 --- /dev/null +++ b/challenge-102/adam-russell/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +## +# You are given a positive integer $N. +# Write a script to produce a hash counting string +# of that length. +## +sub hash_counting_string{ + my($n) = @_; + return "" if $n == 0; + return "#" if $n == 1; + my $h = "$n#"; + return hash_counting_string($n - length($h)) . $h; +} + +MAIN:{ + print hash_counting_string(1). "\n"; + print hash_counting_string(2). "\n"; + print hash_counting_string(3). "\n"; + print hash_counting_string(10). "\n"; + print hash_counting_string(14). "\n"; +} diff --git a/challenge-102/adam-russell/prolog/ch-1.p b/challenge-102/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..929f1520c4 --- /dev/null +++ b/challenge-102/adam-russell/prolog/ch-1.p @@ -0,0 +1,34 @@ +:-initialization(main). + +perfect_square(N):- + A is floor(sqrt(N)), + N is A * A. + +rare(N, N, Rares, Rares). +rare(Lower, Upper, RareAccum, Rares):- + number_codes(Lower, C), + reverse(C, CR), + number_codes(R1, CR), + X0 is Lower + R1, + X1 is Lower - R1, + perfect_square(X0), + perfect_square(X1), + Next is Lower + 1, + rare(Next, Upper, [Lower|RareAccum], Rares). +rare(Lower, Upper, RareAccum, Rares):- + Next is Lower + 1, + rare(Next, Upper, RareAccum, Rares). + +rare_numbers(N, Rares):- + Lower is 10 ^ (N - 1), + Upper is (10 ^ N) - 1, + rare(Lower, Upper, [], Rares). + +main:- + rare_numbers(2, Rares2), + write(Rares2), nl, + rare_numbers(6, Rares6), + write(Rares6), nl, + rare_numbers(9, Rares9), + write(Rares9), nl, + halt. diff --git a/challenge-102/adam-russell/prolog/ch-2.p b/challenge-102/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..b62179b8e0 --- /dev/null +++ b/challenge-102/adam-russell/prolog/ch-2.p @@ -0,0 +1,29 @@ +:-initialization(main). + +hcs(0, String, String). +hcs(1, StringAccum, String):- + hcs(0, [35|StringAccum], String). +hcs(N, StringAccum, String):- + number_codes(N, C), + append(C, "#", Accum), + length(Accum, L), + N0 is N - L, + append(Accum, StringAccum, StringAccum0), + hcs(N0, StringAccum0, String). + +hash_counting_string(N, String):- + hcs(N, [], S), + atom_codes(String, S). + +main:- + hash_counting_string(1, String1), + write(String1), nl, + hash_counting_string(2, String2), + write(String2), nl, + hash_counting_string(3, String3), + write(String3), nl, + hash_counting_string(10, String10), + write(String10), nl, + hash_counting_string(14, String14), + write(String14), nl, + halt. |
