Open In App

Aitken’s Array or Bell Triangle

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Bell triangle or Aitken’s array or the Pierce Triangle is the number triangle obtained by beginning the first row with the number one, then beginning subsequent rows with the last number of the previous row. The following spaces are filled out by adding the number in the preceding column to the number above it. Follow the image below to better understand it.

Aitken’s Array/Bell Triangle for N = 5

Problem:

Given an integer ‘n’, we need to print Aitken’s array upto ‘n’ rows.

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]

Approach to create/print Aitken’s array:

The idea to create Aitken’s array is based on series/pattern printing.

If we look into the examples above, we can find a pattern between any two rows, i.e.

  • The 1st element of the 1st row in the Aitken’s array is always equal to 1
  • The 1st element of any row r in the Aitken’s array is equal to the last element of the previous row.
  • The ith element of any row r in the Aitken’s array  (aitkens[r][i]) = aitkens[r][i-1] + aitkens[r-1][i-1]
Approach to create/print Aitken's array

Approach to create/print Aitken’s array

Steps that were to follow the above approach:

  • First, we have the base case, when n==1, we will return an array of only one element which is 1.
  • Next, we have the recursive call which returns the answer for (n-1) rows, in the form of an array, which we store in arrayFromLastCall[].
  • Next, we put in elements for the current row, arrayForCurrentCall[] in the following manner:
    • The first element, we put into the current array is the last element of the previous array.
    • For, the next elements, we form them by adding up the previous cell element from the current array and the element at the same position from the previous array.
  • Finally, print the current array, and return this array.

Implementation of Aitken’s array

C++




#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
vector<int> rec(int remainingNumberOfRows) {
    if (remainingNumberOfRows == 1) {
        vector<int> array {1};
        cout << "[1]" << endl;
        return array;
    }
    vector<int> arrayFromLastCall = rec(remainingNumberOfRows - 1);
    vector<int> arrayForCurrentCall(remainingNumberOfRows);
    arrayForCurrentCall[0] = arrayFromLastCall[arrayFromLastCall.size() - 1];
    for (int currentRow = 0; currentRow < remainingNumberOfRows - 1; currentRow++) {
        arrayForCurrentCall[currentRow + 1] = arrayForCurrentCall[currentRow] + arrayFromLastCall[currentRow];
    }
    cout << "[";
    for (int i = 0; i < arrayForCurrentCall.size(); i++) {
        cout << arrayForCurrentCall[i];
        if (i != arrayForCurrentCall.size() - 1) {
            cout << ", ";
        }
    }
    cout << "]" << endl;
    return arrayForCurrentCall;
}
 
int main() {
    int numberOfRows = 7;
    cout << "Aitken's array of " << numberOfRows << ": " << endl << endl;
    rec(numberOfRows);
    return 0;
}


Java




// Java Program to print AitkensArray
 
import java.io.*;
import java.util.*;
 
public class AitkensArray {
 
    // Recursive function to print the
    // Aitken's array.
    static int[] rec(int remainingNumberOfRows)
    {
        // Base case
        // 1st row will contain only 1
        // So based on tail recursion,
        // last recursion will be the 1st row
        if (remainingNumberOfRows == 1) {
            int[] array = { 1 };
            System.out.println(Arrays.toString(array));
            return array;
        }
 
        // Store array from previous recursion call
        int[] arrayFromLastCall
            = rec(remainingNumberOfRows - 1);
 
        // Create array to store the current row
        int[] arrayForCurrentCall
            = new int[remainingNumberOfRows];
 
        // Store last element from previous call array to
        // 1st index of current call array
        arrayForCurrentCall[0]
            = arrayFromLastCall[arrayFromLastCall.length
                                - 1];
 
        // Create the current call Aitken's array
        for (int currentRow = 0;
             currentRow < remainingNumberOfRows - 1;
             currentRow++) {
            arrayForCurrentCall[currentRow + 1]
                = arrayForCurrentCall[currentRow]
                  + arrayFromLastCall[currentRow];
        }
 
        // Print the current call Aitken's array
        // as the next row
        System.out.println(
            Arrays.toString(arrayForCurrentCall));
 
        // Return the new formed array as latest row
        return arrayForCurrentCall;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int numberOfRows = 7;
 
        System.out.println("Aitken's array of "
                           + numberOfRows + ": \n");
        rec(numberOfRows);
    }
}


Python3




def rec(remainingNumberOfRows):
    if remainingNumberOfRows == 1:
        array = [1]
        print("[1]")
        return array
    arrayFromLastCall = rec(remainingNumberOfRows - 1)
    arrayForCurrentCall = [0] * remainingNumberOfRows
    arrayForCurrentCall[0] = arrayFromLastCall[-1]
    for currentRow in range(remainingNumberOfRows - 1):
        arrayForCurrentCall[currentRow + 1] = arrayForCurrentCall[currentRow] + arrayFromLastCall[currentRow]
    print("[", end="")
    for i in range(len(arrayForCurrentCall)):
        print(arrayForCurrentCall[i], end="")
        if i != len(arrayForCurrentCall) - 1:
            print(", ", end="")
    print("]")
    return arrayForCurrentCall
 
if __name__ == "__main__":
    numberOfRows = 7
    print(f"Aitken's array of {numberOfRows}: \n")
    rec(numberOfRows)


C#




using System;
 
public class AitkensArray
{
    // Recursive function to print the
    // Aitken's array.
    static int[] Rec(int remainingNumberOfRows)
    {
       
        // Base case
        // 1st row will contain only 1
        // So based on tail recursion,
        // last recursion will be the 1st row
        if (remainingNumberOfRows == 1)
        {
            int[] array = { 1 };
            Console.WriteLine(string.Join(", ", array));
            return array;
        }
 
        // Store array from previous recursion call
        int[] arrayFromLastCall = Rec(remainingNumberOfRows - 1);
 
        // Create array to store the current row
        int[] arrayForCurrentCall = new int[remainingNumberOfRows];
 
        // Store last element from previous call array to
        // 1st index of current call array
        arrayForCurrentCall[0] = arrayFromLastCall[arrayFromLastCall.Length - 1];
 
        // Create the current call Aitken's array
        for (int currentRow = 0; currentRow < remainingNumberOfRows - 1; currentRow++)
        {
            arrayForCurrentCall[currentRow + 1] = arrayForCurrentCall[currentRow] + arrayFromLastCall[currentRow];
        }
 
        // Print the current call Aitken's array
        // as the next row
        Console.WriteLine(string.Join(", ", arrayForCurrentCall));
 
        // Return the new formed array as latest row
        return arrayForCurrentCall;
    }
 
    // Driver Code
    public static void Main()
    {
        int numberOfRows = 7;
 
        Console.WriteLine("Aitken's array of " + numberOfRows + ": \n");
        Rec(numberOfRows);
    }
}


Javascript




function rec(remainingNumberOfRows) {
  // Base case
  // 1st row will contain only 1
  // So based on tail recursion,
  // last recursion will be the 1st row
  if (remainingNumberOfRows == 1) {
    const array = [1];
    console.log(array);
    return array;
  }
 
  // Store array from previous recursion call
  const arrayFromLastCall = rec(remainingNumberOfRows - 1);
 
  // Create array to store the current row
  const arrayForCurrentCall = new Array(remainingNumberOfRows);
 
  // Store last element from previous call array to
  // 1st index of current call array
  arrayForCurrentCall[0] = arrayFromLastCall[arrayFromLastCall.length - 1];
 
  // Create the current call Aitken's array
  for (let currentRow = 0; currentRow < remainingNumberOfRows - 1; currentRow++) {
    arrayForCurrentCall[currentRow + 1] = arrayForCurrentCall[currentRow] + arrayFromLastCall[currentRow];
  }
 
  // Print the current call Aitken's array
  // as the next row
  console.log(arrayForCurrentCall);
 
  // Return the new formed array as latest row
  return arrayForCurrentCall;
}
 
// Driver Code
const numberOfRows = 7;
 
console.log(`Aitken's array of ${numberOfRows}: \n`);
rec(numberOfRows);


Output

Aitken's array of 7: 

[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]

Time complexity : O(n^2)

Space complexity : O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads