aboutsummaryrefslogtreecommitdiff
path: root/challenge-167
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-05-31 22:43:41 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-05-31 22:43:41 +0100
commit75f3dd4cc41b9155b1b40abba6cc3d4cbfec7a20 (patch)
treea1ebdb15aaa7297e44c488c4a96730da3594d987 /challenge-167
parentf47bded222d82e6e94c2bede43849b67d66b6aee (diff)
downloadperlweeklychallenge-club-75f3dd4cc41b9155b1b40abba6cc3d4cbfec7a20.tar.gz
perlweeklychallenge-club-75f3dd4cc41b9155b1b40abba6cc3d4cbfec7a20.tar.bz2
perlweeklychallenge-club-75f3dd4cc41b9155b1b40abba6cc3d4cbfec7a20.zip
- Added solution by Ulrich Rieke.
Diffstat (limited to 'challenge-167')
-rw-r--r--challenge-167/ulrich-rieke/cpp/ch-1.cpp86
-rw-r--r--challenge-167/ulrich-rieke/perl/ch-1.pl44
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-167/ulrich-rieke/cpp/ch-1.cpp b/challenge-167/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..92463670fc
--- /dev/null
+++ b/challenge-167/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,86 @@
+#include <iostream>
+#include <map>
+#include <list>
+#include <algorithm>
+#include <vector>
+#include <cmath>
+
+bool isPrime( int number ) {
+ int stop = std::sqrt( static_cast<double>( number ) ) ;
+ for ( int i = 2 ; i <= stop ; ++i )
+ if ( number % i == 0 )
+ return false ;
+ return true ;
+}
+
+std::list<int> toList( int n ) {
+ std::list<int> digits ;
+ while ( n != 0 ) {
+ digits.push_back( n % 10 ) ;
+ n /= 10 ;
+ }
+ return digits ;
+}
+//from list to int
+int toInt( const std::list<int> & digits ) {
+ int sum = 0 ;
+ int multiplier = 1 ;
+ for ( auto it = digits.begin( ) ; it != digits.end( ) ; ++it ) {
+ sum += multiplier * *it ;
+ multiplier *= 10 ;
+ }
+ return sum ;
+}
+
+//create cycled digits per performing the rotations on a list of digits
+std::list<int> createCycle( int n ) {
+ std::list<int> cycle ;
+ std::list<int> digits( toList( n ) ) ;
+ cycle.push_back( n ) ;
+ digits.push_back( digits.front( ) ) ;
+ digits.pop_front( ) ;
+ int curnum = toInt( digits ) ;
+ while ( curnum != n ) {
+ cycle.push_back( curnum ) ;
+ digits.push_back( digits.front( ) ) ;
+ digits.pop_front( ) ;
+ curnum = toInt( digits ) ;
+ }
+ return cycle ;
+}
+
+bool isCircularPrime( int n ) {
+ std::list<int> cycle( createCycle( n ) ) ;
+ return std::all_of( cycle.begin( ) , cycle.end( ) , isPrime ) ;
+}
+
+int main( ) {
+ std::vector<int> circularPrimes ;
+ int current = 100 ;
+ std::map<int , int> primesSoFar ;
+ while ( circularPrimes.size( ) != 10 ) {
+ std::list<int> currentDigits( toList( current ) ) ;
+ if ( std::find( currentDigits.begin( ) , currentDigits.end( ) , 0 )
+ == currentDigits.end( ) ) {//we don't want 0's in the number
+ if ( isCircularPrime( current )) {
+ //the following function call may be regarded as wasteful
+ //considering what I did previously ; it's for the sake of
+ //clarity
+ std::list<int> currentCycle = createCycle( current ) ;
+ if ( std::all_of( currentCycle.begin( ) , currentCycle.end( ) ,
+ [&primesSoFar]( int i ) { return primesSoFar.find( i ) ==
+ primesSoFar.end( ) ; } )) {//all cycled numbers not found
+ //so far ?
+ circularPrimes.push_back( current ) ;
+ for ( int i : currentCycle )
+ primesSoFar[ i ]++ ;//add them to the numbers found
+ }
+ }
+ }
+ current++ ;
+ }
+ for ( int i : circularPrimes )
+ std::cout << i << ' ' ;
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-167/ulrich-rieke/perl/ch-1.pl b/challenge-167/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..a198049bd0
--- /dev/null
+++ b/challenge-167/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( all none ) ;
+use POSIX ;
+
+sub createCycle {
+ my $num = shift ;
+ my @cycle ;
+ push @cycle , $num ;
+ my $rotated = substr( $num , 1 ) . substr( $num , 0 , 1 ) ;
+ while ( $rotated ne $num ) {
+ push @cycle , $rotated ;
+ $rotated = substr( $rotated , 1 ) . substr( $rotated , 0 , 1 ) ;
+ }
+ return @cycle ;
+}
+
+sub isPrime {
+ my $number = shift ;
+ my $stop = sqrt( $number ) ;
+ for my $i ( 2 .. floor( $stop )) {
+ if ( $number % $i == 0 ) {
+ return 0 ;
+ }
+ }
+ return 1 ;
+}
+
+my @circularPrimes ;
+my %circularsSeen ;
+my $current = 100 ;
+while ( scalar( @circularPrimes ) != 10 ) {
+ if ( $current !~ /0/ ) {
+ my @cycle = createCycle( $current ) ;
+ if ( (all { isPrime( $_ ) } @cycle) && (none { $circularsSeen{ $_ } } @cycle )) {
+ push @circularPrimes , $current ;
+ map { $circularsSeen{ $_ }++ } @cycle ;
+ }
+ }
+ $current++ ;
+}
+say join( ', ' , @circularPrimes ) ;