Skip to content

Instantly share code, notes, and snippets.

@WillNess
Last active January 1, 2016 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WillNess/8217144 to your computer and use it in GitHub Desktop.
Save WillNess/8217144 to your computer and use it in GitHub Desktop.
to start from the start, or from squares in bounded trial divisions sieve, does it change the complexity?
-- http://stackoverflow.com/a/1510186/849891
-- does it change the complexity, where to start from ....? -- make an empirical observation:
{-# OPTIONS_GHC -O2 #-}
g n = let {
svs = takeWhile ((< n).(^2).head) $ sv [2..];
ps = map head svs;
qs = map (^2) ps;
a = map length $ map (takeWhile (< n)) svs; -- whole length below `N`
b = map length $ zipWith (takeWhile . (>)) qs svs -- just to the square of head
} in
(sum a, sum b)
h :: Integral a=>(a,a)-> Float
h (a,b) = fromIntegral a/fromIntegral b
sv (x:xs) = (x:xs) : sv [y|y<-xs,rem y x /= 0]
{- Prelude> mapM_ print $ take 6 $ map (take 20) $ sv [2..]
[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
[3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41]
[5,7,11,13,17,19,23,25,29,31,35,37,41,43,47,49,53,55,59,61]
[7,11,13,17,19,23,29,31,37,41,43,47,49,53,59,61,67,71,73,77]
[11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89]
[13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]
-}
main =
mapM_ (\x-> print (x,(h.g) x)) [ -- 10000,20000,40000,60000,80000,100000,120000,140000,
-- 160000,200000,300000,400000, 700000 -- 1000000
2000000]
{-
( 10000,6.0307) -- wasted effort is 1/6th, getting to the upper limit N=10,000
( 20000,5.334525)
( 40000,4.780339)
( 60000,4.9264565) -- emp.ord.grw., optimal:
( 80000,4.769512) -- 10k: X, 1mln: Y, ~ log (Y*(1-1/4.4)/X*(1-1/6)) / log(100) in `N`
( 100000,4.857076) -- ~ log (Y*(1-1/4.4)/X*(1-1/6)) / log(100*2/3) in `n`
( 120000,5.1768265) -- w/wasted effort: log(Y/X) / log(100)
( 140000,4.838724) -- log(Y/X) / log(66.6) (63.9 actually)
( 160000,4.8267465)
( 200000,4.6948023) -- additional complexity _____________
( 300000,4.7028465) -- in N: log( 0.8333333/0.7727272 ) / log(100) ~ 0.016396 |
( 400000,4.5043993) -- in n: log( 0.8333333/0.7727272 ) / log(63.9) ~ 0.018163 |
( 700000,4.4351783) -- ~~~~~~~~~~~~~/
(1000000,4.393456) -- wasted effort is 1/4.4th, getting to the upper limit N=1,000,000
-- a giant space leak...
-}
-- mentioned at http://stackoverflow.com/questions/20921134/
-- is-there-in-the-world-any-strong-optimizing-compiler-for-a-simple-functional-lan
-- #comment31418410_20921134
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment