Created
March 9, 2021 21:21
-
-
Save terror/eff2d5ec38bf6b1fd1b2b9169125dc0d to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using static System.Console; | |
class Program { | |
static void Main(string[] args) { | |
Write("Number of rows: "); | |
int r = int.Parse(ReadLine()); // 5 | |
Write("\nNumebr of cols: "); | |
int c = int.Parse(ReadLine()); // 3 | |
int count = r * c; // 15 -> how many primes we want | |
int current = 0; // starting value for primes from 0..count | |
var ret = new int[r, c]; // 2d array return value | |
while (count != 0) { | |
if (isPrime(current)) { | |
insert(ret, current); count -= 1; | |
} | |
current += 1; | |
} | |
// Output | |
for(int i = 0; i < r; ++i) { | |
for(int j = 0; j < c; ++j) | |
Write(ret[i, j] + " "); | |
WriteLine(); | |
} | |
} | |
public static void insert(int[,] ret, int val) { | |
for(int i = 0; i < ret.GetLength(0); ++i) { | |
for(int j = 0; j < ret.GetLength(1); ++j) { | |
if (ret[i, j] == 0) { ret[i, j] = val; return; } | |
} | |
} | |
} | |
public static bool isPrime(int n) { | |
if (n <= 1) | |
return false; | |
else if (n % 2 == 0) | |
return n == 2; | |
for(int i = 3; i <= Math.Sqrt(n); i += 2) | |
if (n % i == 0) return false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
g
a
r
b
a
g
e
!