aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/athanasius/perl/ch-1.pl
blob: f76757eb3f6ccbb32a97642ba8db8f99664f9c6e (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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!perl

###############################################################################
=comment

Perl Weekly Challenge 136
=========================

TASK #1
-------
*Two Friendly*

Submitted by: Mohammad S Anwar

You are given 2 positive numbers, $m and $n.

Write a script to find out if the given two numbers are Two Friendly.

    Two positive numbers, m and n are two friendly when gcd(m, n) = 2 ^ p where
    p > 0. The greatest common divisor (gcd) of a set of numbers is the largest
    positive number that divides all the numbers in the set without remainder.

Example 1

    Input: $m = 8, $n = 24
    Output: 1

    Reason: gcd(8,24) = 8 => 2 ^ 3

Example 2

    Input: $m = 26, $n = 39
    Output: 0

    Reason: gcd(26,39) = 13

Example 3

    Input: $m = 4, $n = 10
    Output: 1

    Reason: gcd(4,10) = 2 => 2 ^ 1

=cut
###############################################################################

#--------------------------------------#
# Copyright © 2021 PerlMonk Athanasius #
#--------------------------------------#

#==============================================================================
=comment

Interface
---------
Include the flag "--verbose" (or just "-v") on the command line to display an
explanation of the output.

Implementation
--------------
Calculation of the greatest common divisor is delegated to the gcd() subroutine
in the ntheory (aka Math::Prime::Util) module.

=cut
#==============================================================================

use strict;
use warnings;
use Const::Fast;
use Getopt::Long;
use ntheory        qw( gcd );
use Regexp::Common qw( number );

const my $USAGE =>
"Usage:
  perl $0 [--verbose|-v] <m> <n>

    --verbose    Explain the output?
    <m>          An integer > 0
    <n>          An integer > 0\n";

#------------------------------------------------------------------------------
BEGIN
#------------------------------------------------------------------------------
{
    $| = 1;
    print "\nChallenge 136, Task #1: Two Friendly (Perl)\n\n";
}

#==============================================================================
MAIN:
#==============================================================================
{
    my ($verbose, $m, $n) = parse_command_line();

    print "Input:  \$m = $m, \$n = $n\n";

    my $friendly = 0;
    my $reason   = 'not a power of 2';
    my $gcd      = gcd( $m, $n );

    if ($gcd == 1)
    {
        $reason = '2 ^ 0';
    }
    else
    {
        my $log2 = int( (log( $gcd ) / log( 2 )) + 0.5 );

        if ($gcd == 2 ** $log2)
        {
            $friendly = 1;
            $reason   = "2 ^ $log2";
        }
    }

    printf "Output: %d\n", $friendly ? 1 : 0;

    print "\nReason: gcd($m, $n) = $gcd which is $reason\n" if $verbose;
}

#------------------------------------------------------------------------------
sub parse_command_line
#------------------------------------------------------------------------------
{
    my $verbose = 0;

    GetOptions( verbose => \$verbose )
               or error( 'Invalid command line flag' );

    my $args = scalar @ARGV;
       $args == 2
               or error( "Expected 2 command line arguments, found $args" );

    my ($m, $n) = @ARGV;

    for ($m, $n)
    {
        / ^ $RE{num}{int} $ /x
               or error( qq["$_" is not a valid integer] );

        $_ > 0 or error( qq["$_" is not greater than zero] );
    }

    return ($verbose, $m, $n);
}

#------------------------------------------------------------------------------
sub error
#------------------------------------------------------------------------------
{
    my ($message) = @_;

    die "ERROR: $message\n$USAGE";
}

###############################################################################