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]
Sieve of Atkins
Problem
Given a number n, print all primes smaller than or equal to n.
Example
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19″.
Algorithm
We have discussed sieve of Eratosthenes here, but lets move to the Sieve of Atkins algorithm today which is a modified version of sieve of Eratosthenes.
[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]
To find the list of primes till N
Problem
Given a number n, print all primes smaller than or equal to n.
Example
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19″.
Solution
Method 1 - Brute force
We just loop till number N, and check if individual iterator is prime or not.
Code :
[Read More]
Sieve of Eratosthenes
Problem
Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
Example
For example, if n is 10, the output should be “2, 3, 5, 7″. If n is 20, the output should be “2, 3, 5, 7, 11, 13, 17, 19″.
Algorithm
The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so (Ref Wiki).
[Read More]
Efficient ways to check if number is prime number
Problem
How to check if number is prime or not
You may remember that a prime number is one whose only factors are itself and 1. Other numbers are called oblong numbers (they can be represented as an oblong of dots) or composite numbers.
If you have a number and you want to find out if it’s prime, that is called performing a primality test. The naive approach is to check all numbers m from 2 to sqrt(n) and verify that n % m is not 0.
[Read More]
Prove that number between twin primes is divided by 6
A Twin Prime number is a prime number which differs from other prime number by two. eg (5,7), (11,13), (17,19), (29,31), (41,43), (59,61)…….. Prove that number between twin primes is divisible by 6. Solution 1:
all prime numbers are of the form 6x+1 or 6x-1
so to be twin primes they should be 6x-1 and 6x+1 for some x
so the number netween them is 6x which is divided by 6
[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]