-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathnumericpattern135.py
More file actions
58 lines (44 loc) · 1.43 KB
/
numericpattern135.py
File metadata and controls
58 lines (44 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def get_primes(count):
"""Get the first 'count' prime numbers using Sieve of Eratosthenes."""
if count == 0:
return []
if count < 6:
limit = 15
else:
import math
limit = int(count * (math.log(count) + math.log(math.log(count)) + 2))
sieve = [True] * (limit + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(limit ** 0.5) + 1):
if sieve[i]:
for j in range(i * i, limit + 1, i):
sieve[j] = False
primes = [i for i in range(2, limit + 1) if sieve[i]]
while len(primes) < count:
limit *= 2
sieve = [True] * (limit + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(limit ** 0.5) + 1):
if sieve[i]:
for j in range(i * i, limit + 1, i):
sieve[j] = False
primes = [i for i in range(2, limit + 1) if sieve[i]]
return primes[:count]
def generate_prime_grid(n):
"""
Generate a grid filled with prime numbers in specific row order.
Args:
n: The size of the grid (n x n)
"""
primes = get_primes(n * n)
grid = [[0] * n for _ in range(n)]
idx = 0
for row in range(n):
for col in range(n):
grid[row][col] = primes[idx]
idx += 1
max_width = len(str(primes[-1]))
for row in grid:
print(' '.join(str(num).rjust(max_width) for num in row))
n = int(input())
generate_prime_grid(n)