# Calculating the sum of a *lot* of primes

Peter M. Brigham pmbrig at gmail.com
Wed Nov 11 07:34:29 EST 2015

```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?
>
> -- 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
>
> 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 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
>   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

```