# [erlang-questions] Distributing things

Joe Armstrong <>
Sun Nov 8 18:32:00 CET 2009

```On Sat, Nov 7, 2009 at 9:57 PM, Calum <> wrote:
> On Sat, Nov 7, 2009 at 8:13 PM, Joe Armstrong <> wrote:
>> Hang on - the algorithm just generates a random integer and tests if
>> it is a prime,
>> if it's not a prime it tries again. It has to generate many candidates
>> before it finds a
>> prime. Why not start N parallel processes each of which generates and
>> tests candidates
>> then when either one of them succeeds you can stop. Sounds easier to me.
>>
>> /Joe
>
> Heya Joe (crikey I feel honoured!)

>
> I thought it picked a random number, and then tested N-1 over and over
> to see if it was prime.
> I was trying to think how to coordinate the N value among all the
> processes so that they wouldn't try the same ones.
> I.e.:

But the probability of that is 10^-N  (N= number of digits) so for 80
digit primes 10^-80

> Main program selects 1234567 as a random number, and knows it has 3
> Erlang nodes it can work on
> Node one tries 1234566
> Node two tries 1234565
> Node three tries 1234564
> Then, when node one has found out that 1234566 isn't a prime
> (unsurprisingly!), it would have to know where the other nodes were at
> so it could know to check 1234563.

I'd just tell the N nodes "go generate random numbers and test if they
are primes then tell me if you've got a prime" the chance they do the
same work is very small and if they do
who cares. The simplicity of the algorithm overweighs any potential
inefficiency.
Once you've got a prime tell all the nodes to stop calculating.

>
> Although I suppose it doesn't really matter (for this program) if
> they're all checking 123456* - but for some applications, they'd need
> to be in sync.
>
> The other thing I was wondering was what would happen if I fired up a
> new node in the middle of this running - how would it join in? Are
> there tried-and-tested software patterns to handle this sort of