Python program to print Aitken's array
Last Updated :
23 Jan, 2020
Given a number n. The task is to print the Aikten's array upto n.
Examples:
Input: 5
Output:
[1]
[1, 2]
[2, 3, 5]
[5, 7, 10, 15]
[15, 20, 27, 37, 52]
Input: 7
Output:
[1]
[1, 2]
[2, 3, 5]
[5, 7, 10, 15]
[15, 20, 27, 37, 52]
[52, 67, 87, 114, 151, 203]
[203, 255, 322, 409, 523, 674, 877]
To print it first we follow the following steps:
- We write 1 in the first row.
- Next, we fill the leftmost value of each row by the rightmost value of the previous row.
- The next elements of each row are filled by the simple rule i.e. each element of a particular row is the sum of values to the left of that row with the values of the upper row at the same position.
For better understanding, let's consider the third row of the above example consisting of the elements 2, 3, 5. The leftmost value of this row is the rightmost value of the above row i.e. 2. The next value 3 is the sum of value to the left if that row (2) and the value at same position of the above row (1). Similarly, 5 is the sum of 3 and 2.
Below is the implementation.
Python3 1==
# Python program to print
# Aitken's array
from queue import Queue
from functools import reduce, lru_cache
# for dynamic programming
# Recursive function to print the
# Aitken's array.
@lru_cache()
def rec(n):
# Base case
if n == 1:
print([1])
return [1]
array = [rec(n-1)[-1]]
for k in range(n-1):
array.append(array[k] + rec(n-1)[k])
print(array)
return array
# Driver's code
rec(7)
Output:
[1]
[1, 2]
[2, 3, 5]
[5, 7, 10, 15]
[15, 20, 27, 37, 52]
[52, 67, 87, 114, 151, 203]
[203, 255, 322, 409, 523, 674, 877]
Similar Reads
Python program to print the dictionary in table format Given a Dictionary. The task is to print the dictionary in table format.Examples:Input: {1: ["Samuel", 21, 'Data Structures'], 2: ["Richie", 20, 'Machine Learning'], 3: ["Lauren", 21, 'OOPS with java'], }Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with
2 min read
Python Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12
2 min read
How to Print a Dictionary in Python Python Dictionaries are the form of data structures that allow us to store and retrieve the key-value pairs properly. While working with dictionaries, it is important to print the contents of the dictionary for analysis or debugging.Example: Using print FunctionPython# input dictionary input_dict =
3 min read
Python Program to Find and Print Address of Variable In this article, we are going to see how to find and print the address of the Python variable. It can be done in these ways:Using id() functionUsing addressof() functionUsing hex() functionMethod 1: Find and Print Address of Variable using id()We can get an address using id() function, id() function
2 min read
Python Program to Convert Matrix to String Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string.Using List ComprehensionList comprehension provides a con
2 min read