Go Language Program to Implement Floyd's Triangle


Floyd's Triangle is a right-angled triangular pattern of numbers, named after the renowned American computer scientist Robert W. Floyd. Using sequences of natural integers starting at 1 and increasing by 1 in each row, we build this triangle. In this article, we are going to implement Floyd's Triangle in go, implementation here means we are going to create floyd's triangle and then print it.

Explanation

Floyd's Triangle, much like Pascal's Triangle, is a triangular arrangement of the natural numbers with a right angle. There is progression of numbers from left to right across the table, starting at 1 in each row of the triangle, then 2 and 3, and after that 4, 5, and 6, and so on. Extra digits are added to the row total starting with the next highest row.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

In this triangle each row has one more number than the previous row, and number increases from left to right.

Syntax

func floydsTriangleMethod1(rows int)

The syntax defines finction push for implementing Floyd's Triangle using nested loops in Go, starting with 1 and prints consecutive natural numbers row by row, forming the pattern of Floyd's Triangle with specified number of rows.

Algorithm

  • Start by import the required package (fmt).

  • Define function implementation.

  • Use nested loops to iterate through rows and columns, printing numbers incrementally.

  • Prompt the user to input the number of rows.

  • Compile, run the program, and observe the Floyd's Triangle pattern.

Example 1

In this example, both num and rowCount are initialized to 1 in this case. The number of rows, which begins at 1 and may reach whatever value the user specifies, is controlled by an outer loop. Next, we go through an inner loop to display the row's numbers, increasing the num variable by one after each iteration.

package main
import "fmt"
func floydsTriangleMethod1(rows int) {
    num := 1
	for i := 1; i <= rows; i++ {
    	for j := 1; j <= i; j++ {
            fmt.Print(num, " ")
            num++
        }
        fmt.Println()
	}
}
func main() {
	rows := 5
	fmt.Println("Floyd's Triangle using Nested Loops:")
	floydsTriangleMethod1(rows)
}

Output

Floyd's Triangle using Nested Loops:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Example 2

In this example, we will implement Floyd's Triangle in go, the formula start = rowNumber * (rowNumber - 1) / 2 + 1 is used to calculate the beginning value for each row. By incrementing the loop's beginning value by one before printing each new row, a single loop is used to display all of the data in a single row.

package main
import "fmt"
func floydsTriangleMethod2(rows int) {
    start := 1
    for i := 1; i <= rows; i++ {
    	end := start + i - 1
        for num := start; num <= end; num++ {
        	fmt.Print(num, " ")
    	}
    	fmt.Println()
    	start = end + 1
	}
}
func main() {
    rows := 5
	fmt.Println("Floyd's Triangle using Optimized Approach:")
	floydsTriangleMethod2(rows)
}

Output

Floyd's Triangle using Optimized Approach:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Real Life Implementation

  • Memory Testing: Floyd's Triangle has been discovered as a useful tool for memory testing and cognitive evaluation. Participants may be asked to recall specific numerical values from the triangle or identify patterns within it, allowing for an assessment of their memory and pattern recognition abilities.

  • Data Encryption and Cryptography: The usage of patterns like Floyd's Triangle in various encryption methods allows for the development of pseudorandom integer sequences, which are then used for cryptographic purposes. These sequences have the potential to provide an element of randomness to encryption techniques.

Conclusion

Floyd's Triangle is a fascinating geometric depiction of numerical sequences because of its simplicity and elegance. In this article, we explored two different methods to implement Floyd's Triangle in Go. The nested loops method is easy to understand and suitable for smaller triangle sizes, while the optimized approach offers improved efficiency, making it a better choice for generating larger Floyd's Triangles.

Updated on: 18-Oct-2023

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements