# Dudley triangle

The Dudley Triangle is a pattern of numbers displayed in a triangular shape. It is named after mathematician Dudley T. Dudley, who discovered this unique pattern. The triangle consists of a series of rows, with each row containing a sequence of numbers.

## Problem Statement

The goal is to write a program that generates the Dudley Triangle and displays it on the screen. The program takes an input parameter 'n', which determines the number of rows in the triangle. Each number in the triangle is calculated based on its position and the row it belongs to.

For example, given n = 10, the program should generate the following Dudley Triangle:

```  2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
```

This pattern is obtained by following a specific calculation:

```ans = j * (j + 1) % (r + 2)
```

Where 'r' represents the row number and 'j' represents the position within the row. Each number in the triangle is the result of this calculation. The calculation involves the modulo operator, which ensures that the numbers stay within a specific range.

## Algorithm and Pseudocode

To generate the Dudley Triangle, we can use a nested loop structure. The outer loop iterates over each row, while the inner loop iterates within each row to calculate and display the numbers.

The steps to generate the Dudley Triangle are as follows:

```1. Start the dudleyTriangle function with the input parameter 'n'.
2. If n is less than or equal to 0, return.
3. Initialize a variable 'ans' to 0.
4. Start the outer loop for 'r' from 1 to n.
a. Start the inner loop for 'j' from 1 to r.
i. Calculate the Dudley Triangle number using the formula:
ans = j * (j + 1) % (r + 2)
ii. Display the calculated number on the screen.
b. Print a new line after each row is completed.
5. End the function.
6. In the main function:
a. Call the dudleyTriangle function with the desired number of rows as the argument.
b. Return 0 to exit the program.
```

## Code Solution

``````// C Program for
// Dudley triangle
#include <stdio.h>

void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j *(j + 1) % (r + 2);
// Display calculated result
printf("  %d", ans);
}
// include new line
printf("\n");
}
}
int main()
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
dudleyTriangle(10);
return 0;
}``````

#### Output

``````  2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2``````
``````// Java program for
// Dudley Triangle
public class Triangle
{
public void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
System.out.print(" " + ans);
}
// include new line
System.out.print("\n");
}
}
public static void main(String[] args)
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}
}``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````// Include header file
#include <iostream>

using namespace std;
// C++ program for
// Dudley Triangle
class Triangle
{
public: void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j *(j + 1) % (r + 2);
// Display calculated result
cout << " " << ans;
}
// include new line
cout << "\n";
}
}
};
int main()
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
return 0;
}``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````// Include namespace system
using System;
// Csharp program for
// Dudley Triangle
public class Triangle
{
public void dudleyTriangle(int n)
{
if (n <= 0)
{
return;
}
int ans = 0;
for (int r = 1; r <= n; ++r)
{
for (int j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
Console.Write(" " + ans);
}
// include new line
Console.Write("\n");
}
}
public static void Main(String[] args)
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}
}``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````package main
import "fmt"
// Go program for
// Dudley Triangle

func dudleyTriangle(n int) {
if n <= 0 {
return
}
var ans int = 0
for r := 1 ; r <= n ; r++ {
for j := 1 ; j <= r ; j++ {
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2)
// Display calculated result
fmt.Print(" ", ans)
}
// include new line
fmt.Print("\n")
}
}
func main() {

/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
dudleyTriangle(10)
}``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````<?php
// Php program for
// Dudley Triangle
class Triangle
{
public	function dudleyTriangle(\$n)
{
if (\$n <= 0)
{
return;
}
\$ans = 0;
for (\$r = 1; \$r <= \$n; ++\$r)
{
for (\$j = 1; \$j <= \$r; ++\$j)
{
// Calculate dudley triangle
\$ans = \$j * (\$j + 1) % (\$r + 2);
// Display calculated result
echo(" ".\$ans);
}
// include new line
echo("\n");
}
}
}

function main()
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}
main();``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````// Node JS program for
// Dudley Triangle
class Triangle
{
dudleyTriangle(n)
{
if (n <= 0)
{
return;
}
var ans = 0;
for (var r = 1; r <= n; ++r)
{
for (var j = 1; j <= r; ++j)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
process.stdout.write(" " + ans);
}
// include new line
process.stdout.write("\n");
}
}
}

function main()
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}
main();``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````#  Python 3 program for
#  Dudley Triangle
class Triangle :
def dudleyTriangle(self, n) :
if (n <= 0) :
return

ans = 0
r = 1
while (r <= n) :
j = 1
while (j <= r) :
#  Calculate dudley triangle
ans = j * (j + 1) % (r + 2)
#  Display calculated result
print(" ", ans, end = "")
j += 1

#  include new line
print(end = "\n")
r += 1

def main() :
#    n = 10 [row]
#    ----------
#    2
#    2  2
#    2  1  2
#    2  0  0  2
#    2  6  5  6  2
#    2  6  4  4  6  2
#    2  6  3  2  3  6  2
#    2  6  2  0  0  2  6  2
#    2  6  1  9  8  9  1  6  2
#    2  6  0  8  6  6  8  0  6  2

if __name__ == "__main__": main()``````

#### Output

``````  2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2``````
``````#  Ruby program for
#  Dudley Triangle
class Triangle
def dudleyTriangle(n)
if (n <= 0)
return
end

ans = 0
r = 1
while (r <= n)
j = 1
while (j <= r)
#  Calculate dudley triangle
ans = j * (j + 1) % (r + 2)
#  Display calculated result
print(" ", ans)
j += 1
end

#  include new line
print("\n")
r += 1
end

end

end

def main()
#    n = 10 [row]
#    ----------
#    2
#    2  2
#    2  1  2
#    2  0  0  2
#    2  6  5  6  2
#    2  6  4  4  6  2
#    2  6  3  2  3  6  2
#    2  6  2  0  0  2  6  2
#    2  6  1  9  8  9  1  6  2
#    2  6  0  8  6  6  8  0  6  2
end

main()``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2
``````
``````// Scala program for
// Dudley Triangle
class Triangle()
{
def dudleyTriangle(n: Int): Unit = {
if (n <= 0)
{
return;
}
var ans: Int = 0;
var r: Int = 1;
while (r <= n)
{
var j: Int = 1;
while (j <= r)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
print(" " + ans);
j += 1;
}
// include new line
print("\n");
r += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Triangle = new Triangle();
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}
}``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````
``````// Swift 4 program for
// Dudley Triangle
class Triangle
{
func dudleyTriangle(_ n: Int)
{
if (n <= 0)
{
return;
}
var ans: Int = 0;
var r: Int = 1;
while (r <= n)
{
var j: Int = 1;
while (j <= r)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
print(" ", ans, terminator: "");
j += 1;
}
// include new line
print(terminator: "\n");
r += 1;
}
}
}
func main()
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}
main();``````

#### Output

``````  2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2``````
``````// Kotlin program for
// Dudley Triangle
class Triangle
{
fun dudleyTriangle(n: Int): Unit
{
if (n <= 0)
{
return;
}
var ans: Int ;
var r: Int = 1;
while (r <= n)
{
var j: Int = 1;
while (j <= r)
{
// Calculate dudley triangle
ans = j * (j + 1) % (r + 2);
// Display calculated result
print(" " + ans);
j += 1;
}
// include new line
print("\n");
r += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
/*
n = 10 [row]
----------
2
2  2
2  1  2
2  0  0  2
2  6  5  6  2
2  6  4  4  6  2
2  6  3  2  3  6  2
2  6  2  0  0  2  6  2
2  6  1  9  8  9  1  6  2
2  6  0  8  6  6  8  0  6  2
*/
}``````

#### Output

`````` 2
2 2
2 1 2
2 0 0 2
2 6 5 6 2
2 6 4 4 6 2
2 6 3 2 3 6 2
2 6 2 0 0 2 6 2
2 6 1 9 8 9 1 6 2
2 6 0 8 6 6 8 0 6 2``````

## Time Complexity

The time complexity of the Dudley Triangle algorithm is O(n^2), where n is the number of rows. This is because we have a nested loop structure where the outer loop runs n times, and the inner loop runs on average (n+1)/2 times for each row. Therefore, the overall time complexity is quadratic.

## Output Explanation

The output of the given code is the Dudley Triangle with 10 rows. Each number in the triangle is calculated using the formula mentioned earlier, and the result is displayed on the screen. The numbers are arranged in a triangular shape, with each row starting on a new line.

For example, in the first row, the number 2 is displayed. In the second row, the numbers 2 and 2 are displayed. In the third row, the numbers 2, 1, and 2 are displayed, and so on.

The Dudley Triangle pattern continues until the specified number of rows is reached. Each number in the triangle is calculated based on its position within the row and the row number. The modulo operation ensures that the numbers stay within a certain range.

The output of the given code matches the expected Dudley Triangle for n = 10, as mentioned earlier in the problem statement.

Finally, the Dudley Triangle is a fascinating pattern of numbers arranged in a triangular shape. The given code successfully generates and displays the Dudley Triangle based on the specified number of rows. It follows a simple calculation formula and uses nested loops to iterate over the rows and positions within each row.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.