diff options
| -rw-r--r-- | challenge-054/ulrich-rieke/cpp/ch-1.cpp | 31 | ||||
| -rw-r--r-- | challenge-054/ulrich-rieke/haskell/ch-2.hs | 32 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-054/ulrich-rieke/cpp/ch-1.cpp b/challenge-054/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..9a0b50410e --- /dev/null +++ b/challenge-054/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,31 @@ +#include <vector> +#include <iostream> +#include <algorithm> +#include <cstdlib> +#include <numeric> + +int main( int argc, char * argv[] ) { + if ( argc < 3 ) { + std::cout << "Error! 2 integers expected!\n" ; + return 1 ; + } + int limit = std::atoi( argv[1] ) ; + int permu = std::atoi( argv[ 2 ] ) ; + std::vector<int> numbers( limit ) ; + std::iota( numbers.begin( ) , numbers.end( ) , 1 ) ; + std::vector<std::vector<int> > permutations ; + do { + permutations.push_back( numbers ) ; + } while ( std::next_permutation( numbers.begin( ) , numbers.end( )) ) ; + if ( permutations.size( ) < permu ) { + std::cout << "[]" << std::endl ; + } + else { + std::cout << "[ " ; + for ( int i : permutations[ permu - 1 ] ) { + std::cout << i << " " ; + } + std::cout << "]\n" ; + } + return 0 ; +} diff --git a/challenge-054/ulrich-rieke/haskell/ch-2.hs b/challenge-054/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..2d1fac4016 --- /dev/null +++ b/challenge-054/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,32 @@ +module Collatz + where +import Control.Monad.State.Lazy +import Data.List( sortBy ) + +findSequence :: State ( Int , [Int] ) [Int] +findSequence = do + ( n, sequence ) <- get + if n == 1 + then return (sequence ) + else do + put (nextNum n , sequence ++ [nextNum n]) + findSequence + + +nextNum :: Int -> Int +nextNum n + |even n = div n 2 + |odd n = n * 3 + 1 + +numbersequence :: Int -> [Int] +numbersequence n = evalState findSequence ( n , [] ) + +solution :: [(Int, Int)] +solution = take 20 $ sortBy mySorter $ map (\i -> (i , length $ numbersequence i) ) [1..1000000] + where + mySorter :: (Int, Int ) -> (Int , Int ) -> Ordering + mySorter p1 p2 + |snd p1 > snd p2 = LT + |snd p1 == snd p2 = EQ + |snd p1 < snd p2 = GT + |
