Divisiblity of 5 without using % and / operator

后端 未结 9 2338
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 02:14

how to check whether a number is divisible by 5 or not without using % and / operator. I want a quickest algorithm for this problem.

9条回答
  •  忘掉有多难
    2020-12-02 02:54

    Let's represent the number in base 2. We have:

    abcdefgh*101 = ABCDEFGHIJ
    

    or

    +abcdefgh00
    +  abcdefgh
     ----------
     ABCDEFGHIJ
    

    We are given ABCDEFGHIJ and want to find abcdefgh.

    If you alternately - and + ABCDEFGH with its successive rightshift-by-2, you will get...

    +  ABCDEFGH
    -    ABCDEF
    +      ABCD
    -        AB
    -----------
    +  abcdefgh
    +    abcdef
    -    abcdef
    -      abcd
    +      abcd
    +        ab
    -        ab
    -----------
       abcdefgh
    

    The answer!

提交回复
热议问题