# Using Two Threads For Prime Numbers

Page 1 of 1

## 3 Replies - 14134 Views - Last Post: 15 February 2009 - 11:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86803&amp;s=5725daba93b1c5e144e9f562281155ae&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 vodkanyone

Reputation: 1
• Posts: 14
• Joined: 27-November 08

# Using Two Threads For Prime Numbers

Posted 15 February 2009 - 08:13 AM

Need two threads- thread one computes number of primes from 1 - 2*n/3 and thread two should compute from 2*n/3 - n. Then will add both thread counts and print it out.

When n is 20 the output is

Number of primes is: 5
Number of primes is: 3

This is obviously wrong as output should be

Number of primes is: 8

```package JavaThread;

/**
* Summary description for Program
*/
{
{
}

public void run()
{
int n = 20;
int c = 2 * n / 3;
int a = 0;
int b = 0;

for (int i = 0; i < n; i++)
{

if (i < c)
{
if ((Primes.isPrime(i))) { a++;  }
}
}

if (i >= c )
{
if ((Primes.isPrime(i))) { b++; }
}}
}

System.out.println("Thread " + threadie + " contains " + a + " prime numbers ");
System.out.println("Thread " + threadie + " contains " + b + " prime numbers ");
int NumPrimes = a + b;
System.out.println(" Number of primes is: " + NumPrimes);
}

static boolean isPrime(long n)
{
if (n <= 1) return false;
double limit = Math.sqrt(n);
for (long i = 2; i <= limit; i++)
{
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] arg)
{

th1.start();
th2.start();
try { th1.join(); }
catch (InterruptedException ie) { }
try { th2.join(); }
catch (InterruptedException ie) { }

System.out.println("\nThreads 1 and 2 have finished");
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Using Two Threads For Prime Numbers

### #2 horace

• D.I.C Lover

Reputation: 535
• Posts: 2,825
• Joined: 25-October 06

## Re: Using Two Threads For Prime Numbers

Posted 15 February 2009 - 09:31 AM

a, b and NumPrimes are local variables inside the threads so they don't know about each other.
As a quick fix to get it working you could make a and b static and calculate NumPrimes in main()
```/**
* Summary description for Program
*/
{
static	 int a = 0;
static	 int b = 0;

{
}

public void run()
{
int n = 20;
int c = 2 * n / 3;
//	int a = 0;
//	 int b = 0;

for (int i = 0; i < n; i++)
{

if (i < c)
{
if ((Primes.isPrime(i))) { a++;  }
}
}

if (i >= c )
{
if ((Primes.isPrime(i))) { b++; }
}}
}

if(threadie == 1)  System.out.println("Thread " + threadie + " contains " + a + " prime numbers ");
if(threadie == 2)  System.out.println("Thread " + threadie + " contains " + b + " prime numbers ");
//	 int NumPrimes = a + b;
//	  System.out.println(" Number of primes is: " + NumPrimes);
}

static boolean isPrime(long n)
{
if (n <= 1) return false;
double limit = Math.sqrt(n);
for (long i = 2; i <= limit; i++)
{
if (n % i == 0) return false;
}
return true;
}

public static void main(String[] arg)
{

th1.start();
th2.start();
try { th1.join(); }
catch (InterruptedException ie) { }
try { th2.join(); }
catch (InterruptedException ie) { }

System.out.println("\nThreads 1 and 2 have finished");
int NumPrimes = a + b;
System.out.println(" Number of primes is: " + NumPrimes);
}
}

```

I am sure there is a more elegant way of doing this though

a run give
```Thread 1 contains 5 prime numbers
Thread 2 contains 3 prime numbers

Threads 1 and 2 have finished
Number of primes is: 8

```

This post has been edited by horace: 15 February 2009 - 09:33 AM

### #3 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: Using Two Threads For Prime Numbers

Posted 15 February 2009 - 11:28 AM

and what was wrong with your first topic?

### #4 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: Using Two Threads For Prime Numbers

Posted 15 February 2009 - 11:55 AM

this will slightly improve the performance of your isPrime() method
```static boolean isPrime(long n)
{
if(n == 2)return true;
if (n < 2 || n % 2 == 0) return false;
for (long i = 3; i*i <= n; i += 2)
{
if (n % i == 0) return false;
}
return true;
}

```