Advertisement
Guest User

A326406

a guest
Oct 2nd, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.lang.Math;
  2.  
  3. class A326406{
  4.     public static void main(String[] args){
  5.     int length = 100;                                               //length of sequence
  6.     int side = (int) (Math.ceil((Math.sqrt(8*length+1)-1)/2)+1);
  7.     int[][] grid = new int[side][side];
  8.     fillGrid(grid);
  9.     //display(grid);
  10.     int[][] marked = markMines(grid);
  11.     int[][] transf = transform(marked);
  12.     read(length, transf);
  13.     }
  14.    
  15.     static int[] coordinates(int n){
  16.         int[] output = new int[2];
  17.         int lower = (int) (Math.ceil((Math.sqrt(8*n+1)-1)/2)-1);
  18.         int max = (lower+2)*(lower+1)/2;
  19.         int reminder = max - n;
  20.        
  21.         if(lower%2==1){
  22.             output[0]= lower - reminder;
  23.             output[1]= reminder;
  24.         } else {
  25.             output[1]= lower - reminder;
  26.             output[0]= reminder;
  27.         }
  28.        
  29.        
  30.         //System.out.println(n+"   " + lower + "  " + reminder + "  " + output[0]+ "   "+output[1]);
  31.         return output;
  32.     }
  33.    
  34.     static void fillGrid(int[][] grid){
  35.         grid[0][0] = 1;
  36.         for (int i = 2; i < grid.length*grid.length+1 ; i++){
  37.             int[] position = coordinates(i);
  38.             if (position[0] > -1 && position[1]>-1 && position[0] < grid.length && position[1] < grid.length) {grid[position[0]][position[1]]=i;}
  39.         }
  40.     }
  41.  
  42.     static int[][] markMines(int[][] grid){
  43.         int[][] output = new int[grid.length][grid.length];
  44.         for(int i = 0; i < grid.length; i++){
  45.             for(int j = 0; j < grid.length; j++){
  46.                 if (isPrime(grid[i][j])){
  47.                     output[i][j] = -1;
  48.                 } else {
  49.                     output[i][j] = grid[i][j];
  50.                 }
  51.             }
  52.         }
  53.         return output;
  54.     }
  55.  
  56.     static int checkMine(int i, int j, int[][] grid){
  57.         if (grid[i][j] == -1) {return 1;}
  58.         else {return 0;}
  59.     }
  60.  
  61.     static int[][] transform(int[][] grid){
  62.         int[][] output = new int[grid.length][grid.length];
  63.        
  64.         for(int i = 0; i < grid.length; i++){
  65.             for(int j = 0; j < grid.length; j++){
  66.                 if (checkMine(i,j,grid)==1){output[i][j]=-1;}
  67.                 else {
  68.                     for (int k = -1; k<2;k++){
  69.                         for (int l = -1; l<2;l++){
  70.                             if ((i+k>-1) && (j+l>-1) && (i+k<grid.length) && (j+l<grid.length)) {output[i][j]=output[i][j]+checkMine(i+k,j+l,grid);}
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         return output;
  77.     }
  78.    
  79.     static boolean isPrime(int n){
  80.         if (n==1){return false;}
  81.         if (n==2){return true;}
  82.         if (n>2){
  83.             if (n%2==0) {return false;}
  84.             else{
  85.                 for(int i=3; i*i < n+1 ;i++){
  86.                     if (n%i == 0) {return false;}
  87.                 }
  88.             }
  89.         }
  90.         return true;
  91.     }
  92.    
  93.     static void display(int[][] array){
  94.         for (int i = 0; i< array.length; i++){
  95.             for (int j = 0; j< array[0].length; j++){
  96.                 System.out.print(array[i][j]+"\t");
  97.             }
  98.         System.out.println();
  99.         }
  100.     }
  101.  
  102.     static void read(int n, int[][] grid){
  103.         for (int i = 1; i< n+1; i++){
  104.             System.out.println(i+ "  " + grid[coordinates(i)[0]][coordinates(i)[1]]);
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement