From eb4ccd1d13b390f36d35102d0630e636caa6e74d Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 12 Jan 2022 11:56:55 +0000 Subject: - Added solutions by Ulrich Rieke. --- challenge-147/ulrich-rieke/cpp/ch-1.cpp | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 challenge-147/ulrich-rieke/cpp/ch-1.cpp (limited to 'challenge-147/ulrich-rieke/cpp/ch-1.cpp') diff --git a/challenge-147/ulrich-rieke/cpp/ch-1.cpp b/challenge-147/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..975ba98215 --- /dev/null +++ b/challenge-147/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +bool isPrime( int n ) { + if ( n == 1 ) + return false ; + if ( n == 2 ) + return true ; + int root = static_cast( floor( sqrt( static_cast( n ) ))) ; + for ( int i = 2 ; i < root + 1 ; i++ ) + if ( n % i == 0 ) + return false ; + return true ; +} + +bool isLeftTruncatablePrime( int n ) { + if ( ! isPrime( n ) ) + return false ; + std::string numberstring( std::to_string( n ) ) ; + auto found = numberstring.find( '0' ) ; + if ( found != std::string::npos ) + return false ; + int len = static_cast(numberstring.length( ) ) ; + for ( int i = 0 ; i < len ; i++ ) { + int num = std::stoi( numberstring.substr( i ) ) ; + if ( ! isPrime( num ) ) + return false ; + } + return true ; +} + +int main( ) { + std::vector leftTruncatables ; + int current = 0 ; + while ( leftTruncatables.size( ) < 20 ) { + current++ ; + if ( isLeftTruncatablePrime( current ) ) + leftTruncatables.push_back( current ) ; + } + for ( int i : leftTruncatables ) + std::cout << i << ' ' ; + std::cout << std::endl ; + return 0 ; +} -- cgit