aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-102/stuart-little/perl/ch-1.pl164
-rwxr-xr-xchallenge-102/stuart-little/perl/ch-2.pl17
2 files changed, 181 insertions, 0 deletions
diff --git a/challenge-102/stuart-little/perl/ch-1.pl b/challenge-102/stuart-little/perl/ch-1.pl
new file mode 100755
index 0000000000..21efb3c76c
--- /dev/null
+++ b/challenge-102/stuart-little/perl/ch-1.pl
@@ -0,0 +1,164 @@
+#!/usr/bin/perl
+use warnings;
+use v5.12;
+
+# run <script> <number>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+use POSIX qw(ceil);
+
+sub isSq($n) {
+ my $s = sqrt $n;
+ return $s == int($s);
+}
+
+sub isRare($n) {
+ my $rev= int scalar reverse $n;
+ isSq($n+$rev) && ($n >= $rev) && isSq($n-$rev)
+}
+
+sub rarePals($d) {
+ my $halfDigs = ceil($d/2);
+ my $rest = $d - $halfDigs;
+ my @res = grep { isSq(2*$_) }
+ map { $_ . (scalar reverse substr($_,0,$rest)) }
+ (10**($halfDigs-1)..10**$halfDigs-1);
+ return \@res;
+}
+
+my $digs = $ARGV[0];
+($digs <= 22) && do {
+ my @basenrs = map {$_ =~ s/\n$//r} <DATA>;
+ my @all = sort ((grep {length($_) == $digs} @basenrs),@{rarePals($digs)});
+ for (@all) {say};
+ exit;
+};
+
+for (grep { isRare($_) } (10**($digs-1)..10**$digs-1)) {say};
+
+__DATA__
+65
+621770
+281089082
+2022652202
+2042832002
+868591084757
+872546974178
+872568754178
+6979302951885
+20313693904202
+20313839704202
+20331657922202
+20331875722202
+20333875702202
+40313893704200
+40351893720200
+200142385731002
+204238494066002
+221462345754122
+244062891224042
+245518996076442
+248359494187442
+403058392434500
+441054594034340
+816984566129618
+2078311262161202
+2133786945766212
+2135568943984212
+2135764587964212
+2135786765764212
+4135786945764210
+6157577986646405
+6889765708183410
+8052956026592517
+8052956206592517
+8191154686620818
+8191156864620818
+8191376864400818
+8650327689541457
+8650349867341457
+22542040692914522
+67725910561765640
+86965750494756968
+225342456863243522
+225342458663243522
+225342478643243522
+284684666566486482
+284684868364486482
+297128548234950692
+297128722852950692
+297148324656930692
+297148546434930692
+497168548234910690
+619431353040136925
+619631153042134925
+631688638047992345
+633288858025996145
+633488632647994145
+653488856225994125
+811865096390477018
+865721270017296468
+871975098681469178
+898907259301737498
+2042401829204402402
+2060303819041450202
+2420424089100600242
+2551755006254571552
+2702373360882732072
+2825378427312735282
+6531727101458000045
+6988066446726832640
+8066308349502036608
+8197906905009010818
+8200756128308135597
+8320411466598809138
+22134434735752443122
+22134434753752443122
+22134436953532443122
+22136414517954423122
+22136414971554423122
+22136456771730423122
+61952807156239928885
+61999171315484316965
+65459144877856561700
+208393425242000083802
+219518549668074815912
+257661195832219326752
+286694688797362186682
+837982875780054779738
+2414924301133245383042
+2414924323311045383042
+2414946523311023183042
+2576494891793995836752
+2576494893971995836752
+2620937863931054483162
+2620937863931054483162
+2620955641393276283162
+2622935621573476481162
+2622935643751276481162
+2622937641933274481162
+2622955841933256281162
+2622957843751254281162
+2727651947516658327272
+2747736918335953517072
+2788047668617596408872
+2788047848617776408872
+2788047868437576408872
+2788047888617376408872
+2939501759705522349392
+2939503375709360349392
+2939503537707740349392
+2939521359525562149392
+2939521557527542149392
+2939523577527340149392
+2939523779525320149392
+2959503377707360349192
+6344828989519887483525
+8045841652464561594308
+8045841654642561594308
+8655059576513659814468
+8655059772157639814468
+8655079374155679614468
+8655079574515659614468
+8888070771864228883913
diff --git a/challenge-102/stuart-little/perl/ch-2.pl b/challenge-102/stuart-little/perl/ch-2.pl
new file mode 100755
index 0000000000..d78a5bc464
--- /dev/null
+++ b/challenge-102/stuart-little/perl/ch-2.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+use warnings;
+use v5.12;
+
+# run <script> <number>
+
+use feature qw(signatures);
+no warnings qw(experimental::signatures);
+
+sub hshCount($n) {
+ $n == 0 && return "";
+ $n == 1 && return "#";
+ my $stub = $n . "#";
+ return hshCount($n - length($stub)) . $stub;
+}
+
+say hshCount($ARGV[0]);