Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Euler;
- import java.util.ArrayList;
- package Euler;
- import java.util.ArrayList;
- public class p046 {
- public static void main(String[] args){
- int compositeList[] = Euler.listOddComposites(10000);
- for(int i:compositeList){
- if(checkGoldbach(i)==false){
- System.out.println(i+" is the smallest counterexample to Goldbach's conjecture.");
- break;
- }
- }
- }
- public static boolean checkGoldbach(int i){
- int primeList[]=Euler.listPrimes(10000);
- ArrayList<Boolean> goldbach=new ArrayList<Boolean>();
- for(int j=0;primeList[j]<i&!goldbach.contains(true);j++){
- if((Math.sqrt((i-primeList[j])/2))%1==0){
- System.out.println(i+"="+primeList[j]"+2*"+(int)Math.sqrt((i-primeList[j])/2)+"^2");
- goldbach.add(true);
- }
- }
- if(goldbach.contains(true))
- return true;
- else
- return false;
- }
- }
- ODD COMPOSITES SIEVE.
- public static int[] listOddComposites(int n) {
- boolean[] isprime = listPrimality(n);
- int count = 0;
- for (boolean b : isprime) {
- if (!b)
- count++;
- }
- int[] result = new int[count];
- for (int i = 4, j = 0; i < isprime.length; i++) {
- if (!isprime[i]&&i%2!=0) {
- result[j] = i;
- j++;
- }
- }
- int c=1,i=0;
- for(i=0;c!=0;i++){
- if(result[i]==0){
- c=0;
- }
- }
- int[] finalListTrim = new int[i-1];
- for(int j=0;j<i-1;j++){
- finalListTrim[j]=result[j];
- }
- return finalListTrim;
- }
- public static boolean[] listPrimality(int n) {
- boolean[] result = new boolean[n+1];
- if (n >= 2)
- result[2] = true;
- for (int i = 3; i <= n; i += 2)
- result[i] = true;
- for (int i = 3, end = (int)Math.sqrt(n); i <= end; i += 2) {
- if (result[i]) {
- for (int j = i * i; j <= n; j += i << 1)
- result[j] = false;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement