1   /*
2    * PrimeGroups - Trying to find groups of prime numbers
3    * Copyright (C) 2009 Christian Schenk
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    * 
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  package org.christianschenk.primegroups;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * Tries to find groups of prime numbers with certain distances, e.g.
26   * <ul>
27   * <li>pairs of twin primes</li>
28   * <li>pairs of sexy primes</li>
29   * <li>triplets of sexy primes</li>
30   * </ul>
31   * 
32   * @author Christian Schenk
33   */
34  public class PrimeGroups {
35  
36  	/**
37  	 * Holds the prime numbers.
38  	 */
39  	private final PrimeFinder primeFinder;
40  
41  	public PrimeGroups(final int start, final int end) {
42  		this.primeFinder = new PrimeFinder();
43  		this.primeFinder.findPrimes(start, end);
44  	}
45  
46  	/**
47  	 * Returns <code>amount</code> numbers with <code>distance</code> apart from each other.
48  	 */
49  	public List<Integer> getGroup(final int amount, final int distance) {
50  		if (amount < 2 || distance < 1) throw new RuntimeException("Invalid parameters.");
51  
52  		final List<Integer> result = new ArrayList<Integer>();
53  		final List<Integer> primes = this.primeFinder.getPrimes();
54  
55  		// find groups with the given restrictions, i.e. distance and amount
56  		for (int i = 0, n = primes.size(); i < n; i++) {
57  			final int curPrime = primes.get(i);
58  			boolean found = true;
59  			for (int j = 1; j < amount; j++) {
60  				if (primes.contains(curPrime + j * distance) == false) {
61  					found = false;
62  					break;
63  				}
64  			}
65  
66  			if (found) {
67  				result.add(curPrime);
68  			}
69  		}
70  
71  		return result;
72  	}
73  
74  	/**
75  	 * To save memory only the first element of a group is stored in the {@link #getGroup(int, int)}
76  	 * method. We'll print the following prime numbers - i.e. the whole group - using this method.
77  	 */
78  	public void printGroups(final List<Integer> groups, final int amount, final int distance) {
79  		for (final int prime : groups) {
80  			for (int i = 0; i < amount; i++) {
81  				System.out.print((prime + i * distance) + " ");
82  			}
83  			System.out.println("");
84  		}
85  	}
86  
87  	public static void main(final String[] args) {
88  		final int start = 2;
89  		final int end = 100;
90  		final int amount = 3;
91  		final int distance = 6;
92  
93  		final PrimeGroups pg = new PrimeGroups(start, end);
94  		pg.printGroups(pg.getGroup(amount, distance), amount, distance);
95  	}
96  }