Created
August 14, 2020 23:25
-
-
Save TrashCoder96/8db79542ec75237bb985b47d50e3653d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.*; | |
public class Main { | |
final static int size = 2255; | |
public static void main(String[] args) { | |
int[][] matrix = algol(); | |
Set<Integer> hashSet = new HashSet<>(size * size); | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < size; j++) { | |
hashSet.add(matrix[i][j]); | |
} | |
} | |
System.out.println("Count in matrix = " + hashSet.size() + ". Expected count = " + size * size); //without unit tests!!! | |
} | |
public static int[][] algol() { | |
Random random = new Random(); | |
List<Integer> arr = new ArrayList<>(size * size); | |
int[][] matrix = new int[size][size]; | |
int step = Integer.MAX_VALUE / (size * size); | |
int last = 0; | |
for (int i = 0; i < size * size; i++) { | |
int newn = last + random.nextInt(step); | |
arr.add(newn); | |
last = newn + 1; | |
} | |
Collections.shuffle(arr); | |
int n = 0; | |
for (int i = 0; i < size; i++) { | |
for (int j = 0; j < size; j++) { | |
matrix[i][j] = arr.get(n++); | |
} | |
} | |
return matrix; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment