Sobes.tech
Middle+

Write a Python program: create a 1000x1000 matrix, fill rows with Fibonacci sequence, square the elements of the secondary diagonal. Time: 2 minutes.

sobes.tech AI

Answer from AI

python import numpy as np import time

start_time = time.time()

Function to generate Fibonacci sequence of length n

def fibonacci_sequence(n): fib = [0, 1] for i in range(2, n): fib.append(fib[i-1] + fib[i-2]) return fib

size = 1000 matrix = np.zeros((size, size), dtype=np.int64) fib_seq = fibonacci_sequence(size)

Filling each row with the Fibonacci sequence

for i in range(size): matrix[i, :] = fib_seq

Squaring the elements on the secondary diagonal

for i in range(size): j = size - 1 - i matrix[i, j] = matrix[i, j] ** 2

print(f"Execution time: {time.time() - start_time:.2f} seconds")