aboutsummaryrefslogtreecommitdiff
path: root/challenge-085/lars-thegler/perl/ch-2.pl
blob: da3406a56b55026f6df04496cbd2d01d950b37e3 (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
#!/usr/bin/env perl

use Modern::Perl;

# TASK #2 › Power of Two Integers
# Submitted by: Mohammad S Anwar
# You are given a positive integer $N.

# Write a script to find if it can be expressed as a ^ b where a > 0 and b > 1. Print 1 if you succeed otherwise 0.

# Example 1:
# Input: 8
# Output: 1 as 8 = 2 ^ 3
# Example 2:
# Input: 15
# Output: 0
# Example 3:
# Input: 125
# Output: 1 as 125 = 5 ^ 3

my $N = shift;

if ( $N == 1 ) {
    say 1;
    exit;
}

for my $a ( 2 .. sqrt $N ) {
    my $b = 1;
    my $p;
    do {
        $b++;
        $p = $a**$b;
    } until $p >= $N;
    if ( $p == $N ) {
        say 1;
        exit;
    }
}
say 0;