/
ch-2.pl
51 lines (41 loc) · 1.05 KB
/
ch-2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!perl.exe
use strict;
use warnings;
use ntheory qw/divisors/;
use IO::Prompter;
use 5.30.0;
### Author: Robert DiCicco
### Date: 15-DEC-2021
### Challenge #143 Stealthy Number v 2.0
# initialize vars
# array to hold list of divisors
my @final = ();
# sum of initial set of divisors
my $target = 0;
# 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(@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,$_);
}
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[2] x $final[3] = $final[0] x $final[1] and $final[2] + $final[3] = $final[0] + $final[1] + 1");
} else {
print("Not Stealthy!\n");
}