aboutsummaryrefslogtreecommitdiff
path: root/challenge-156/ulrich-rieke/cpp/ch-1.cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-03-16 00:02:05 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-03-16 00:02:05 +0000
commit47cc91ffed790a5373e7227d50cbf5e5c68f40ef (patch)
tree2dbefa97d7df8d03aad0bae43b39eae265224097 /challenge-156/ulrich-rieke/cpp/ch-1.cpp
parent081071c9a5eb941107c40373971d46f700d1421d (diff)
downloadperlweeklychallenge-club-47cc91ffed790a5373e7227d50cbf5e5c68f40ef.tar.gz
perlweeklychallenge-club-47cc91ffed790a5373e7227d50cbf5e5c68f40ef.tar.bz2
perlweeklychallenge-club-47cc91ffed790a5373e7227d50cbf5e5c68f40ef.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-156/ulrich-rieke/cpp/ch-1.cpp')
-rw-r--r--challenge-156/ulrich-rieke/cpp/ch-1.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-156/ulrich-rieke/cpp/ch-1.cpp b/challenge-156/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..abcbd0512b
--- /dev/null
+++ b/challenge-156/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,45 @@
+#include <vector>
+#include <iostream>
+#include <algorithm>
+#include <cmath>
+
+bool isPrime( int n ) {
+ if ( n == 1 )
+ return false ;
+ if ( n == 2 )
+ return true ;
+ int root = static_cast<int>( floor( sqrt( static_cast<double>( n ) ))) ;
+ for ( int i = 2 ; i < root + 1 ; i++ )
+ if ( n % i == 0 )
+ return false ;
+ return true ;
+}
+
+std::vector<int> toBinary( int n ) {
+ std::vector<int> binaryDigits ;
+ while ( n != 0 ) {
+ binaryDigits.push_back( n % 2 ) ;
+ n /= 2 ;
+ }
+ return binaryDigits ;
+}
+
+bool isPernicious( int n ) {
+ std::vector<int> binaryDigits { toBinary( n ) } ;
+ return isPrime( std::count( binaryDigits.begin( ) , binaryDigits.end( ) ,
+ 1 )) ;
+}
+
+int main( ) {
+ std::vector<int> perniciousNumbers ;
+ int current = 0 ;
+ while ( perniciousNumbers.size( ) != 10 ) {
+ current++ ;
+ if ( isPernicious( current ) )
+ perniciousNumbers.push_back( current ) ;
+ }
+ for ( int i : perniciousNumbers )
+ std::cout << i << ' ' ;
+ std::cout << std::endl ;
+ return 0 ;
+}