Open Source Technical Information: Given a number n , check its divisibility by 63 without using % and / operators in the most efficient way..............?

Friday, 21 October 2011

Given a number n , check its divisibility by 63 without using % and / operators in the most efficient way..............?

,
Here is the Solution For this:- 

n=num>>6;
if(num ^ (((n+1)<<6)-(n+1)))
printf("\nnot divsibile");
else
printf("\ndivisible"
 );

Explanation:-
see just a simple logics is here
let the no is n = 509

what we have to do n/63 = 509 / 63

we can write n = 63*q

=> q = n/63; where (q -> quotient)

=> q = n/(64-1) --------------(1)

now
since, n/63 > n/64

so we can write above equ.

n/63 = (n/64) + 1;

= (n/2^6) + 1;

(WE KNOW THAT IF WE DO LEFTT SHIFT A NUM BY 1 THEN WE GET THE RESULT WHICH IS SAME AS DIVIDE BY 2 SO IF WE WANT THE RESULT OF DIVIDING A NUM BY 64 WE HAVE TO LEFT-SHIFT THE NO 6 TIMES)


Q = (n>>6) + 1; --------------------(2)( 509 >> 6 = 0000 0001 1111 1101>>6 = 0000 0000 0000 0111 = 7) = 7+1 = 8

whatever the result we get is our quotient(Q);

so no we have

1) the given no n;
2) and the quotient Q;
3) remainder 0;

Now in respect to find the new no we have a simple formulae

DIVEDENT = DIVISOR * QUOTIENT + REMAINDER = 63*Q + 0; = (64-1)*Q ; = 64*Q - Q; = 2^6*Q -Q;
(WE KNOW THAT IF WE DO RIGHT SHIFT A NUM BY 1 THEN WE GET THE RESULT WHICH IS SAME AS MULTIPLY BY 2 SO IF WE GET THE RESULT OF MULTIPLYING A NUM BY 64 WE HAVE TO RIGHT-SHIFT THE NO 6 TIMES) so
= Q<<6 - Q; (8 << 6 - 8 = 0000 0000 0000 1000 << 6 - 8 = 0000 0010 0000 0000 - 8 = 512 - 8 = 504 )

now for cheacking if XOR of both the num is 0 then it print divisible else no divisible..so

num1 ^ num2 = 509 ^ 504 = 0000 0001 1111 1101 ^ 0000 0010 0000 0000 = 0000 0011 1111 1101 = 1024 which is true value so it execute the if part
ie not divisible

otherwise it produce 0 and it's false so else part will be display.........that's it.

2 comments to “Given a number n , check its divisibility by 63 without using % and / operators in the most efficient way..............?”

  • 21 December 2011 at 06:32
    Kapil Gupta says:

    its a gud try....
    its working with 63*i ( 0 < i < 65)
    if u will go out of this range of i.. it always give "not Divisible"

    I think it should be work for i >= 65 and i < 0

  • 21 December 2011 at 22:30
    Unknown says:

    Nice ....
    I'll try to sort out this problem ....thank you...

Post a Comment

Write your tips here...

Deal of the Day

Advertisement here

Advertisement here