Skip to content

Instantly share code, notes, and snippets.

View mstahl's full-sized avatar
🤪

max thom stahl mstahl

🤪
View GitHub Profile
module Main where
import Control.Parallel
import Control.Parallel.Strategies
import Data.List
import qualified Data.IntSet as Set
divisors :: (Integral t) => t -> [t]
divisors n = nub $ divs ++ inv_divs
where divs = [d | d <- [1..((floor . sqrt . fromIntegral) n)], n `mod` d == 0]
module Main where
import System
import Data.List
import Primes
primes :: [Integer]
primes = filter (prime) [2..]
concatenations :: Show a => [a] -> [Integer]
module Main where
import Data.Map as Map
p :: (Ord a, Num t, Num a) => a -> a -> Map (a, a) t -> Map (a, a) t
p k n m =
if member (k, n) m then
m
else if k > n then
insert (k, n) 0 m
{-
sequence.hs
Generates the funky 1, 11, 21, 1211, ... sequence.
-}
import Data.List
next_in_sequence s = foldl1 (++) $ map (\x -> [length x, head x]) $ group s
-- The smallest number expressible as the sum of a prime square,
-- prime cube, and prime fourth power is 28. In fact, there are
-- exactly four numbers below fifty that can be expressed in
-- such a way:
--
-- 28 = 2^(2) + 2^(3) + 2^(4) 33 = 3^(2) + 2^(3) + 2^(4) 49 =
-- 5^(2) + 2^(3) + 2^(4) 47 = 2^(2) + 3^(3) + 2^(4)
--
-- How many numbers below fifty million can be expressed as the
-- sum of a prime square, prime cube, and prime fourth power?
parFilter :: (a -> Bool) -> [a] -> [a]
parFilter _ [] = []
parFilter f (x:xs) =
let px = f x
pxs = parFilter f xs
in par px $ par pxs $ case px of True -> x : pxs
False -> pxs
-- The hyperexponentiation or tetration of a number a by a positive integer
-- b, denoted by a↑↑b or ^(b)a, is recursively defined by:
--
-- a↑↑1 = a, a↑↑(k+1) = a^((a↑↑k)).
--
-- Thus we have e.g. 3↑↑2 = 3^(3) = 27, hence 3↑↑3 = 3^(27) = 7625597484987
-- and 3↑↑4 is roughly 10^(3.6383346400240996*10^12).
--
-- Find the last 8 digits of 1777↑↑1855.
--
modulus = 14 ^ 8
(.^^.) :: Integer -> Integer -> Integer
a .^^. 1 = a
a .^^. (k + 1) = powMod modulus a (a .^^. k)
main :: IO ()
main = do mapM_ print [2731 .^^. n | n <- [1..15]]
-- Has the following output:
@mstahl
mstahl / befunginatr.rb
Created February 10, 2011 04:54
A Befunge interpreter written in Ruby
#!/usr/bin/ruby
require 'pp'
@core = {}
@hello_world = <<__END_HELLO_WORLD__
> v
v ,,,,,"Hello"<
>48*, v
@mstahl
mstahl / alphabetagaga.rb
Created August 31, 2011 02:58
Alpha Beta Gaga - An alphabetization reverse-engineerer whatchamajiggiddypoo.
#!/usr/bin/env ruby -wKU
#
# alphabetagaga.rb
#
# Takes a text file as input, with terms on lines sorted by some mysterious
# alphabet. Returns a string representing the ordering of the alphabet by
# which the file was sorted.
#
# max thom stahl <max@villainousindustri.es>
# github.com/mstahl