![]() |
Code Examples
A repository of 155 code examples for BeepBeep
|
Compute twin primes by distributing the computation across two machines over a network. More...
Classes | |
class | BigIntegerFunctions |
Contains a few utility functions for manipulating Java's BigInteger objects. More... | |
class | TwinPrimesA |
The code for Machine A in the twin prime example. More... | |
class | TwinPrimesB |
The code for Machine B in the twin prime example. More... | |
Compute twin primes by distributing the computation across two machines over a network.
Twin primes are pairs of numbers p and p+2 such that both are prime. For example, (3,5), (11,13) and (17,19) are three such pairs. The twin prime conjecture asserts that there exists an infinity of such pairs.
In our setup, Machine A will be programmed to check if each odd number 3, 5, 7, etc. is prime. If so, it will send the number n to Machine B, which will then check if n+2 is prime. If this is the case, Machine B will print to the console the values of n and n+2.
The interest of this setup is that checking if a number is prime is an operation that becomes very long for large integers (especially with the algorithm we use here). By having the verification for n and n+2 on two separate machines, the whole processor chain can actually run two primality checks at the same time.
Note that this chain of processors is only meant to illustrate a possible use of the HTTP gateways. As such, it is not a very efficient way of finding twin primes: when n and n+2 are both prime, three primality checks will be done: Machine A will first discover that n is prime, which will trigger Machine B to check if n+2 also is. However, since Machine A checks all odd numbers, it will also check for n+2 in its next computation step. Could you think of a better way of using processors to make this more efficient?
A few things you might want to try: