Realization of Yang Hui Triangle by shell script

Keywords: Linux

According to Yang Hui triangle, the first and last elements of each row are 1, and each number is equal to the sum of the two numbers above it, and the number of elements in each row is equal to the number of rows. Using these rules, we can easily realize Yang Hui triangle!
My idea is to use two arrays, loop each other according to each other's elements to assign their own elements, and both end and end are 1.

OK I hope you can understand it. Here is the code implementation.

    #!/bin/bash
    declare -a triangle
    declare -a triangleTwo
    #Assign a value to the first element of an array
    triangle[0]=1
    triangleTwo[0]=1
    #Vertical flag, which is used to execute a code segment in turn in a loop
    flag=true
    #Print 1 of triangle array first
    echo $triangle
    #Let's start with a five-line Yang Hui triangle.
    for ((i=1;i<$1;i++)); do
        #Use if else statements to loop code segments that are executed in turn
        if $flag; then
            #Take out the number of elements of triangle and use it to do the following number of cycles
            numbers=${#triangle[*]}
            #Assign a value of 1 to the last character
            triangleTwo[$numbers]=1
            #Determine the number of times a loop assignment is performed based on the length of triangle
            for ((x=1;x<=${numbers};x++));do
                #The value of triangle Two to the two adjacent elements in triangle
                triangleTwo[x]=$((triangle[$[x-1]]+triangle[x]))
            done
            #Print triangleTwo array
            echo ${triangleTwo[*]}
            #Change fla g to achieve the effect of circular execution of code segments
            flag=false
        else
            numbers=${#triangleTwo[*]}
            triangle[$numbers]=1
            for ((x=1;x<=${numbers};x++));do
                triangle[x]=$((triangleTwo[$[x-1]]+triangleTwo[x]))
            done
            echo ${triangle[*]} 
            flag=true
            fi  
    done

This is not enough. We can also let the user enter the number of rows and print the corresponding number of rows.

#!/bin/bash
YangHuiTriangle (){
    declare -a triangle
    declare -a triangleTwo
    #Assign a value to the first element of an array
    triangle[0]=1
    triangleTwo[0]=1
    #Vertical flag, which is used to execute a code segment in turn in a loop
    flag=true
    #Print 1 of triangle array first
    echo $triangle
    #Let's start with a five-line Yang Hui triangle.
    for ((i=1;i<$1;i++)); do
        #Use if else statements to loop code segments that are executed in turn
        if $flag; then
            #Take out the number of elements of triangle and use it to do the following number of cycles
            numbers=${#triangle[*]}
            #Assign a value of 1 to the last character
            triangleTwo[$numbers]=1
            #Determine the number of times a loop assignment is performed based on the length of triangle
            for ((x=1;x<=${numbers};x++));do
                #The value of triangle Two to the two adjacent elements in triangle
                triangleTwo[x]=$((triangle[$[x-1]]+triangle[x]))
            done
            #Print triangleTwo array
            echo ${triangleTwo[*]}
            #Change fla g to achieve the effect of circular execution of code segments
            flag=false
        else
            numbers=${#triangleTwo[*]}
            triangle[$numbers]=1
            for ((x=1;x<=${numbers};x++));do
                triangle[x]=$((triangleTwo[$[x-1]]+triangleTwo[x]))
            done
            echo ${triangle[*]} 
            flag=true
            fi
    done
}

while true; do 
    read -p "Please enter the number of elements(or input q to quit):" line
    [ "$line" == q ] && break
    if [[ $line =~ ^[0-9]+$ ]]; then
        YangHuiTriangle $line
    else
        echo "Please input correct number."
    fi
done        

Implementation results:

Okay! Great success!
If the author has any mistakes or needs improvement, please leave a message to inform me!

Posted by sinisake on Mon, 08 Apr 2019 01:51:30 -0700