Bash Script to Print Floyd Triangle with Output and Explanation
Use a nested loop in Bash where the outer loop controls the rows and the inner loop prints consecutive numbers incrementing a counter, like
for ((i=1; i<=rows; i++)); do for ((j=1; j<=i; j++)); do echo -n "$num "; ((num++)); done; echo; done to print Floyd's triangle.Examples
Input3
Output1
2 3
4 5 6
Input5
Output1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Input1
Output1
How to Think About It
To print Floyd's triangle, think of printing numbers in rows where each row has one more number than the previous. Start with 1 and keep increasing the number after printing each one. Use an outer loop for rows and an inner loop for numbers in each row.
Algorithm
1
Get the number of rows as input2
Initialize a counter to 13
For each row from 1 to the number of rows:4
For each position in the current row:5
Print the current counter value and increment it6
Move to the next line after each rowCode
bash
#!/bin/bash read -p "Enter number of rows: " rows num=1 for ((i=1; i<=rows; i++)); do for ((j=1; j<=i; j++)); do echo -n "$num " ((num++)) done echo done
Output
Enter number of rows: 4
1
2 3
4 5 6
7 8 9 10
Dry Run
Let's trace printing Floyd's triangle for 3 rows through the code
1
Initialize
rows=3, num=1
2
First row (i=1)
Print num=1, then num=2
3
Second row (i=2)
Print num=2, num=3, then num=4
4
Third row (i=3)
Print num=4, num=5, num=6, then num=7
| Row (i) | Numbers printed | Next num value |
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 3 | 4 |
| 3 | 4 5 6 | 7 |
Why This Works
Step 1: Outer loop controls rows
The outer for loop runs from 1 to the number of rows, deciding how many lines to print.
Step 2: Inner loop prints numbers per row
The inner for loop runs from 1 to the current row number, printing that many numbers.
Step 3: Incrementing the number
A variable num starts at 1 and increases by 1 after printing each number to keep the sequence continuous.
Alternative Approaches
Using while loops
bash
#!/bin/bash read -p "Enter rows: " rows num=1 i=1 while [ $i -le $rows ]; do j=1 while [ $j -le $i ]; do echo -n "$num " ((num++)) ((j++)) done echo ((i++)) done
While loops offer the same logic but use different syntax; some prefer them for clarity.
Using seq command and arithmetic
bash
#!/bin/bash read -p "Enter rows: " rows num=1 for i in $(seq 1 $rows); do for j in $(seq 1 $i); do echo -n "$num " ((num++)) done echo done
Using <code>seq</code> generates sequences explicitly; it is more readable but slightly slower.
Complexity: O(n^2) time, O(1) space
Time Complexity
The nested loops run roughly n*(n+1)/2 times, which is O(n^2), where n is the number of rows.
Space Complexity
Only a few variables are used, so space complexity is O(1), constant space.
Which Approach is Fastest?
All approaches have similar time complexity; using built-in loops is slightly faster than calling external commands like seq.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loops with arithmetic | O(n^2) | O(1) | Simplicity and speed |
| While loops | O(n^2) | O(1) | Preference for while syntax |
| Using seq command | O(n^2) | O(1) | Readability but slightly slower |
Use
echo -n to print numbers on the same line without a newline.Forgetting to increment the number after printing causes repeated numbers in the triangle.