diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
| commit | 49f7f459092f538b5468e255ff4e8ebac490d70a (patch) | |
| tree | fa4a94a8207a58a56d4dca98ff22d39bed047f6e /challenge-159/ulrich-rieke/cpp/ch-2.cpp | |
| parent | 1711da4f548d3134248cd5e02a13d71762d5e4cb (diff) | |
| parent | 72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff) | |
| download | perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.gz perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.bz2 perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.zip | |
Merge remote-tracking branch 'upstream/master'
# Conflicts:
# challenge-160/paulo-custodio/Makefile
Diffstat (limited to 'challenge-159/ulrich-rieke/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-159/ulrich-rieke/cpp/ch-2.cpp | 41 |
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 ; +} |
