aboutsummaryrefslogtreecommitdiff
path: root/challenge-159/ulrich-rieke/cpp/ch-2.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-159/ulrich-rieke/cpp/ch-2.cpp')
-rw-r--r--challenge-159/ulrich-rieke/cpp/ch-2.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-159/ulrich-rieke/cpp/ch-2.cpp b/challenge-159/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..85ae8382fe
--- /dev/null
+++ b/challenge-159/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,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 ;
+}