diff --git a/Numeric Patterns/numericpattern134.py b/Numeric Patterns/numericpattern134.py index d98d70f4..5ddcd466 100644 --- a/Numeric Patterns/numericpattern134.py +++ b/Numeric Patterns/numericpattern134.py @@ -1,21 +1,43 @@ -print("Enter the no of rows: ") +def generate_pattern(rows=5): + """ + Generate a pattern dynamically where: + - Odd rows (1st, 3rd, 5th...): Increment normally + - Even rows (2nd, 4th...): Fill in reverse order at the end + + Example for 5x5: + Row 1: 1-5 (block 0) + Row 2: 11-15 (block 2) + Row 3: 21-25 (block 4) + Row 4: 16-20 (block 3) + Row 5: 6-10 (block 1) + """ + pattern = [] + + odd_rows = (rows + 1) // 2 + even_rows = rows // 2# Number of even-positioned rows + + block_order = [] + for i in range(odd_rows): + block_order.append(i * 2) + + for i in range(even_rows - 1, -1, -1): + block_order.append(i * 2 + 1) + + for block in block_order: + row = [] + start = block * rows + 1 + for j in range(rows): + row.append(start + j) + pattern.append(row) + + return pattern + + +def print_pattern(pattern): + """Print the pattern in a formatted grid""" + for row in pattern: + print(' '.join(f'{num:3d}' for num in row)) + n = int(input()) -count = 1 -for i in range(n): - for j in range(n): - print(count, end=" ") - count+=1 - if(count>24): - count-=10 - else: - count+=5 - - print() - -Enter the no of rows: -5 -1 2 3 4 5 -11 12 13 14 15 -21 22 23 24 25 -16 17 18 19 20 -26 27 28 29 30 +pattern = generate_pattern(n) +print_pattern(pattern) diff --git a/Numeric Patterns/numericpattern135.py b/Numeric Patterns/numericpattern135.py new file mode 100644 index 00000000..b9920e1b --- /dev/null +++ b/Numeric Patterns/numericpattern135.py @@ -0,0 +1,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) \ No newline at end of file