Triangle Quest - HackerRank Solution Python

Featured image

Triangle Quest is a medium-level problem of python on Hackerrank that requires knowledge of loop and string pattern in python. In this post, you will learn how to solve Hackerrank’s Triangle Quest problem and its solution in Python.

Problem Statement and Explanation

Based on the given a positive integer N, print a numerical triangle of height N-1. Complete the code using loop, arithmetic operators, and print statement.

Input Format

Triangle Quest HackerRank Solution python

Explanation of Solution

The formula works by first calculating the product of i and pow(10, i) - 1. This product represents the number of 1s in a string of i digits. For example, if i is 2, the product is 2 * (10^2 - 1) = 19. This means that a string of 2 digits will have 19 1s in it.

The formula then divides this product by 9. This is because any number that consists only of 1s is divisible by 9. For example, the number 198 is divisible by 9 because 1 + 9 + 8 = 18, the output will be 22.

The result of the division is then converted to an integer and printed to the console.