diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-01-12 15:59:26 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-01-12 15:59:26 +0000 |
| commit | dd99d53e09a332c4ff5153b8bb54c9506a03860d (patch) | |
| tree | e65264cbef15fbcd3e15dd12da74cc5084e507cb /challenge-147/ulrich-rieke/cpp/ch-1.cpp | |
| parent | 94cdbab3f1198c78a3dcedb9ada1a8596114cfcb (diff) | |
| parent | d995c005f623c0f80f4cdfbf2247856c146fd7e3 (diff) | |
| download | perlweeklychallenge-club-dd99d53e09a332c4ff5153b8bb54c9506a03860d.tar.gz perlweeklychallenge-club-dd99d53e09a332c4ff5153b8bb54c9506a03860d.tar.bz2 perlweeklychallenge-club-dd99d53e09a332c4ff5153b8bb54c9506a03860d.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-147/ulrich-rieke/cpp/ch-1.cpp')
| -rw-r--r-- | challenge-147/ulrich-rieke/cpp/ch-1.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
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 <iostream> +#include <string> +#include <cmath> +#include <vector> + +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 ; +} + +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<int>(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<int> 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 ; +} |
