/
ch-1.pl
67 lines (54 loc) · 1.94 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
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# Copyright (c) 2024, Bob Lied
#=============================================================================
# ch-1.pl Perl Weekly Challenge 254 Task 1 Three Power
#=============================================================================
# You are given a positive integer, $n. Write a script to return true if
# the given integer is a power of three otherwise return false.
# Example 1 Input: $n = 27 Output: true
# Example 2 Input: $n = 0 Output: true
# Example 3 Input: $n = 6 Output: false
#=============================================================================
use v5.38;
use builtin qw/true false/; no warnings "experimental::builtin";
use Getopt::Long;
my $Verbose = 0;
my $DoTest = 0;
GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
exit(!runTest()) if $DoTest;
say isPof3(shift) ? "true" : "false";
sub isPof3($n)
{
if ( $n > 0 ) { $n /= 3 while ( $n % 3 == 0 );
return $n == 1;
}
elsif ( $n == 0 ) { return true }
else { return false; }
}
sub runTest
{
use Test2::V0;
use builtin qw/true false/; no warnings "experimental::builtin";
is( isPof3(27), true, "Example 1");
is( isPof3( 0), true, "Example 2");
is( isPof3( 6), false, "Example 3");
my $p3 = Power->new(base => 3);
is( $p3->isPowerOf(27), true, "Example 1 with class");
is( $p3->isPowerOf( 0), true, "Example 2 with class");
is( $p3->isPowerOf( 6), false, "Example 3 with class");
done_testing;
}
use feature "class"; no warnings "experimental::class";
class Power
{
field $base :param = 10;
method isPowerOf($n) {
if ( $n > 0 ) { $n /= $base while ( $n % $base == 0 );
return $n == 1;
}
elsif ( $n == 0 ) { return true }
else { return false; }
}
}