Calculating the sum of a *lot* of primes

Geoff Canyon gcanyon at gmail.com
Wed Nov 11 02:45:20 EST 2015


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



More information about the use-livecode mailing list