Hmmm,> Does the solutions in Attila’s link not suit your needs?i somehow missed Attila’s link. The code there is really great.Regards,KimOn 22 Jan., 16:14, Xi Chen <heydic…> wrote:> Does the solutions in Attila’s link not suit your needs?>> q)p:{[s;n]$[n<4;enlist 2;r,1_where s[n]r:.z.s[s]ceiling sqrt n]}> q)s3:{@[x#010001b;y*til each ceiling x%y:2_y;:;0b]}> q)last p[s3] 2000000> 1999993> q)sum long$p[s3] 2000000> 142913828984j>> Regards,> Xi>> On 22 January 2011 14:45, kuentang <kuent...> wrote:>>>> > Hmmm,>> >> q) sum p> >> 1179908154>> > is not the correct answer. Because of overflow the primes need to be> > casted to long.>> > sum {[x] r::
long$(); { f:first x; r,:long$f; ?:(1_x) where not> > 0=(1_x) mod f}/[{0< count x};x]; :r} 2_til 2000000>> > Can you provide a more elegant solution ?>> > On 22 Jan., 15:25, _Oz_ <zakharovo...> wrote:> >> Hi, kuentang!>> >> You are absolutely right, it wasn't the sieve.> >> All that could be squeezed out of this easy line for the task you've> >> mentioned will be pretty slow - around 16 min.>> >> q) \t p:{$[min y mod x@where x<=sqrt y;x,y;x]}/[(),2;3+2*til 1000000]> >> 952167>> >> q) last p> >> 1999993>> >> q) sum p> >> 1179908154>> >> Sieve should be much faster:> >> 1. it'll reduce the scoop of candidates with each found prime < upper> >> bound/2 (actually all that will be left after that in the scoop will> >> be primes)> >> 2. it will use mostly multiplication, not division.> >> (With the sieve idea is not to test candidates, but instead reduce> >> scoop by eliminating chunks from it with each prime.)> >> Proper implementation could be found at the link that Attila provided.>> >> But even the most naive implementation of the sieve there is (loopy,> >> ineffective & not in functional form at all) will return the same> >> result twice as fast:> >> .a.s:3+til 2000000;> >> .a.r:(),2;> >> f:{.a.s:.a.s except (last .a.r)*2+til
int$((last .a.s)> >> %last .a.r)-1;.a.r,:1#.a.s;.a.s:1_.a.s}>> >> q) \t while[(last .a.r)<(last .a.s)%2;f]> >> 472056>> >> q) p~.a.r,.a.s> >> 1b>> >> Cheers,> >> Oz>> >> On Jan 22, 1:51?pm, kuentang <kuent…> wrote:>> >> > Hi Oz,>> >> > i need to correct myself. The solution you publish is not the sieve of> >> > eratosthenes, even it works.>> >> > Consider the following problem:>> >> > Calculate the sum of all the primes below two million.>> >> > The solution you purpose is not fast enough.>> >> > On 14 Jan., 14:11, kuentang <kuent…> wrote:>> >> > > Hi Oz,>> >> > > thx for your solution.>> >> > > Your solution is much faster, because you use the sieve of> >> > > eratosthenes.> >> > > The implementation itself is also very straightforward.>> >> > > Regards,> >> > > Kim>> >> > > On 14 Jan., 11:27, Oz <zakharovo…> wrote:>> >> > > > Hi, Kim!>> >> > > > Something along those lines maybe?>> >> > > > ({$[min y mod x;x,y;x]}/[2;3+til 110000])[10000]>> >> > > > 104743 is the 10001st prime (given that 1st prime is 2)>> >> > > > (that one was right off the bat - actually it tries all of 110000> >> > > > numbers,> >> > > > you will have to do something more intelligent to stop as soon as> >> > > > found desired prime (count x))>> >> > > > Cheers,> >> > > > Oz>> >> > > > On Jan 14, 12:38?pm, kuentang <kuent…> wrote:>> >> > > > > Hi together,>> >> > > > > i would like to calculate the 10001 st prime number.>> >> > > > > This is the code that i used, but i need to wait like hell. Any> >> > > > > improvement, suggestions, or ways to do it more elegantly?>> >> > > > > { l:1+first x; p:not 0=min l mod 2_til l ; :$[p;(l;(l,last x));> >> > > > > (l;last x)] ? }/[{1000 >count last x};(2;enlist 2)]>> >> > > > > Regards,> >> > > > > Kim>> > –> >
Submitted via Google Groups</kuent…></zakharovo…></kuent…></kuent…></zakharovo…></kuent…></heydic…>