Skip to content

Instantly share code, notes, and snippets.

@ihercowitz
Created February 13, 2012 17:55
Show Gist options
  • Save ihercowitz/1818672 to your computer and use it in GitHub Desktop.
Save ihercowitz/1818672 to your computer and use it in GitHub Desktop.
package com.testes;
/* Demonstrates the Goldbach's conjecture - Every even integer greater than 2 can be expressed as the sum of two primes
*
* author: Luiz Vessosa
*/
import java.util.ArrayList;
import java.util.List;
public class Euler {
private static final int NUM_TO_TEST = 134;
public static void main(String[] args) {
List<Integer> primes = primeGen(NUM_TO_TEST);
System.out.println("Current Number: " + NUM_TO_TEST);
System.out.print(NUM_TO_TEST + " = ");
boolean foundSomething = false;
for (int i = 0; i < primes.size(); i++) {
for (int j = i; j < primes.size(); j++) {
if ((primes.get(i) + primes.get(j)) == NUM_TO_TEST) {
if (foundSomething)
System.out.print(" or ");
System.out.print(primes.get(i) + " + " + primes.get(j));
foundSomething = true;
}
}
}
}
private static List<Integer> primeGen(int max) {
int x, y = 0;
List<Integer> result = new ArrayList<Integer>();
for (x = 2; x < max; x++) {
if (x % 2 != 0 || x == 2) {
for (y = 2; y <= x / 2; y++) {
if (x % y == 0) {
break;
}
}
if (y > x / 2) {
result.add(x);
//System.out.println(x);
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment