How to check the divisibility of a number not in base 10 without conversion?

Say I have a base number 3, 1211. How can I verify that this number is divisible by 2 without converting it back to base 10?

Update
Original Problem - From TopCoder
Signs 3 and 9 have an interesting property. If you take several multiples of 3 and summarize your numbers, you get several more multiples of 3. For example, 118 * 3 = 354 and 3 + 5 + 4 = 12, which is a multiple of 3. In the same way, if you take several of 9 and sum it digits, you get another multiple of 9. For example, 75 * 9 = 675 and 6 + 7 + 5 = 18, which is a multiple of 9. Name any digit for which this property is of interest, except for 0 and 1, for which the property is true trivially. A number that is interesting in one database is not necessarily interesting in another. For example, 3 is interesting in base 10, but uninteresting in base 5. Given the int base, your task is to return all the interesting numbers for this base in ascending order. To determine if a digit is interesting or not, you do not need to consider all multiple digits. You can be sure,that if the property is satisfied for all multiple digits with less than four characters, then it also holds for multiple numbers with a large number of digits. For example, in base 10 you do not need to consider any multiples greater than 999.
Notes
- When the base is greater than 10, the numbers may have a numerical value greater than 9. Since integers are displayed by default in base 10, do not be alarmed when such numbers appear on your screen as more than one decimal digit. For example, one of the interesting numbers in base 16 is 15.
Limitations
- the base is 3 to 30 inclusive.

This is my decision:

class InterestingDigits {
public:
    vector<int> digits( int base ) {
        vector<int> temp;
        for( int i = 2; i <= base; ++i )
            if( base % i == 1 )
                temp.push_back( i );
        return temp;
    }
};

The trick was well explained here: https://math.stackexchange.com/questions/17242/how-does-base-of-a-number-relate-to-modulos-of-its-each-individual-digit

Thanks
Chan

+3
source share
5 answers

If your number k is in base three, you can write it as

k = a0 3^n + a1 3^{n-1} + a2 3^{n-2} + ... + an 3^0

where a0, a1, ..., an are the numbers in the base-three representation.

, , , , 2, . , k mod 2

k mod 2 = (a0 3^n + a1 3^{n-1} + a2 3^{n-2} + ... + an 3^0) mod 2
        = (a0 3^n) mod 2 + (a1 3^{n-1}) mod 2 + ... + an (3^0) mod 2
        = (a0 mod 2) (3^n mod 2) + ... + (an mod 2) (3^0 mod 2)

, 3 ^ = 1 (mod 2),

k mod 2 = (a0 mod 2) + (a1 mod 2) + ... + (an mod 2)

, , , . , 0, 1 2, , 1s !

, m, m - 1, m. , 10 9, , .

+8

:

n b

n = (n * b) + d

d.

, , m :

n = ((n * b) + d) % m

n m . , d . , 0.

n == 0, d == 0: n = ((0 * 3) + 0) % 2 = 0
n == 0, d == 1: n = ((0 * 3) + 1) % 2 = 1
n == 0, d == 2: n = ((0 * 3) + 2) % 2 = 0
n == 1, d == 0: n = ((1 * 3) + 0) % 2 = 1
n == 1, d == 1: n = ((1 * 3) + 1) % 2 = 0
n == 1, d == 2: n = ((1 * 3) + 2) % 2 = 1

, 1 2 0 2.

+3

, -3, - /.

if (n % 2 == 0) {
    // divisible by 2, so even
} else {
    // odd
}

, 3. , , , .

    0 2 2 0
    _______
2 ⟌ 1 2 1 1 
    0
    ---
    1 2
    1 1
    -----
      1 1
      1 1
      -----
        0 1 <--- remainder = 1 (so odd)

( , "" base-3, )

+1

( ) - , ; , nmber .

? 0, 1 2 (1, 3, 9, 27,...). A 0 , / () . A 1 3, ). 0 (). , , , .

+1

, 10, : 1. 2, <= 1211, 1210 (. , ) 2. Substract 1210 1211, 1 3. 1 < 10, 1211 2

how to reach 1210: 1. starts with 2 2. 2 + 2 = 11 3. 11 + 2 = 20 4. 20 + 2 = 22 5. 22 + 2 = 101 6. 101 + 2 = 110 7. 110 + 2 = 112 8. 112 + 2 = 121 9. 121 + 2 = 200 10. 200 + 2 = 202 ... // repeat until you get the greatest number <= 1211 it is basically the same as base 10, and only rounding occurs at 3 instead of 10.

+1
source

Source: https://habr.com/ru/post/1787102/


All Articles