Open In App

Python program to print Aitken’s array

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:

Below is the implementation.




# 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]
Article Tags :