aboutsummaryrefslogtreecommitdiff
path: root/challenge-066/noud/raku/ch-2.p6
blob: e603d5501966bc2455906480d44e2baa13c8de9d (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
# You are given an integer $N.
#
# Write a script to check if the given number can be expressed as mn where m
# and n are positive integers. Otherwise print 0.
#
# Please make sure m > 1 and n > 1.
#
# BONUS: If there are more than one ways to express the given number then print
# all possible solutions.
#
# Example 1:
#
# For given $N = 9, it should print 32 or 3^2.
# Example 2:
#
# For given $N = 45, it should print 0.
# Example 3:
#
# For given $N = 64, it should print all or one of 8^2 or 2^6 or 4^3.

sub power-int($n) {
    my @r = ((2..$n**.5) X (2..$n**.5)).grep({ $_[0] ** $_[1] eq $n});

    if (@r.elems == 0) {
        say 0;
    } else {
        say "$_[0]^$_[1]" for @r;
    }
}

say "Example 1, N=9:";
power-int(9);

say "Example 2, N=45:";
power-int(45);

say "Example 3, N=64:";
power-int(64);