Jump to content

Stern-Brocot sequence: Difference between revisions

m
→‎{{header|Haskell}}: Tidied iterate version.
m (→‎{{header|REXX}}: added/changed whitespace and comments, added checks to possibly bypass computing some of the tasks.)
m (→‎{{header|Haskell}}: Tidied iterate version.)
Line 2,436:
Or, expressed in terms of iterate:
 
<lang haskell>import Data.ListFunction (nubBy, sortByon)
import Data.MonoidList ((<>)nubBy, sortOn)
import Data.Ord (comparing)
 
import Data.Monoid ((<>))
------------------ STERN-BROCOT SEQUENCE -----------------
import Data.Function (on)
 
sternBrocot :: [Int]
sternBrocot = head <$> iterate go [1, 1]
where
let go (a:b:xs) = (b : xs) <> [a + b, b]
in head go (a : b : xs) = (b : xs) <$> iterate[a go+ [1b, 1b]
 
-- TEST ------------------------------------ TEST -------------------------
main :: IO ()
main = do
Line 2,452 ⟶ 2,453:
print $
take 10 $
nubBy (on (==) fst) $
sortOn fst $
sortBy (comparing fst) $ takeWhile ((110 >=) . fst) $ zip sternBrocot [1 ..]
print $ take 1 $ dropWhile takeWhile ((100110 />=) . fst) $ zip sternBrocot [1 ..]
sortBy (comparing fst) $ takeWhile ((110 >=) . fst) $ zip sternBrocot [1 ..]
print $ (all ((1 ==) . uncurry gcd) . (zip <*> tail)) $ take 1000 sternBrocot</lang>
print $
take 1 $
dropWhile ((100 /=) . fst) $
zip sternBrocot [1 ..]
print $
print $ (all ((1 ==) . uncurry gcd) . (zip <*> tail)) $ take 1000 sternBrocot</lang>
take 1000 sternBrocot</lang>
{{Out}}
<pre>[1,1,2,1,3,2,3,1,4,3,5,2,5,3,4]
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.