diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-26 16:14:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-26 16:14:48 +0000 |
| commit | f76cc19c3f9ac573e8b645817153638403ffd80c (patch) | |
| tree | e00a356952124f2fb4167b01ad930db5e1858fc1 | |
| parent | 0b9ea18c4669d32532df0bb6aee0b94cb11061a6 (diff) | |
| parent | 4a60f293b696b8da32539511b1ee45caf3fa6110 (diff) | |
| download | perlweeklychallenge-club-f76cc19c3f9ac573e8b645817153638403ffd80c.tar.gz perlweeklychallenge-club-f76cc19c3f9ac573e8b645817153638403ffd80c.tar.bz2 perlweeklychallenge-club-f76cc19c3f9ac573e8b645817153638403ffd80c.zip | |
Merge pull request #5568 from E7-87-83/newt
Week 149 Task 2
| -rw-r--r-- | challenge-149/cheok-yin-fung/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-149/cheok-yin-fung/perl/ch-2.pl | 42 |
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-149/cheok-yin-fung/blog.txt b/challenge-149/cheok-yin-fung/blog.txt new file mode 100644 index 0000000000..49bab24a65 --- /dev/null +++ b/challenge-149/cheok-yin-fung/blog.txt @@ -0,0 +1 @@ +https://E7-87-83.github.io/coding/challenge_149.html diff --git a/challenge-149/cheok-yin-fung/perl/ch-2.pl b/challenge-149/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..5abce58fc2 --- /dev/null +++ b/challenge-149/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,42 @@ +# The Weekly Challenge 149 +# Task 2 Largest Square + +use v5.22.0; +use warnings; +use Math::BigInt; +use List::Util qw/uniq/; +use Test::More tests => 5; + +my $_N = $ARGV[0] || 3; + +find_sq($_N); + +sub find_sq { + + my $N = $_[0]; + + my $upper_sqrt = Math::BigInt->new($N)->bpow( int(($N+1)/2)); + my $sq = Math::BigInt->new($upper_sqrt)->bpow(2); + + my $bool = undef; + + do { + $upper_sqrt->bdec(); + $sq->bsub($upper_sqrt)->bsub($upper_sqrt)->bdec(); + my $sq_baseN = $sq->to_base($N); + my @arr = split "", $sq_baseN; + my $uniq_num = uniq @arr; + $bool = 1 if scalar @arr == $uniq_num; + } while (!$bool); + + say "in decimal base: ", $sq; + say "in base-N: ", $sq->to_base($N); + + return $sq; +} + +ok find_sq(2) == 1, "N = 2"; +ok find_sq(3) == 1, "N = 3"; +ok find_sq(7) == 751689, "N = 7"; +ok find_sq(8) == 10323369, "N = 8"; +ok find_sq(12) == 8706730814089, "N = 12"; |
