aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-15 19:55:20 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-15 19:55:20 +0000
commit5f1b4ae531406be166a2e4d7e8d36bb23f689a23 (patch)
treedaadf7e47f6ceac35e510dafad79abdbcb0da218
parent00df103b9caa3f0eccbde85603bd40613641402c (diff)
downloadperlweeklychallenge-club-5f1b4ae531406be166a2e4d7e8d36bb23f689a23.tar.gz
perlweeklychallenge-club-5f1b4ae531406be166a2e4d7e8d36bb23f689a23.tar.bz2
perlweeklychallenge-club-5f1b4ae531406be166a2e4d7e8d36bb23f689a23.zip
- Tidied up solution to Stealthy Numbers by Robert DiCicco.
-rw-r--r--challenge-143/robert-dicicco/perl/ch-2.pl38
1 files changed, 16 insertions, 22 deletions
diff --git a/challenge-143/robert-dicicco/perl/ch-2.pl b/challenge-143/robert-dicicco/perl/ch-2.pl
index 3bd0ea1527..8f9027516f 100644
--- a/challenge-143/robert-dicicco/perl/ch-2.pl
+++ b/challenge-143/robert-dicicco/perl/ch-2.pl
@@ -4,10 +4,11 @@ use strict;
use warnings;
use ntheory qw/divisors/;
use IO::Prompter;
+use 5.30.0;
### Author: Robert DiCicco
-### Date: 13-DEC-2021
-### Challenge #143 Stealthy Number
+### Date: 15-DEC-2021
+### Challenge #143 Stealthy Number v 2.0
# initialize vars
# array to hold list of divisors
@@ -16,9 +17,6 @@ my @final = ();
# sum of initial set of divisors
my $target = 0;
-# limit to single digits
-my $limit = 10;
-
# Get number from user
my $inp = prompt 'Input the number : ', -integer => [ 1 .. 999 ];
chomp($inp);
@@ -28,30 +26,26 @@ $inp = int($inp);
my @d = divisors($inp);
# get divisors and their sums
-foreach(sort @d){
- #skip if divisor is greater than $limit
- next if($_ >= $limit);
-
+foreach(@d){
my $val = $inp / $_;
+ # skip the opposite pair (e.g. 3,4 but skip 4,3)
+ next if ($val > $_);
+ my $digit_sum = $val + $_;
+
+ if($target == 0){
+ $target = $digit_sum;
+ push(@final,$val,$_);
+ }
- # is single digit
- if($val < $limit) {
- my $digit_sum = $val + $_;
- if($target == 0){
- $target = $digit_sum;
- push(@final,$val,$_);
- }
-
- if($digit_sum == ($target - 1)){
- push(@final,$val,$_);
- last;
- }
+ if($digit_sum == ($target + 1)){
+ push(@final,$val,$_);
+ last;
}
}
# if we have 4 divisors that meet the criteria, print
if(scalar(@final) == 4){
- print("$final[0] x $final[1] = $final[2] x $final[3] and $final[0] + $final[1] = $final[2] + $final[3] + 1\n");
+ print("$final[2] x $final[3] = $final[0] x $final[1] and $final[2] + $final[3] = $final[0] + $final[1] + 1");
} else {
print("Not Stealthy!\n");
}