How to create a numerical system?

2

Situation

Recently I received a challenge in college to do:

  

From the decimal system 0123456789 , using only the numbers that are still readable, find the thousandth number of this system.

     

Example 8 will be the 5th number, the 25th, or the 17th.

Development

Accepted numbers: 0125689

0   1   2   5   6   8   9
0   1   2   3   4   5   6

10  11  12  15  16  18  19
7   8   9   10  11  12  13

20  21  22  25  26  28  29
14  15  16  17  18  19  20

My proposal

Create a base numeric system 7 with these "characters".

Problem

It does not follow the same pattern of conversion of bases , it would be more for Roman numerals tato more complex). When adding 1+2 the result is 5 , 9+5 = 12

Question

How to manage this system computationally, let's say I want to add 25 + 18. Or any other example.

Obs

I've extrapolated the challenge a bit, which would just do with a loop removing the not allowed numbers, but I think it's a good study.

Searches

Non-Decimal Numbering Systems
#

    

asked by anonymous 02.03.2018 / 15:25

1 answer

2

Looking back, I checked that my idea really is about base 7, with only one conversion at the end.

Analogy

If I need to create a base with 0125689 it would be the same as creating a base with abcdefg . With that in mind I noticed that the question is not with characters involved, but rather how many.

Solution

In this way the basic conversion logic remains.

What'sdoneafterthisisjustanormalcharacterconversion.InthesamewaythatIwouldconvert:

a=>0,b=>1,c=>2,d=>5,e=>6,f=>8,g=>9

Real:

0=>0,1=>1,2=>2,3=>5,4=>6,5=>8,6=>9

Code

var tot = 1000;
var base = 7;
var rest = [];

console.log('Numero',tot,'(10)')

while(tot>base){
	rest.push(tot%base);
	tot = Math.floor(tot/base);
}
var result = tot+rest.reverse().join('')+"";
console.log('normal', result, '(7)');

var convert = {	3 : 5, 4 : 6, 5 : 8, 6 : 9 }
for(var i in convert){
	var r = convert[i];
	result = result.replace(new RegExp(i, 'g'),r);
}
console.log('result', result, '(7) => convertido');

Auxiliary reading

Arithmetic in Non-Decimal Bases

    
16.03.2018 / 18:44