aboutsummaryrefslogtreecommitdiff
path: root/challenge-066/craig/perl/ch-2.pl
blob: a847a8d201abfc860a78eff2723cd499e7fd9da7 (plain)
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
use strict;
use v5.16;

my $n = shift;
die "\nMust provide positive integer.\n" if !$n || $n < 0;
my $exp = 1;
my $result;

# Work up to $n in powers of 2
do {
    $result = 2 ** $exp;
    $exp++;
} while ($result < $n);

# Find nth root up to $exp
my $i = 2;
while ($i <= $exp) {
    $result = $n ** (1/$i);
    if ($result !~ /\D/) {
        say "$result^$i";
        exit;
    } else {
        $i++;
    }
}

say "0";