diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-13 18:07:02 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-12-13 18:07:02 +0000 |
| commit | ecafe0e840b90c1462ec4ac924f3255dd3beb2c0 (patch) | |
| tree | d306fbdaac931ca65c37db81d939ea44113f1356 /challenge-143 | |
| parent | ffebe35bdbcc79bf7092cd82f9da9c4d426f4beb (diff) | |
| download | perlweeklychallenge-club-ecafe0e840b90c1462ec4ac924f3255dd3beb2c0.tar.gz perlweeklychallenge-club-ecafe0e840b90c1462ec4ac924f3255dd3beb2c0.tar.bz2 perlweeklychallenge-club-ecafe0e840b90c1462ec4ac924f3255dd3beb2c0.zip | |
- Added solution by Robert DiCicco.
Diffstat (limited to 'challenge-143')
| -rw-r--r-- | challenge-143/robert-dicicco/perl/ch-2.pl | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-143/robert-dicicco/perl/ch-2.pl b/challenge-143/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..3bd0ea1527 --- /dev/null +++ b/challenge-143/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!perl.exe + +use strict; +use warnings; +use ntheory qw/divisors/; +use IO::Prompter; + +### Author: Robert DiCicco +### Date: 13-DEC-2021 +### Challenge #143 Stealthy Number + +# initialize vars +# array to hold list of divisors +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); +$inp = int($inp); + +# calculate list of divisors +my @d = divisors($inp); + +# get divisors and their sums +foreach(sort @d){ + #skip if divisor is greater than $limit + next if($_ >= $limit); + + my $val = $inp / $_; + + # 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 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"); +} else { + print("Not Stealthy!\n"); +} |
