2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -0,0 +1,4 @@
|
|||
hailstone n
|
||||
| n == 1 = 1
|
||||
| even n = n `div` 2
|
||||
| odd n = 3*n + 1
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
data Environment = Environment { count :: Int, value :: Int }
|
||||
deriving Eq
|
||||
|
|
@ -0,0 +1 @@
|
|||
environments = [ Environment 0 n | n <- [1..12] ]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
process (Environment c 1) = Environment c 1
|
||||
process (Environment c n) = Environment (c+1) (hailstone n)
|
||||
|
|
@ -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 }
|
||||
|
|
@ -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)
|
||||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue