aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-11-12 23:16:59 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-11-12 23:16:59 +0800
commitaae8862469f431e7c08dc668961cc7a4fe1e4593 (patch)
tree0b77796a93ead78556e452c7dc1a15b40636fad0
parent0589e7e4202ba4f67287490b24bf4a49fbe2450b (diff)
downloadperlweeklychallenge-club-aae8862469f431e7c08dc668961cc7a4fe1e4593.tar.gz
perlweeklychallenge-club-aae8862469f431e7c08dc668961cc7a4fe1e4593.tar.bz2
perlweeklychallenge-club-aae8862469f431e7c08dc668961cc7a4fe1e4593.zip
Week 138 Task 2
-rw-r--r--challenge-138/perl/ch-2.pl78
1 files changed, 78 insertions, 0 deletions
diff --git a/challenge-138/perl/ch-2.pl b/challenge-138/perl/ch-2.pl
new file mode 100644
index 0000000000..3cf1916ec2
--- /dev/null
+++ b/challenge-138/perl/ch-2.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/perl
+# The Weekly Challenge 138
+# Task 2 Split Number
+# Usage: ch-2.pl $N
+use v5.12.0;
+use warnings;
+use List::Util qw/ any sum /;
+use Integer::Partition;
+use Algorithm::Combinatorics qw / permutations /;
+use Test::More tests => 3;
+
+my $NUM = $ARGV[0] || 1;
+
+say split_number($NUM);
+
+
+
+sub split_number {
+ my $N = $_[0];
+ my $rt = sqrt $N;
+ die "Input must be a square number\n" unless $rt == int $rt;
+ my $len = length $N;
+ my $upper = length $rt;
+
+ my %wlen;
+
+
+ my $i = Integer::Partition->new($len);
+ while (my $a = $i->next) {
+ next if any { $_ > $upper } @$a;
+ my $j = permutations($a);
+ while (my $b = $j->next) {
+ if (!defined($wlen{join ",", @$b})) {
+ my @config = ( substr($N ,0, $b->[0]) );
+ my $acc = 0;
+ for my $k (0..scalar @$b - 2) {
+ $acc += $b->[$k];
+ my $temp = substr($N, $acc, $b->[$k+1]);
+ $temp =~ s/^[0]*//g;
+ $temp = 0 if $temp eq "";
+ push @config, $temp;
+ }
+ if ( (sum @config) == $rt) {
+ say "sqrt($N) = " ,
+ "$rt = ",
+ join " + ", map { $_ == 0 ? "(0)" : $_ } @config;
+ return 1;
+ }
+ $wlen{join ",", @$b} = 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+ok split_number(81) == 1, "Example 1";
+ok split_number(9801) == 1, "Example 2";
+ok split_number(36) == 0, "Example 3";
+
+
+=pod for fun
+for my $num (1..100) {
+ split_number($num*$num);
+}
+
+Output:
+sqrt(1) = 1 = 1
+sqrt(81) = 9 = 8 + 1
+sqrt(100) = 10 = 10 + 0
+sqrt(1296) = 36 = 1 + 29 + 6
+sqrt(2025) = 45 = 20 + 25
+sqrt(3025) = 55 = 30 + 25
+sqrt(6724) = 82 = 6 + 72 + 4
+sqrt(8281) = 91 = 82 + 8 + 1
+sqrt(9801) = 99 = 98 + 01
+sqrt(10000) = 100 = 100 + 00
+