Fizz Buzz

Problem Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz” Example of sequence - 3,6,9,12,15,18,1,24,27. Solution Method 1- Brute force Here we notice that that after 15, there is 18. So, we should increment the iterator by 2, and then 1 i. [Read More]

Fizz Buzz

Problem Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz” Example of sequence - 3,6,9,12,15,18,1,24,27. Solution Method 1- Brute force Here we notice that that after 15, there is 18. So, we should increment the iterator by 2, and then 1 i. [Read More]

Algorithm to find out number of factors of a number

Problem Given a number find the number of factors of that number Example Factors of 18 are 1, 2, 3, 6, 9, 18. Hence 6 factors. Solution Method 1 - Using divisor function. Let n be the given number. If n = p1^e1 * p2^e2 * ... * pk^ek, where each p is a prime number, then the number of factors of n is (e1 + 1)*(e2 + 1)* . [Read More]

Program to print all prime factors of a given number

Problem Given a number n, write an efficient function to print all prime factors of n.  Example For example, if the input number is 12, then output should be “2 2 3″. And if the input number is 315, then output should be “3 3 5 7″. Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. [Read More]

Find the nth Ugly Number

Problem Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. …By convention, 1 is included. Write a program to find and print the 1500’th ugly number.   Solution Method 1 - Brute force Loop for all positive integers until ugly number count is smaller than n, if an integer is ugly than increment ugly number count. [Read More]