aboutsummaryrefslogtreecommitdiff
path: root/challenge-159/ulrich-rieke/cpp/ch-2.cpp
blob: 85ae8382fece8a4fee9ef5163db29999687c562d (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
#include <iostream>
#include <vector>
#include <set>
#include <cstdlib>

//the smallest current divisor of a number is a prime number
std::vector<int> primeDecompose( int n ) {
  std::vector<int> primes ;
  int current = 2 ;
  while ( n != 1 ) {
      if ( n % current == 0 ) {
    primes.push_back( current ) ;
    n /= current ;
      }
      else
    current++ ;
  }
  return primes ;
}

int main( int argc, char * argv[] ) {
  int n = std::atoi( argv[1] ) ;
  while ( n <= 0 ) {
      std::cout << "Please enter a number greater than 0!\n" ;
      std::cin >> n ;
  }
  std::vector<int> primes { primeDecompose( n ) } ;
  std::set<int> uniqueNums( primes.begin( ) , primes.end( ) ) ;
  int pl = primes.size( ) ;
  int ul = uniqueNums.size( ) ;
  if ( pl == ul ) {
      if ( pl % 2 == 0 )
    std::cout << 1 ;
      else
    std::cout << -1 ;
  }
  else
      std::cout << 0 ;
  std::cout << std::endl ;
  return 0 ;
}