diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-04 03:10:20 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-06-04 03:10:20 +0100 |
| commit | 7bcf2d3292c4a1bfc064ddbb7c6806248e243ef4 (patch) | |
| tree | f94008cf9791e97dd910493218c2bf754f48a24f /challenge-167 | |
| parent | 8e5a4c3795924e266b130e60511d3a5fea54eb77 (diff) | |
| download | perlweeklychallenge-club-7bcf2d3292c4a1bfc064ddbb7c6806248e243ef4.tar.gz perlweeklychallenge-club-7bcf2d3292c4a1bfc064ddbb7c6806248e243ef4.tar.bz2 perlweeklychallenge-club-7bcf2d3292c4a1bfc064ddbb7c6806248e243ef4.zip | |
- Added solution by Ulrich Rieke.
Diffstat (limited to 'challenge-167')
| -rw-r--r-- | challenge-167/ulrich-rieke/haskell/ch-1.hs | 26 | ||||
| -rw-r--r-- | challenge-167/ulrich-rieke/raku/ch-1.raku | 27 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-167/ulrich-rieke/haskell/ch-1.hs b/challenge-167/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..b54ca89ec9 --- /dev/null +++ b/challenge-167/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,26 @@ +module Challenge167 + where + +isPrime :: Int -> Bool +isPrime n + |n == 2 = True + |n == 1 = False + |otherwise = null $ filter (\i -> mod n i == 0 ) [2 .. root] + where + root :: Int + root = floor $ sqrt $ fromIntegral n + +findCycle :: Int -> [Int] +findCycle n = (takeWhile (/= n ) $ iterate step ( step n )) ++ [n] +where + step :: Int -> Int + step d = read ( tail s ++ [head s] ) + where + s = show d + +solution :: [Int] +solution = take 10 $ filter (\i -> (noNull i ) && ( all (\d -> isPrime d +&& d >= i ) $ findCycle i )) [100 , 101 ..] + where + noNull :: Int -> Bool + noNull d = not ( elem '0' $ show d ) diff --git a/challenge-167/ulrich-rieke/raku/ch-1.raku b/challenge-167/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..607adff4f5 --- /dev/null +++ b/challenge-167/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,27 @@ +use v6 ; + +sub findCycles( Int $n is copy ) { + my $numberstring = ~$n ; + my @cycle ; + my $rotated = $numberstring.substr( 1 ) ~ $numberstring.substr( 0 , 1 ) ; + @cycle.push( +$rotated ) ; + while ( $rotated ne $numberstring ) { + $rotated = $rotated.substr( 1 ) ~ $rotated.substr(0 , 1 ) ; + @cycle.push( +$rotated ) ; + } + return @cycle ; +} + +my @circularPrimes ; +my $current = 100 ; +while ( @circularPrimes.elems != 10 ) { + if ( ~$current !~~ /0/ ) { + my @cycle = findCycles( $current ) ; + my @selected = grep ( { $_ >= $current } ) , @cycle ; + if ( (so is-prime @cycle.all) && (@selected.elems == @cycle.elems) ) { + @circularPrimes.push( $current ) ; + } + } + $current++ ; +} +say @circularPrimes.join( ', ' ) ; |
