Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a number is divisible by 3 [closed]

Write code to determine if a number is divisible by 3. The input to the function is a single bit, 0 or 1, and the output should be 1 if the number received so far is the binary representation of a number divisible by 3, otherwise zero.

Examples:

input  "0":       (0)  output 1
inputs "1,0,0":   (4)  output 0
inputs "1,1,0,0": (6)  output 1

This is based on an interview question. I ask for a drawing of logic gates but since this is stackoverflow I'll accept any coding language. Bonus points for a hardware implementation (verilog etc).

Part a (easy): First input is the MSB.

Part b (a little harder): First input is the LSB.

Part c (difficult): Which one is faster and smaller, (a) or (b)? (Not theoretically in the Big-O sense, but practically faster/smaller.) Now take the slower/bigger one and make it as fast/small as the faster/smaller one.

like image 493
Eyal Avatar asked May 10 '09 07:05

Eyal


People also ask

How do you check if a number is divisible by 3?

A number is divisible by 3 if sum of its digits is divisible by 3. Illustration: For example n = 1332 Sum of digits = 1 + 3 + 3 + 2 = 9 Since sum is divisible by 3, answer is Yes.

How do you check if a number is divisible by 3 in SQL?

The percent sign (%) is used for this. For example: 7%3 == 1 because 7 is divisible by 3 two times, with 1 left over. So to check if a number is divisible by 3, you need to determine if dividing the number by three has a remainder of zero. var number = 21; if( number % 3 == 0) { //The number is divisible by three.

How does the divisibility rule for 3 work?

A number is divisible by 3, or 9, if the sum of its digits is divisible by 3 or 9. For example, 89474 is divisible by 3 if 8+9+4+7+4 = 32 is divisible by 3, (which is divisible by 3 if 3+2=5 is divisible by 3).

How do you check if a number is divisible by 3 in C ++?

Check if a large number is divisible by 3 or not in C++ In this case the number is very large number. So we put the number as string. A number will be divisible by 3, if the sum of digits is divisible by 3.


2 Answers

There's a fairly well-known trick for determining whether a number is a multiple of 11, by alternately adding and subtracting its decimal digits. If the number you get at the end is a multiple of 11, then the number you started out with is also a multiple of 11:

47278    4 - 7 + 2 - 7 + 8 = 0, multiple of 11     (47278 = 11 * 4298)
52214    5 - 2 + 2 - 1 + 4 = 8, not multiple of 11 (52214 = 11 * 4746 + 8)

We can apply the same trick to binary numbers. A binary number is a multiple of 3 if and only if the alternating sum of its bits is also a multiple of 3:

4   = 100       1 - 0 + 0 = 1, not multiple of 3
6   = 110       1 - 1 + 0 = 0, multiple of 3
78  = 1001110   1 - 0 + 0 - 1 + 1 - 1 + 0 = 0, multiple of 3
109 = 1101101   1 - 1 + 0 - 1 + 1 - 0 + 1 = 1, not multiple of 3

It makes no difference whether you start with the MSB or the LSB, so the following Python function works equally well in both cases. It takes an iterator that returns the bits one at a time. multiplier alternates between 1 and 2 instead of 1 and -1 to avoid taking the modulo of a negative number.

def divisibleBy3(iterator):

    multiplier = 1
    accumulator = 0

    for bit in iterator:
        accumulator = (accumulator + bit * multiplier) % 3
        multiplier = 3 - multiplier

    return accumulator == 0
like image 74
Luke Woodward Avatar answered Sep 23 '22 19:09

Luke Woodward


Here... something new... how to check if a binary number of any length (even thousands of digits) is divisible by 3.

divisible-by-3 state machine

-->((0))<---1--->()<---0--->(1)        ASCII representation of graph

From the picture.

  1. You start in the double circle.
  2. When you get a one or a zero, if the digit is inside the circle, then you stay in that circle. However if the digit is on a line, then you travel across the line.
  3. Repeat step two until all digits are comsumed.
  4. If you finally end up in the double circle then the binary number is divisible by 3.

You can also use this for generating numbers divisible by 3. And I wouldn't image it would be hard to convert this into a circuit.

1 example using the graph...

11000000000001011111111111101 is divisible by 3 (ends up in the double circle again)

Try it for yourself.

You can also do similar tricks for performing MOD 10, for when converting binary numbers into base 10 numbers. (10 circles, each doubled circled and represent the values 0 to 9 resulting from the modulo)

EDIT: This is for digits running left to right, it's not hard to modify the finite state machine to accept the reverse language though.

NOTE: In the ASCII representation of the graph () denotes a single circle and (()) denotes a double circle. In finite state machines these are called states, and the double circle is the accept state (the state that means its eventually divisible by 3)

like image 25
clinux Avatar answered Sep 19 '22 19:09

clinux