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
source
share