2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -0,0 +1,4 @@
hailstone n
| n == 1 = 1
| even n = n `div` 2
| odd n = 3*n + 1

View file

@ -0,0 +1,2 @@
data Environment = Environment { count :: Int, value :: Int }
deriving Eq

View file

@ -0,0 +1 @@
environments = [ Environment 0 n | n <- [1..12] ]

View file

@ -0,0 +1,2 @@
process (Environment c 1) = Environment c 1
process (Environment c n) = Environment (c+1) (hailstone n)

View file

@ -0,0 +1,5 @@
process = execState $ do
n <- gets value
c <- gets count
when (n > 1) $ modify $ \env -> env { count = c + 1 }
modify $ \env -> env { value = hailstone n }

View file

@ -0,0 +1,13 @@
fixedPoint f x
| fx == x = [x]
| otherwise = x : fixedPoint f fx
where fx = f x
prettyPrint field = putStrLn . foldMap (format.field)
where format n = (if n < 10 then " " else "") ++ show n ++ " "
main = do
let result = fixedPoint (map process) environments
mapM_ (prettyPrint value) result
putStrLn (replicate 36 '-')
prettyPrint count (last result)

View file

@ -0,0 +1,6 @@
main = do
let result = map (fixedPoint process) environments
mapM_ (prettyPrint value) result
putStrLn (replicate 36 '-')
putStrLn "Counts: "
prettyPrint (count . last) result

View file

@ -2,14 +2,14 @@ my $calculator = sub ($n is rw) {
return ($n == 1) ?? 1 !! $n %% 2 ?? $n div 2 !! $n * 3 + 1
};
sub next (%this is rw, &get_next) {
sub next (%this, &get_next) {
return %this if %this.<value> == 1;
%this.<value>.=&get_next;
%this.<count>++;
return %this;
};
my @hailstones = map { $_ = %(value => $_, count => 0) }, 1 .. 12;
my @hailstones = map { %(value => $_, count => 0) }, 1 .. 12;
while not all( map { $_.<value> }, @hailstones ) == 1 {
say [~] map { $_.<value>.fmt("%4s") }, @hailstones;