User talk:Gaaijz: Difference between revisions
(→Jers) |
No edit summary |
||
Line 10: | Line 10: | ||
[[User:DanBron|DanBron]] 13:48, 2 September 2008 (UTC) |
[[User:DanBron|DanBron]] 13:48, 2 September 2008 (UTC) |
||
:: Hi Dan. I noticed your and TBH's activity on this wiki. Now you mention it, in time I'll think about a rabbit sequence task. Right now I'm working on Perfect Numbers (so far I have a generator based on the Lucas-lehmer test: not the tester that's asked for) and polynomial fitting (but that's in Haskell). --[[User:Gaaijz|Gaaijz]] 17:18, 2 September 2008 (UTC) |
:: Hi Dan. I noticed your and TBH's activity on this wiki. Now you mention it, in time I'll think about a rabbit sequence task. Right now I'm working on Perfect Numbers (so far I have a generator based on the Lucas-lehmer test: not the tester that's asked for) and polynomial fitting (but that's in Haskell). --[[User:Gaaijz|Gaaijz]] 17:18, 2 September 2008 (UTC) |
||
== Zigzag == |
|||
Two different approaches with Haskell for the zigzag task. |
|||
flist = map (:[]) |
|||
elist = flip replicate [] |
|||
revNrev True = cycle [reverse,id] |
|||
revNrev _ = cycle [id,reverse] |
|||
transpN True = id |
|||
transpN _ = transpose |
|||
zigzag m n = (transpN rev).map concat. transpose |
|||
. uncurry ((.(map (liftM2 (++) (elist.(dl-).length) flist))).(++).(map (liftM2 (++) flist (elist.(dl-).length)))) |
|||
$ splitAt k revcs |
|||
where |
|||
k = truncate . sqrt . fromIntegral $ (m*n) |
|||
dl = min m n |
|||
nd = abs (m-n) |
|||
rev = m<n |
|||
pseudoAntiDiags = unfoldr (\((c:cs),xs) -> if null xs then Nothing else Just (take c xs,(cs,drop c xs))) |
|||
([1..dl]++(replicate nd dl)++[dl-1,dl-2..0],[0..m*n-1]) |
|||
revcs = zipWith id (revNrev rev) pseudoAntiDiags |
|||
-- slower, almost complete emulation of the J-solution |
|||
groupon f x y= f x == f y |
|||
tab n = fst . until (null.snd) (\(xs,ys)-> (xs++[take n ys], drop n ys)) . (,) [] |
|||
grade xs = map snd. sort $ zip xs [0..] |
|||
zigzagJ m n = tab n. grade .concat $ zipWith id (cycle [reverse,id]) fdiag |
|||
where fdiag = map (map snd). groupBy (groupon fst).sortBy (comparing fst) |
|||
$ zip (map sum $ sequence [[0..m-1],[0..n-1]] ) [0..] |
|||
*Main> sum.map sum $ zigzag 500 500 |
|||
1185103928 |
|||
(0.69 secs, 103908376 bytes) |
|||
*Main> sum.map sum $ zigzagJ 500 500 |
|||
31249875000 |
|||
(4.55 secs, 575802084 bytes) |
Revision as of 07:43, 6 September 2008
welcome!
Welcome to Rosetta Code! If you have any questions, drop them in the appropriate talk page, and someone will get back to you. If you have a more general question, try Mwn3d's or my talk pages. --Short Circuit 04:10, 2 September 2008 (UTC)
Jers
Hey Arie. This is Dan Bron from the J forums. Tracy Harms is here too. It's good to have J representation on RosettaCode. I'm glad you posted a J solution to the Pyramid of Numbers, that was near the top of my to do list. Have you considered posting a task along the lines of your Rabbit Sequence?
Oh, by the way, I linkified your user page. I hope you don't mind. Go ahead and revert it if you like it better the other way.
DanBron 13:48, 2 September 2008 (UTC)
- Hi Dan. I noticed your and TBH's activity on this wiki. Now you mention it, in time I'll think about a rabbit sequence task. Right now I'm working on Perfect Numbers (so far I have a generator based on the Lucas-lehmer test: not the tester that's asked for) and polynomial fitting (but that's in Haskell). --Gaaijz 17:18, 2 September 2008 (UTC)
Zigzag
Two different approaches with Haskell for the zigzag task.
flist = map (:[]) elist = flip replicate [] revNrev True = cycle [reverse,id] revNrev _ = cycle [id,reverse] transpN True = id transpN _ = transpose zigzag m n = (transpN rev).map concat. transpose . uncurry ((.(map (liftM2 (++) (elist.(dl-).length) flist))).(++).(map (liftM2 (++) flist (elist.(dl-).length)))) $ splitAt k revcs where k = truncate . sqrt . fromIntegral $ (m*n) dl = min m n nd = abs (m-n) rev = m<n pseudoAntiDiags = unfoldr (\((c:cs),xs) -> if null xs then Nothing else Just (take c xs,(cs,drop c xs))) ([1..dl]++(replicate nd dl)++[dl-1,dl-2..0],[0..m*n-1]) revcs = zipWith id (revNrev rev) pseudoAntiDiags -- slower, almost complete emulation of the J-solution groupon f x y= f x == f y tab n = fst . until (null.snd) (\(xs,ys)-> (xs++[take n ys], drop n ys)) . (,) [] grade xs = map snd. sort $ zip xs [0..] zigzagJ m n = tab n. grade .concat $ zipWith id (cycle [reverse,id]) fdiag where fdiag = map (map snd). groupBy (groupon fst).sortBy (comparing fst) $ zip (map sum $ sequence [[0..m-1],[0..n-1]] ) [0..] *Main> sum.map sum $ zigzag 500 500 1185103928 (0.69 secs, 103908376 bytes) *Main> sum.map sum $ zigzagJ 500 500 31249875000 (4.55 secs, 575802084 bytes)