Calculating the sum of a *lot* of primes

Jerry Jensen jhj at jhj.com
Thu Nov 12 00:54:01 EST 2015


Interesting challenge, and puzzling crashiness. As a first thought, making the bAdd function private might have some speed advantage - but since the message path isn’t very complicated in this example, it might not be a lot. I’m not sure how many primes are involved, but I think its lots, as you say.

I hope to spend some coherent time tomorrow looking into the crashiness (and the spiffy algorithm!).
.Jerry

> On Nov 10, 2015, at 11:45 PM, Geoff Canyon <gcanyon at gmail.com> 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





More information about the use-livecode mailing list