Created
February 13, 2012 17:55
-
-
Save ihercowitz/1818672 to your computer and use it in GitHub Desktop.
Goldbach's conjecture - http://en.wikipedia.org/wiki/Goldbach%27s_conjecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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