/
ch-1.pl
executable file
·56 lines (48 loc) · 1.04 KB
/
ch-1.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
52
53
54
55
56
#!/usr/bin/perl
# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
use v5.26; # The Weekly Challenge - 2024-01-29
use utf8; # Week 254 task 1 - Three power
use strict; # Peter Campbell Smith
use warnings;
binmode STDOUT, ':utf8';
three_power(27);
three_power(0);
three_power(6);
three_power(1);
three_power(1853020188851840);
three_power(1853020188851841);
three_power(2197000000000000);
three_power(205891132094649);
sub three_power {
# initialise
my ($n, $j, $try, $limit);
$n = shift;
say qq[\nInput: $n];
if ($n > 2197000000000000) {
say qq[-- too big (max is 2197000000000000)];
return;
}
# powers of 3
$try = 1;
for $j (0 .. 33) {
if ($try == $n) {
say qq[Output: true, $n is 3 ** $j];
last;
} elsif ($try > $n) {
say qq[Output: false, $n is not a power of 3];
last;
}
$try *= 3;
}
# cubes
for $j (0 .. 130000) {
$try = $j ** 3;
if ($try == $n) {
say qq[ true, $n is $j ** 3];
last;
} elsif ($try > $n) {
say qq[ false, $n is not a perfect cube];
last;
}
}
}