Close

## Last digit of a power

Given a number expressed in base, exponent form (ab) where a is arbitrary big number For example contains 100 digits, We have to write a program to find the last digit in it’s expanded form. Eg: 45712 has a last digit of 1 Since the range of numbers is huge, it is impossible to expand…

## Number of rectangles

Given a number of unit squares (1 x 1). How many different rectangles can be formed with them? For example let us consider 4 units. We can form 5 different rectangles like the following. Two rectangles are considered same if they are oriented in a different way but same dimensions. Basically we have to arrange…

## Adding a list of big numbers

Most of the popular programming languages like C/C++, Java, C# provide primitive data types (int, long) to hold values up to only a specific range. A 64-bit integer can store only store numbers in the range of -264 to 263-1. This can store numbers containing roughly 19-20 digits.What if we have to deal with even…

## Number of trailing zeros in a factorial

This problem looks easy to solve for small numbers. Simply calculate the factorial and count how many zeroes are present at the end! But the factorial grows big very fast even for small numbers which cannot fit into standard data types. (20! = 2432902008176640000. Cannot fit into a long variable also). In summary this is…

## Finding the integer square root of a number

How do we implement a function to find the integer square root of a number? For example int_sqrt(4) = 2, int_sqrt(10) = 3. (We have to take floor of the actual square root) A simple method is to iterate i from 1 to N/2 and keep checking until i2 < N, and whenever i2 >=…

## Computing the power of a given number

Given a base and it’s exponent, how do we efficiently evaluate it? In other way, how do implement a pow() function which is typically provided by a library. For example 210 = 1024. The simplest method that comes to our mind is multiply the base with 1 in a loop for exponent times. double result…

## Counting the number of perfect squares in a given range

Given a range of numbers, how do we count the number of perfect squares? This problem is posted on Hackerrank as part of 20/20 Hack March 2014. If you do not want to read the solution and you want to solve the problem on your own, Click on the link. If you want to know…

## Generating a pascal triangle

In this post, we will write a program to generate a pascal triangle of given height. For example, Pascal triangle of height 5 is shown below 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 The idea behind Pascal triangle is that each element in a row is the…

## Program to find GCD of two numbers

In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), or highest common factor (hcf), of two or more integers (at least one of which is not zero), is the largest positive integer that divides the numbers without a remainder – from WikipediaFor example, the GCD of 8 and 12…