## Question: Triangle Quest 2 - Hacker Rank (Python)

You are given a positive integer ** N**.

Your task is to print a palindromic triangle of size

**.**

*N*For example, a palindromic triangle of size **5** is:

```
1
121
12321
1234321
123454321
```

You can’t take more than two lines. The first line (a for-statement) is already written for you.

You have to complete the code using exactly one print statement.

Using more than one for-statement will give a score of 0.

**Input Format:**

A single line of input containing the integer **N**.

**Constraints:**

**0 < N < 10**

**Output format**

Print the palindromic triangle of size **N** as explained above.

**Sample input:**

`5`

**Sample output:**

```
1
121
12321
1234321
123454321
```

## Possible solutions

As you can see, we again have to use the loop and we are not allowed to have a solution in more than two lines to pass the test. So, again we will have only one possible solution to pass the test.

While running the code, make sure that you have not copied the comments.

- Using Python for loop
- Alternative solution

### Solution-1: Using For loop

We will now use the for loop to find the triangle of a number based on the above-mentioned conditions. let us start the python program. First, we will use the for loop with will iterate from 1 to a number input by the user. Then we will use a formula that will help us to build the required triangle.

```
# usig for loop which takes the input from user
for i in range(1, int(input()) + 1):
# printing the triangle of the number
print((10 ** i - 1) ** 2 // 81)
```

Input:

`5`

Output:

```
1
121
12321
1234321
123454321
```

As you can see, we were able to find out the required triangle using Python and our solution is also in two lines. Let us now understand how the formula is working and how it is able to print out the triangle as required in the question.

`(10 ** i - 1) ** 2 // 81`

If i == 1, we get:

```
= (10** 1 - 1) ** 2 // 81
= (9) ** 2 // 81
= 81 // 81
= 1
```

When the i == 2:

```
= ( 10 ** 2 - 1) ** 2 // 81
= ( 99) **2 // 81
= 9801 // 81
= 121
```

When i == 3:

```
= ( 10 ** 3 -1 ) ** 2 // 81
= ( 999) **2 // 81
= 998001 // 81
= 12321
```

And so on.

Now, to pass the test on Hacker rank, it is recommended to write the code in the editor without comments because the editor reads the code up to 2 lines only as shown below:

When you type code without comments you will be able to pass the test.

### Solution-2: Using different calculation

Another way to solve the question is by altering the equation and getting the same result. Let us implement in Python.

```
# using for loop
for i in range(1,int(input())+1):
# changing the formula to get the same results
print (((10**i - 1)//9)**2)
```

Input:

`5`

Output:

```
1
121
12321
1234321
123454321
```

As you can see, we get the desired result. The formula works in a similar way to the previous one. You can check it by putting values one by one as we did in the previous solution.

## Summary

In this short article, we learn how we can solve the Triangle Quest 2 question on Hacker Rank. As per requirement, we were able to solve the question within two lines of code.

## References

Python loops

Hacker Rank Solution for Triangle Quest 2

*Related Keywords: triangle quest hackerrank solution, triangle quest 2 hackerrank solution, triangle quest 2 hackerrank solution python3, triangle quest 2, triangle quest 2 hackerrank*