Advertisement
pseudocreator

XO hashCode, more general

Nov 19th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1.  public static Random rand = new Random();
  2.  private final int[] primes = calcPrimes(DIM, DIM);
  3.  
  4.  @Override
  5.  public int hashCode(){
  6.    
  7.    int result = 1;
  8.    
  9.    for(int i = 0; i < DIM; i++){
  10.      for(int j = 0; j < DIM; j++){
  11.        result += (tabla[i][j] + 3) * primes[i + j];
  12.      }
  13.    }
  14.    
  15.    return result;
  16.  }
  17.  
  18.  private int[] calcPrimes(int d1, int d2){
  19.    final int d = d1 * d2;
  20.    int[] result = new int[d];
  21.    
  22.    for(int i = 0; i < d; i++){
  23.      //Increasing the value(to some number)
  24.      //will improve in results
  25.      result[i] = getPrime(50);
  26.    }
  27.    
  28.    return result;
  29.  }
  30.  
  31.  private int getPrime(int t){
  32.    int result = 1;
  33.    while(true){
  34.      int r = rand.nextInt(t);
  35.      if(isPrime(r)){
  36.        return r;
  37.      }
  38.    }
  39.  }
  40.  
  41.  public static boolean isPrime(int n){
  42.  
  43.    for(int i = 2; i < n; i++){
  44.      if(n % i == 0){
  45.        return false;
  46.      }
  47.    }
  48.    
  49.    return true;
  50.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement