Project Euler – Problem 49

Problem 49:
The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Vector;

class runner
	public static boolean isPrime(int n){
		if(primes.contains(n)) return true;
		double limit = Math.sqrt(n);
		for(int p: primes){
			if(p > limit) break;
			if(n % p == 0) return false;
		return true;

	//Concept from Steinhaus–Johnson–Trotter
	public static void permute(String obj, Vector arr){
		if(arr.size() == 0){
		Vector result = new Vector();

		for(String str: arr){
			for(int i=0;i primes = new Vector();

	public static void main (String[] args) throws java.lang.Exception
		long time = System.currentTimeMillis();

		for(int i=3; i<=9999; i++){
			isPrime(i);//populate prime array

		for(int n : primes){
			String cur = ""+n;
			if(cur.length() != 4) continue;
			Vector permutations = new Vector();
			for(int i=0;i permuteAndPrimeSet = new LinkedHashSet();
			for(String str : permutations){
				int i = Integer.valueOf(str);
				if(i/1000 == 0 || i < n) continue;
				if(primes.contains(i)) permuteAndPrimeSet.add(i);
			if(permuteAndPrimeSet.size() < 3) continue;
			//[1013, 1031, 1103, 1301, 3011]
			Object[] permuteAndPrime = permuteAndPrimeSet.toArray();
			for(int j=1; j