Wednesday, April 29, 2009

Decimal to Binary

Input : any Decimal Number

Output : Binary format 

Logic:

1) Take any natural number say..10.
2)  divide by 2, and find remainder...repeat the same process until u get 0.
divisor number remainder
         2 10 (0)
          ---------------------------------
      2 5 (1)
      ----------------------------------
      2 2 (0)
      ----------------------------------
      2 1 (1)
      ----------------------------------
            0
3) Now look at the remainder pattern from bottom to top, ie. 1010 which is an equivallent for decimal number 10.

Sunday, April 26, 2009

Combination of Strings

Input : Any number of Strings
Output : Combinations of Strings
for eg: A,B,C(input)
then Output : 
A
B
C
AB
AC
AD
BC
BD
CD
ABC

mathematical formula : nCr =  n ! / (n-r) ! r !

Algorithm with explanations:

1) Enter the number of strings.
          Let say 3. A,B and C.
2) now loop from i=1 to 2^3-1 = 7 (in general (2^n) -1 ).
3) Observe binary patterns for each of the numbers coming in that loop.
     like in the following case.
Number Bianry Pattern
(A) (B) (C)
  1 0 0 1
2 0 1 0
3 0 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1       
4) now put the appropriate string where 1 appears, and put nothing in case of 0.
ie.
1st entry becomes C
2nd entry B
3 BC
4 A
5 AC
6 AB
7 ABC

Thats all  u need.
Thanks