Is the logic for checking the number divided by 3 or not?

without using%, / or *, I have to find no. divisible by 3 or not?

it may be an interview question.

Thank.

+3
source share
10 answers

Subtract 3 until you

hit 0 - the number was divided by 3 (or)

get a number less than 0 - the number is not divisible

 if (number > 0)
 {
        while (number > 0)
        {
            number -= 3;
    }
}
else if( number < 0)
{
    while number < 0:
        number += 3
}
return number == 0
+10
source

There are different ways. The simplest is pretty obvious:

int isdivby3(int n) {
    if (n < 0) n = -n;

    while (n > 0) n -= 3;

    return n == 0;
}

But we can improve it. Any number can be represented as follows: ("," means the range inclusive):

Base2 (AKA binary)
(0,1) + 2*(0,1) + 4*(0,1)

Base4
(0,3) + 4*(0,3) + 16*(0,3)

BaseN
(0,N-1) + N*(0,N-1) + N*N*(0,N-1)

Now the trick, the number xis divisible by n-1if and only if the number xin the database nis divisible by n-1. This trick is well known for 9:

1926 = 6 + 2*10 + 9*100 + 1*1000
6+2+9+1 = 8 + 1*10
8+1 = 9 thus 1926 is divisible by 9

3 base4. , 4 - 2, . number(base).

27(10) = 123(4)
Digitsum
12(4)
Digitsum again
3(4) = Divisible!

C:

int div3(int n) {
    if (n < 0) n = -n;
    else if (n == 0) return 1;

    while (n > 3) {
        int d = 0;

        while (n > 0) {
            d += n & 3;
            n >>= 2;
        }

        n = d;
    }

    return n == 3;
}

.

+10

. (, , , .)

sprintf, , . . 3, 6 9, 3. - 10, . - 9, .

, 813478902, , , 42, , 6, 3.

+6

for, 3 , , 0. , 0, , 3

+1

, 3 .

:

00: 00(00)
01: 0001
02: 0010
03: 00(11)
04: 0100
05: 0101
06: 01(10)
07: 0111
08: 1000
09: 10(01)
10: 1010
11: 1011
12: 11(00)
13: 1101
14: 1110
15: 11(11)
16: 10000
17: 10001
18: 100(10)
19: 10011
20: 10100
21: 101(01)

, , ( ), {00, 11, 10, 01}. , .

mask = 00 , 00. , (mask + 03) & 0x03, . 11. ((count & 3) == mask)

#include <stdio.h>

int main (void)
{
  int i=0;
  unsigned int mask = 0x00;

  for (i=0; i<100;i++)
  {
    if ((i&0x03) == mask)
    {
      printf ("\n%d", i);
      mask = (mask + 3) & 0x03;
    }
  }
  printf ("\n");
  return 0;
}

. , @nightcracker

, . , - . .

UMass

, ,

+1
number = abs(number)

while (number > 0)
{
   number -= 3;
}

return number == 0
0

, n - , .

n 0, 3; n = (2 ^ p) * (2 * n1 + 1) n 3, 2 * n1 + 1 , ak >= 0 2 * n1 + 1 = 3 * (2 * k +1) iff n1 = 3 * k + 1 iff n1 = 1 n1 > 1 n1-1 3. :

int ism3( int n)
{   for(;n;)
    {    while( !(n & 1)) n >>= 1;
         n >>= 1;
         if ( n == 0) return 0;
         n-= 1;
    }
    return 1;
 }
0

, 3, , 3. 3, 3. , 54467565687 3, 5 + 4 + 4 + 6 + 7 + 5 + 6 + 5 + 6 + 8 + 7 = 63 63 3. , , , , 3, 3 , 3. 0, 3 ( ), 3 ( ). , 3 (, ) . Um abraço todos.

0
source

A number is divided by three if its binary variable digit is zero:

bool by3(int n) {
  int s=0;
  for (int q=1; n; s+=q*(n&1), n>>=1, q*=-1);
  return !s;
}
0
source

You can use user reviews:

int isDivisibleBy3(int n)
{
   int areDivisibleBy3[] = {};
   for(int i = 0; i < 0; i++)
   {
       if(n == areDivisibleBy3[i])
       {
           return 1;
       }
   }

   return 0;
}

When a user reports an error indicating that a number divisible by 3 does not give the correct result, you simply add that number to the array and increase the number icompared to the for loop condition.

This is great because you never have to worry about numbers that the user never uses.

Remember to add unit test for whenever a user reports an error!

0
source

All Articles