aboutsummaryrefslogtreecommitdiff
path: root/challenge-085/paulo-custodio/python/ch-2.py
blob: ef869c12f88c9a4700c37595178c57e0e38fc21a (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
#!/usr/bin/env python3

# Challenge 085
#
# 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

import sys

def get_prime_factors(n):
    i = 2
    prime_factors = []
    while i*i <= n:
        if n%i == 0:
            prime_factors.append(i)
            n //= i
        else:
            i += 1

    if n>1:
        prime_factors.append(n)

    return prime_factors

def is_perfect_power(n):
    factors = list(set(get_prime_factors(n)))
    return len(factors)==1

print(1 if is_perfect_power(int(sys.argv[1])) else 0)