Advertisement
Guest User

46

a guest
Oct 14th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1.  
  2. package Euler;
  3.  
  4. import java.util.ArrayList;
  5. package Euler;
  6.  
  7. import java.util.ArrayList;
  8. public class p046 {
  9.     public static void main(String[] args){
  10.         int compositeList[] = Euler.listOddComposites(10000);
  11.         for(int i:compositeList){
  12.             if(checkGoldbach(i)==false){
  13.                 System.out.println(i+" is the smallest counterexample to Goldbach's conjecture.");
  14.                 break;
  15.             }
  16.         }
  17.    
  18.     }
  19.     public static boolean checkGoldbach(int i){
  20.         int primeList[]=Euler.listPrimes(10000);
  21.         ArrayList<Boolean> goldbach=new ArrayList<Boolean>();
  22.         for(int j=0;primeList[j]<i&!goldbach.contains(true);j++){
  23.             if((Math.sqrt((i-primeList[j])/2))%1==0){
  24.                 System.out.println(i+"="+j+"+2*"+(int)Math.sqrt((i-primeList[j])/2)+"^2");
  25.                 goldbach.add(true);
  26.             }
  27.         }
  28.         if(goldbach.contains(true))
  29.             return true;
  30.         else
  31.             return false;
  32.     }
  33. }
  34.  
  35.  
  36. ODD COMPOSITES SIEVE.
  37.  
  38. public static int[] listOddComposites(int n) {
  39.         boolean[] isprime = listPrimality(n);
  40.         int count = 0;
  41.         for (boolean b : isprime) {
  42.             if (!b)
  43.                 count++;
  44.         }
  45.         int[] result = new int[count];
  46.         for (int i = 4, j = 0; i < isprime.length; i++) {
  47.             if (!isprime[i]&&i%2!=0) {
  48.                 result[j] = i;
  49.                 j++;
  50.             }
  51.         }
  52.         int c=1,i=0;
  53.         for(i=0;c!=0;i++){
  54.             if(result[i]==0){
  55.                 c=0;
  56.             }
  57.         }
  58.         int[] finalListTrim = new int[i-1];
  59.         for(int j=0;j<i-1;j++){
  60.             finalListTrim[j]=result[j];
  61.         }
  62.         return finalListTrim;
  63.     }
  64. public static boolean[] listPrimality(int n) {
  65.         boolean[] result = new boolean[n+1];
  66.         if (n >= 2)
  67.             result[2] = true;
  68.         for (int i = 3; i <= n; i += 2)
  69.             result[i] = true;
  70.         for (int i = 3, end = (int)Math.sqrt(n); i <= end; i += 2) {
  71.             if (result[i]) {
  72.                 for (int j = i * i; j <= n; j += i << 1)
  73.                     result[j] = false;
  74.             }
  75.         }
  76.         return result;
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement