Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active December 22, 2015 19:59
Show Gist options
  • Select an option

  • Save WillNess/6523256 to your computer and use it in GitHub Desktop.

Select an option

Save WillNess/6523256 to your computer and use it in GitHub Desktop.
primes = ($[3,5..]) $ (id &&& map (\x->[x*x,x*x+2*x..])
>>> second (foldi (\(x:xs)->(x:).union xs) [])
>>> (2:).uncurry minus
Prelude Saga Control.Arrow Control.Applicative> ($[3,5..]) $ (id&&&id) >>> second
(map (\x->[x*x, x*x+2*x..])) >>> second (foldi (\(x:xs)->(x:).union xs) []) >>>
(2:).uncurry minus >>> drop 160000 >>> take 10
[2160563,2160589,2160617,2160619,2160629,2160671,2160677,2160721,2160733,2160749]
(2.79 secs, 800462928 bytes)
Prelude Saga Control.Arrow Control.Applicative> map (logBase 2) $ (zipWith (/) <*> tail)
[2.79, 1.22, 0.54, 0.30, 0.10]
[1.1933839742663013,1.1758498353994176,0.8479969065549502,1.584962500721156]
Prelude Saga Control.Arrow Control.Applicative> map (logBase 4) $ (zipWith (/) <*> tail)
[2.79, 0.54, 0.10]
[1.1846169048328594,1.2164797036380532]
((like APL, which used (\x->[x+x,x+x+x..]) in apl-slides.pdf ))
BUT actually, APL achieves this as -- tail . scanl1 (zipWith (+)) . repeat $ [2..]
import Data.Array.Unboxed
-- array :: (Ix i) => (i, i) -> [(i, e)] -> Array i e
-- listArray :: (Ix i) => (i, i) -> [e] -> Array i e
-- accumArray :: (Ix i) => (e -> a -> e) -> e -> (i, i) -> [(i, a)] -> Array i e
-- assocs :: (Ix i) => Array i e -> [(i, e)]
prs1To n = [i | (i,True) <- assocs $ (accumArray (\a b->False) True (2,n) [(i,()) |
xs <- takeWhile (not.null) . map (takeWhile (<= n)) .
tail . scanl1 (zipWith (+)) . repeat $ [2..n] ,
i <- xs ] :: UArray Int Bool)]
-- ...n/2], ...n/3], ...n/4], ...n/5], ...
Prelude> mapM_ print . take 10 . map (take 10) $ tail . scanl1 (zipWith (+)) . repeat $ [2..]
[ 4, 6, 8,10,12,14,16,18, 20, 22] -- 2x
[ 6, 9,12,15,18,21,24,27, 30, 33] -- 3x
[ 8,12,16,20,24,28,32,36, 40, 44] -- 4x
[10,15,20,25,30,35,40,45, 50, 55] -- 5x
[12,18,24,30,36,42,48,54, 60, 66] -- 6x
[14,21,28,35,42,49,56,63, 70, 77] -- 7x
[16,24,32,40,48,56,64,72, 80, 88] -- 8x
[18,27,36,45,54,63,72,81, 90, 99] -- 9x
[20,30,40,50,60,70,80,90,100,110] -- 10x
[22,33,44,55,66,77,88,99,110,121] -- 11x
Prelude> mapM_ print . take 10 . map (take 10) $ tail . scanl1 (zipWith (+)) . repeat $ [3,5..]
[ 6,10,14,18, 22, 26, 30, 34, 38, 42]
[ 9,15,21,27, 33, 39, 45, 51, 57, 63]
[12,20,28,36, 44, 52, 60, 68, 76, 84]
[15,25,35,45, 55, 65, 75, 85, 95,105]
[18,30,42,54, 66, 78, 90,102,114,126]
[21,35,49,63, 77, 91,105,119,133,147]
[24,40,56,72, 88,104,120,136,152,168]
[27,45,63,81, 99,117,135,153,171,189]
[30,50,70,90,110,130,150,170,190,210]
[33,55,77,99,121,143,165,187,209,231]
Prelude> mapM_ print . take 10 . map (take 10) $ tail . iterate (zipWith (+) [6,10..]) $ [3,5..]
[ 9, 15, 21, 27, 33, 39, 45, 51, 57, 63]
[15, 25, 35, 45, 55, 65, 75, 85, 95,105]
[21, 35, 49, 63, 77, 91,105,119,133,147]
[27, 45, 63, 81, 99,117,135,153,171,189]
[33, 55, 77, 99,121,143,165,187,209,231]
[39, 65, 91,117,143,169,195,221,247,273]
[45, 75,105,135,165,195,225,255,285,315]
[51, 85,119,153,187,221,255,289,323,357]
[57, 95,133,171,209,247,285,323,361,399]
[63,105,147,189,231,273,315,357,399,441]
Prelude> mapM_ print . take 10 . zipWith take [10,9..] $ -- 2:[3,5..]\\smash_$
map fst . tail . iterate (\((_:a),(_:b))-> (zipWith (+) a b, b))
$ ([1,3..], [2,6..])
[ 9, 15, 21, 27, 33, 39, 45, 51, 57, 63] 69 75 81 87 93 99 105 111 117 123
[ 25, 35, 45, 55, 65, 75, 85, 95,105] 115 125 135 145 155 165 175
[ 49, 63, 77, 91,105,119,133,147] 161 175
[ 81, 99,117,135,153,171,189]
[121,143,165,187,209,231]
[169,195,221,247,273]
[225,255,285,315]
[289,323,357]
[361,399]
[441]
Prelude> mapM_ print . take 10 . map (take 10) $
map fst . tail . iterate (\((_:a),(_:b))-> (zipWith (+) a b, b))
$ ([1,3..], [2,6..])
[ 9, 15, 21, 27, 33, 39, 45, 51, 57, 63]
[ 25, 35, 45, 55, 65, 75, 85, 95,105,115]
[ 49, 63, 77, 91,105,119,133,147,161,175]
[ 81, 99,117,135,153,171,189,207,225,243]
[121,143,165,187,209,231,253,275,297,319]
[169,195,221,247,273,299,325,351,377,403]
[225,255,285,315,345,375,405,435,465,495]
[289,323,357,391,425,459,493,527,561,595]
[361,399,437,475,513,551,589,627,665,703]
[441,483,525,567,609,651,693,735,777,819]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment