aboutsummaryrefslogtreecommitdiff
path: root/challenge-156/ulrich-rieke/cpp/ch-1.cpp
diff options
context:
space:
mode:
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 ;
+}