Python Pattern11
Python Pattern11
To print any pattern, you must first understand its logic and
technique. Use the below steps to print any pattern in Python.
1. Decide the number of rows and columns
The number of rows and columns is crucial when printing a
pattern. To achieve this, we utilize two loops: outer loops
and nested loops. The outer loop is the number of rows, while the
inner loop tells us the column needed to print the pattern.
num_terms = 5
result = sum_of_squares(num_terms)
print(f"The sum of squares of the first {num_terms} natural
numbers is: {result}")
num_terms = 5
result = sum_of_squares_formula(num_terms)
print(f"The sum of squares of the first {num_terms} natural
numbers is: {result}")
EXPLANATION
Method 1: Loops through numbers from 1 to n, squares each
number (i ** 2), and adds them up.
Method 2: Uses the formula n * (n + 1) * (2n + 1) // 6 to directly
calculate the sum of squares, which is faster.