Making a program parallel in Java - HELP

Hello @all,

I’ve only learnt a bit about parallelization of programs, but, I’m now facing what seems to be an embarassingly parallel situation.

I simply need to compute and list ALL twin primes below N specifically in Java.

I think I’ve done some decent algorithmic optimizations to tackle this.

But, this will be solely a speed competition, and my code runs in 2m and 46s for N=100.000.000

I was wondering how I can exploit Java’s concurrency mechanism to speed up my code?

import java.lang.*;
import java.util.*;

public class Main
	public static int isprime(long x)
 		long max= (long) Math.sqrt(x);
		long i;
		long dx=4;
 		for(i=5; i<=max; dx=-(dx-6),i+=dx)
		if( (x%i) ==0)
			return 0;
 		return 1;

	public static void main(String[] args)
		 long i,count=1;
 		  Scanner in = new Scanner(;
		  int n = in.nextInt();

	 	System.out.printf("%d ", 3);
		if(isprime(i)==1 && isprime(i+2)==1)
			System.out.printf("%d %d " ,i,i+2);
	 //printf("\b \n\nTotal Primes within range: %d\n",count);

If anyone could either help doing the paralellization thing or even improving the algorithm, I’d be really glad :slight_smile: