aboutsummaryrefslogtreecommitdiff
path: root/challenge-085/pkmnx/c++/ch-2/ch-2.cpp
blob: bcb18bbd92c8ff47df61791ae12f4d0cffd8ecd2 (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
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>

using namespace std;

int main() {

   long n;
   cin >> n;

   if ( n < 0 ) { 
      cout << "Input: " << n << " is not positive." << endl;
      exit(0);
   }
   if ( n > INT_MAX ) { 
      cout << "Input: " << n << " is greater than INT_MAX: " << INT_MAX << endl;
      exit(0);
   }

   cout << "Input: " << n << endl;

   for ( int i = 2; i <= n; ++i ) {
      if ( n % i == 0 ) {

         for ( int j = 2; j < INT_MAX; j++ ) {

            long ij = 1;
            for ( int k = 0; k < j; k++ ) {
               ij *= i;
               if ( ij > n ) { break; }
            }
            if ( ij > n ) { break; }

            if ( ij == n ) {
               cout << "Output: 1 as " << n << " = " << i << " ^ " << j << endl;
               exit(0);
            }

         }
      
      }
   }

   cout << "Output: 0" << endl;
   return 0;
}