Close

## Minimum number of squares formed from a rectangle

Given a rectangle of some length and width, How do you divide them into minimum number of squares? This is a problem from Codechef.For example give a rectangle of size 2 x 4, we can make them into two squares of 2 x 2.Similarly a rectangle of size 3 x 7, can only be divided…

## How to check if a given number is Fibonacci number

How to check if the given number is a Fibonacci number? A Fibonacci series starts with 0, 1. Remaining elements are formed by adding previous two numbers. Here are a few Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,… If a number is given, a straight forward way to check if…

## 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…

## Printing the prime factorization of a given number

How do we write a program to find the prime factorization of a given number? For example prime factorization for 18 can written as 2 * 3 * 3 or 2 * 3 ^ 2. For any number there is only one possible prime factorization.  An efficient algorithm can be given as follows. we run…

## Finding all the factors of a given number

Given a number, how do we write a program to find all of it’s factors or divisors. For example, for the number 18, the factors are {1, 2, 3, 6, 9, 18}. An immediate algorithm that comes to our mind is to run a loop from 1 to n. In each iteration check if the…