Calculating the sum of a *lot* of primes
Geoff Canyon
gcanyon at gmail.com
Wed Nov 11 09:29:49 EST 2015
Ha, caught me being lazy. For loops like
repeat with i = 1 to fld 1
there is (almost) no difference -- the field gets referenced one time at
the start and that's it.
But since I was lazy about how a loop with "step 2" in it would exit, I was
slower.
Thanks!
gc
On Wed, Nov 11, 2015 at 7:34 AM, Peter M. Brigham <pmbrig at gmail.com> wrote:
> There may be other factors more important for speed, but one thing is to
> avoid having to access the contents of a field 1.5 billion times, by
> putting fld 1 into a variable, say "maxNbr", and then doing
>
> repeat until i > maxNbr…
>
> -- Peter
>
> Peter M. Brigham
> pmbrig at gmail.com
> http://home.comcast.net/~pmbrig
>
>
> On Nov 11, 2015, at 2:45 AM, Geoff Canyon wrote:
>
> > I'm playing with a challenge to calculate the sum of the primes less than
> > 3,000,000,000
> >
> > So far I have this, which gets the job done for the primes up to about a
> > billion, but then dies (the whole app) without even a dialog. It's also
> not
> > very fast. It would probably take an hour or two to finish.
> >
> > Any suggestions for efficiency or non-crashed-ness?
> >
> > function bAdd x,y
> > -- adds two positive numbers of arbitrary length
> > put 0 into c
> > repeat with i = 14 to max(length(x),length(y))
> > add char -i to 13 - i of x + char -i to 13 - i of y to c
> > put char -14 to -1 of c before r
> > delete char -14 to -1 of c
> > end repeat
> > return c & r
> > end bAdd
> >
> > on mouseUp
> > put 2 into T
> > put 0 into S
> > put 3 into i
> > repeat until i > fld 1 -- fld 1 contains 3000000000
> > if P[i] is empty then
> > put bAdd(i,T) into T
> > put i & comma after P[i*3]
> > else
> > repeat for each item x in P[i]
> > put x & comma after P[i + x + x]
> > end repeat
> > delete P[i]
> > end if
> > if the seconds > S then
> > put the seconds into S
> > put i && T into fld 2
> > unlock screen
> > lock screen
> > end if
> > add 2 to i
> > end repeat
> > put i && T into fld 2
> > end mouseUp
> > _______________________________________________
> > use-livecode mailing list
> > use-livecode at lists.runrev.com
> > Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
More information about the use-livecode
mailing list