## 8.6 Exercises

### 8.6.1 Converting decimal numbers into base N

1. Create a conversion function dec2base(x, base) that converts a decimal number x into a positional number of different base (with $$2 \leq$$ base $$\leq 10$$). Thus, the dec2base() function provides a complement to base2dec() from the i2ds package:
library(i2ds)
base2dec(11, base = 2)
#> [1] 3
dec2base(3,  base = 2)
#> [1] "11"
1. Use your dec2base() function to compute the following conversions:
dec2base(100, base =  2)
dec2base(100, base =  3)
dec2base(100, base =  5)
dec2base(100, base =  9)
dec2base(100, base = 10)
1. Create a brief simulation that samples $$N = 20$$ random decimal numbers $$x_i$$ and base values $$b_i$$ $$(2 \leq b_i <= 10)$$ and shows that

base2dec(dec2base($$x_i,\ b_i$$),$$b_i$$) ==$$x_i$$

(i.e., converting a numeric value from decimal notation into a number in base $$b_i$$ notation, and back into decimal notation yields the original numeric value).

#### Solution

Table 8.1: Convert integer values from decimal to base notation, and back to decimal notation.
n_org base n_base n_dec same
7179 10 7179 7179 TRUE
7548 8 16574 7548 TRUE
6252 10 6252 6252 TRUE
3759 6 25223 3759 TRUE
6676 8 15024 6676 TRUE
7605 2 1110110110101 7605 TRUE
3774 2 111010111110 3774 TRUE
4546 5 121141 4546 TRUE
542 6 2302 542 TRUE
9556 9 14087 9556 TRUE
8973 8 21415 8973 TRUE
3390 10 3390 3390 TRUE
5426 6 41042 5426 TRUE
4473 4 1011321 4473 TRUE
995 4 33203 995 TRUE
3116 10 3116 3116 TRUE
1681 8 3221 1681 TRUE
7150 4 1233232 7150 TRUE
890 7 2411 890 TRUE
2248 3 10002021 2248 TRUE
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}