Guest User

A326405

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