Skip to main content
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.


n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$:

[...Array(m = k = 7 ** n)].reduce(s => … (--k + m).toString(7) …, 0)
  1. We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$:

    [...Array(m = k = 7 ** n)].reduce(s => … (--k + m).toString(7) …, 0)

EachFrom now on, each digit will be interpreted as a Roman numeral symbol according to the following table:

0: I    4: C
1: M    5: D
2: X    6: V
3: L

We replace all valid subtractive pairs of the form AB with B:$$\begin{array}{}0\longleftrightarrow \text{I}, & 1\longleftrightarrow \text{M}, & 2\longleftrightarrow \text{X}, & 3\longleftrightarrow \text{L},\\ 4\longleftrightarrow \text{C}, & 5\longleftrightarrow \text{D}, & 6\longleftrightarrow \text{V} \end{array}$$

  1. We replace all valid subtractive pairs of the form AB with B:
.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC remains unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

  1. We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:
/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test(…)  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(…)  // with Roman symbols

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.


n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$:

[...Array(m = k = 7 ** n)].reduce(s => … (--k + m).toString(7) …, 0)

Each digit will be interpreted as a Roman numeral symbol according to the following table:

0: I    4: C
1: M    5: D
2: X    6: V
3: L

We replace all valid subtractive pairs of the form AB with B:

.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC remains unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test(…)  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(…)  // with Roman symbols

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.


n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

  1. We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$:

    [...Array(m = k = 7 ** n)].reduce(s => … (--k + m).toString(7) …, 0)

From now on, each digit will be interpreted as a Roman numeral symbol:

$$\begin{array}{}0\longleftrightarrow \text{I}, & 1\longleftrightarrow \text{M}, & 2\longleftrightarrow \text{X}, & 3\longleftrightarrow \text{L},\\ 4\longleftrightarrow \text{C}, & 5\longleftrightarrow \text{D}, & 6\longleftrightarrow \text{V} \end{array}$$

  1. We replace all valid subtractive pairs of the form AB with B:
.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC remains unchanged, which also leaves an invalid I in place
  1. We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:
/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test(…)  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(…)  // with Roman symbols
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.


n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$ and interpret each:

[...Array(m = k = 7 ** n)].reduce(s => … (--k + m).toString(7) …, 0)

Each digit will be interpreted as a Roman numeral symbol according to the following table:

0: I    4: C
1: M    5: D
2: X    6: V
3: L

We replace all valid subtractive pairs of the form AB with B:

s.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
s.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC is leftremains unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test(...)  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(...)  // with Roman symbols

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.


n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$ and interpret each digit as a Roman numeral symbol according to the following table:

0: I    4: C
1: M    5: D
2: X    6: V
3: L

We replace all valid subtractive pairs of the form AB with B:

s.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
s.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC is left unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test(...)  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(...)  // with Roman symbols

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.


n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$:

[...Array(m = k = 7 ** n)].reduce(s => … (--k + m).toString(7) …, 0)

Each digit will be interpreted as a Roman numeral symbol according to the following table:

0: I    4: C
1: M    5: D
2: X    6: V
3: L

We replace all valid subtractive pairs of the form AB with B:

.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC remains unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test()  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test()  // with Roman symbols
saved 1 byte
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES7), 134133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.

 
n=>[...Array(m=k=7**n)].reduce(s=>s+/^.6*5^1*5?4{0,3}3?2{0,3}16?0{0,3}$/.test((--k+m).toString(7).replace(/0[12]|2[34]|4[56]0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$ and interpret each digit as a Roman numeral symbol according to the following table:

0: I    4: C
1: VM    5: D
2: X    6: MV
3: L

We replace all valid subtractive pairs of the form AB with B:

s.replace(/0[12]|2[34]|4[56]0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
s.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC is left unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^.6*5^1*5?4{0,3}3?2{0,3}16?0{0,3}$/.test(...)  // in the code
/^.M*D^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(...)  // with Roman symbols

#JavaScript (ES7), 134 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.

n=>[...Array(m=k=7**n)].reduce(s=>s+/^.6*5?4{0,3}3?2{0,3}1?0{0,3}$/.test((--k+m).toString(7).replace(/0[12]|2[34]|4[56]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$ and interpret each digit as a Roman numeral symbol according to the following table:

0: I    4: C
1: V    5: D
2: X    6: M
3: L

We replace all valid subtractive pairs of the form AB with B:

s.replace(/0[12]|2[34]|4[56]/g, s => s[1]))  // in the code
s.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^.6*5?4{0,3}3?2{0,3}1?0{0,3}$/.test(...)  // in the code
/^.M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(...)  // with Roman symbols

#JavaScript (ES7), 133 bytes

Edit: Fixed to match the results returned by Jonathan Allan's code, which was given as a reference implementation by the OP.

 
n=>[...Array(m=k=7**n)].reduce(s=>s+/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test((--k+m).toString(7).replace(/0[62]|2[34]|4[51]/g,s=>s[1])),0)

Try it online!

###How?

We generate all numbers of \$N\$ digits in base 7 with an extra leading \$1\$ and interpret each digit as a Roman numeral symbol according to the following table:

0: I    4: C
1: M    5: D
2: X    6: V
3: L

We replace all valid subtractive pairs of the form AB with B:

s.replace(/0[62]|2[34]|4[51]/g, s => s[1]))  // in the code
s.replace(/I[VX]|X[LC]|C[DM]/g, s => s[1]))  // with Roman symbols

Examples:

  • XLIXIV becomes LXV
  • XIIV becomes XIV, leaving a I that will make the next test fail
  • IC is left unchanged, which also leaves an invalid I in place

We check that the remaining symbols are in the correct order and do not appear more times than they're allowed to:

/^1*5?4{0,3}3?2{0,3}6?0{0,3}$/.test(...)  // in the code
/^M*D?C{0,3}L?X{0,3}V?I{0,3}$/.test(...)  // with Roman symbols
minor update
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Post Undeleted by Arnauld
fixed version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Post Deleted by Arnauld
added an explanation
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
added a non-recursive version
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
deleted 49 characters in body
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading