tag:blogger.com,1999:blog-5359546512544809971.post4062460999361495253..comments2024-03-22T10:42:37.237-07:00Comments on Jeremy Bytes: Sequences vs. Lists in F# (with Euler Problems #7, #8, #9, and #10)Jeremyhttp://www.blogger.com/profile/06749690234470413216noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-5359546512544809971.post-3914083759759800152016-07-31T00:00:13.676-07:002016-07-31T00:00:13.676-07:00You've got the list vs seq down pat here.
Th...You've got the list vs seq down pat here. <br />There are a few things I'd do slightly differently -- in the isPrime function, the shorter sequence<br /><br />Seq.initInfinite (i -> i+2) |> Seq.takeWhile (i -> i*i <= x) <br /><br />instead of <br /><br />for i = 2 to x/2 do<br /><br />the multiplication being safe from overflow since x is a representable number in Int32.<br /><br />When the problem gives you the small primes, it would also be OK to pre-sieve by having 2 and 3 known and then only looking at values of the form (6n ± 1).<br /><br />Prime generation is also a good candidate for solutions involving memoization, with isPrime only starting the exhaustive sequence after running out of known prime values.Steve Gilhamhttps://www.blogger.com/profile/03622573187942388226noreply@blogger.com