Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Shortest representation in generalised Roman numerals

Jelly, 45 bytes L‘ḃ5MḢ;‘$œṖƲḅ5aị¥⁸N1¦Sṭ “¢¦½2d‘Çȷ6¡iḃ5ị“IVXLC Try it online! A pair of links that takes an integer as its argument and returns a string with the shortest generalised Roman num...

posted 1y ago by NikNakk‭  ·  edited 1y ago by NikNakk‭

Answer
#3: Post edited by user avatar NikNakk‭ · 2023-11-30T09:34:43Z (about 1 year ago)
  • # [Jelly], 45 bytes
  • L‘ḃ5MḢ;‘$œṖƲḅ5aị¥⁸N1¦Sṭ
  • “¢¦½2d‘Çȷ6¡iḃ5ị“IVXLC
  • [Try it online!][TIO-lpkzui3t]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkzui3t]: https://tio.run/##y0rNyan8/9/nUcOMhzuaTX0f7lhkDWSrHJ38cOe0Y5se7mg1TXy4u/vQ0keNO/wMDy0LfrhzLdejhjmHFh1admivUQpQ7eH2E9tNDi3MBOkHKgVKeoZF@Dj////fxAQA "Jelly – Try It Online"
  • A pair of links that takes an integer as its argument and returns a string with the shortest generalised Roman numeral. The TIO link is limited to finding the first 10,000 (rather than million) Roman numerals because it times out otherwise.
  • Full explanation to follow, but this is a more efficient approach than the recursive one below because each substring of the Roman numerals is effectively memoised and used to calculate the longer ones. It also saves a couple of bytes which is nice!
  • ## Previous version [Jelly], 47 bytes
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ?
  • “¢¦½2d‘ṗⱮ6ẎÇ€iị⁸ʋ%35ị“I LXVC
  • [Try it online!][TIO-lpkf59rf]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkf59rf]: https://tio.run/##y0rNyan8/9/34Y5F1o8aZqgcnfxw57Rjmw7Pf9S0xs/w0LLgY5uAUg93dNlzPWqYc2jRoWWH9hqlAFU@3Dn90cZ1Zg939R1uB6rNfLi7@1HjjlPdqsamIGbDHE8Fn4gw5////@saGgAA "Jelly – Try It Online"
  • (only checks Roman numerals up to length 6 since otherwise times out)
  • A full program that takes an integer argument and prints a string. Brute forces the answer by generating all possible Roman numerals with digits IVXLC up to length 8, evaluating them using the described method, and then finding the first which has the correct value.
  • ## Explanation
  • ```
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ? # ‎⁡Helper link: takes a Roman numeral (but made of the integer values of each digit) and returns its value
  • Ʋ Ḋ? # ‎⁢If length >1 then following as a monad:
  • Ʋ # ‎⁣- Following as a monad:
  • Ḣ # ‎⁤ - Head (get first index)
  • M # ‎⁢⁡ - Indices of maximal values
  • ;‘$ # ‎⁢⁢ - Concatenate to index plus 1
  • œṖ # ‎⁢⁣ - Split the original Roman numeral at those two indices
  • ߀ # ‎⁢⁤- Call the helper link for each of the two or three groups generated
  • N1¦ # ‎⁣⁡- Negate first group
  • S # ‎⁣⁢- Sum
  • Ḣ # ‎⁣⁣Else: Head
  • ‎⁣⁤
  • “¢¦½2d‘ṗⱮ8ẎÇ€iị⁸ʋ%35ị“I LXVC # ‎⁤⁡Main link: takes an integer and returns the shortest numeral that evaluates to it
  • “¢¦½2d‘ # ‎⁤⁢Integers 1,5,10,50,100
  • ṗⱮ8 # ‎⁤⁣Cartesian power of these for each of 1 to 8
  • Ẏ # ‎⁤⁤Join outer lists
  • ʋ # ‎⁢⁡⁡Following as a dyad using the list of lists of integers as the left argument and the main link’s argument as the right
  • Ç€ # ‎⁢⁡⁢- Call helper link for each of the list of lists of integers
  • i # ‎⁢⁡⁣- Index of the original main link argument in these (so find the index of the shortest Roman numeral)
  • ị⁸ # ‎⁢⁡⁤- Index into the list of lists of integers
  • %35 # ‎⁢⁢⁡Mod 35
  • ị“I LXVC # ‎⁢⁢⁢Modular index into "I LXVC" (Jelly will terminate the unterminated string at the end of the program)
  • 💎
  • ```
  • Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).
  • # [Jelly], 45 bytes
  • L‘ḃ5MḢ;‘$œṖƲḅ5aị¥⁸N1¦Sṭ
  • “¢¦½2d‘Çȷ6¡iḃ5ị“IVXLC
  • [Try it online!][TIO-lpkzui3t]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkzui3t]: https://tio.run/##y0rNyan8/9/nUcOMhzuaTX0f7lhkDWSrHJ38cOe0Y5se7mg1TXy4u/vQ0keNO/wMDy0LfrhzLdejhjmHFh1admivUQpQ7eH2E9tNDi3MBOkHKgVKeoZF@Dj////fxAQA "Jelly – Try It Online"
  • A pair of links that takes an integer as its argument and returns a string with the shortest generalised Roman numeral. The TIO link is limited to finding the first 10,000 (rather than million) Roman numerals because it times out otherwise.
  • Full explanation to follow, but this is a more efficient approach than the recursive one below because each substring of the Roman numerals is effectively memoised and used to calculate the longer ones. It also saves a couple of bytes which is nice!
  • ## Previous version [Jelly], 47 bytes
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ?
  • “¢¦½2d‘ṗⱮ8ẎÇ€iị⁸ʋ%35ị“I LXVC
  • [Try it online!][TIO-lpkf59rf]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkf59rf]: https://tio.run/##y0rNyan8/9/34Y5F1o8aZqgcnfxw57Rjmw7Pf9S0xs/w0LLgY5uAUg93dNlzPWqYc2jRoWWH9hqlAFU@3Dn90cZ1Zg939R1uB6rNfLi7@1HjjlPdqsamIGbDHE8Fn4gw5////@saGgAA "Jelly – Try It Online"
  • (TIO only checks Roman numerals up to length 6 since otherwise times out)
  • A full program that takes an integer argument and prints a string. Brute forces the answer by generating all possible Roman numerals with digits IVXLC up to length 8, evaluating them using the described method, and then finding the first which has the correct value.
  • ## Explanation
  • ```
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ? # ‎⁡Helper link: takes a Roman numeral (but made of the integer values of each digit) and returns its value
  • Ʋ Ḋ? # ‎⁢If length >1 then following as a monad:
  • Ʋ # ‎⁣- Following as a monad:
  • Ḣ # ‎⁤ - Head (get first index)
  • M # ‎⁢⁡ - Indices of maximal values
  • ;‘$ # ‎⁢⁢ - Concatenate to index plus 1
  • œṖ # ‎⁢⁣ - Split the original Roman numeral at those two indices
  • ߀ # ‎⁢⁤- Call the helper link for each of the two or three groups generated
  • N1¦ # ‎⁣⁡- Negate first group
  • S # ‎⁣⁢- Sum
  • Ḣ # ‎⁣⁣Else: Head
  • ‎⁣⁤
  • “¢¦½2d‘ṗⱮ8ẎÇ€iị⁸ʋ%35ị“I LXVC # ‎⁤⁡Main link: takes an integer and returns the shortest numeral that evaluates to it
  • “¢¦½2d‘ # ‎⁤⁢Integers 1,5,10,50,100
  • ṗⱮ8 # ‎⁤⁣Cartesian power of these for each of 1 to 8
  • Ẏ # ‎⁤⁤Join outer lists
  • ʋ # ‎⁢⁡⁡Following as a dyad using the list of lists of integers as the left argument and the main link’s argument as the right
  • Ç€ # ‎⁢⁡⁢- Call helper link for each of the list of lists of integers
  • i # ‎⁢⁡⁣- Index of the original main link argument in these (so find the index of the shortest Roman numeral)
  • ị⁸ # ‎⁢⁡⁤- Index into the list of lists of integers
  • %35 # ‎⁢⁢⁡Mod 35
  • ị“I LXVC # ‎⁢⁢⁢Modular index into "I LXVC" (Jelly will terminate the unterminated string at the end of the program)
  • 💎
  • ```
  • Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).
#2: Post edited by user avatar NikNakk‭ · 2023-11-30T09:32:31Z (about 1 year ago)
  • # [Jelly], 47 bytes
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ?
  • “¢¦½2d‘ṗⱮ6ẎÇ€iị⁸ʋ%35ị“I LXVC
  • [Try it online!][TIO-lpkf59rf]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkf59rf]: https://tio.run/##y0rNyan8/9/34Y5F1o8aZqgcnfxw57Rjmw7Pf9S0xs/w0LLgY5uAUg93dNlzPWqYc2jRoWWH9hqlAFU@3Dn90cZ1Zg939R1uB6rNfLi7@1HjjlPdqsamIGbDHE8Fn4gw5////@saGgAA "Jelly – Try It Online"
  • (only checks Roman numerals up to length 6 since otherwise times out)
  • A full program that takes an integer argument and prints a string. Brute forces the answer by generating all possible Roman numerals with digits IVXLC up to length 8, evaluating them using the described method, and then finding the first which has the correct value.
  • ## Explanation
  • ```
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ? # ‎⁡Helper link: takes a Roman numeral (but made of the integer values of each digit) and returns its value
  • Ʋ Ḋ? # ‎⁢If length >1 then following as a monad:
  • Ʋ # ‎⁣- Following as a monad:
  • Ḣ # ‎⁤ - Head (get first index)
  • M # ‎⁢⁡ - Indices of maximal values
  • ;‘$ # ‎⁢⁢ - Concatenate to index plus 1
  • œṖ # ‎⁢⁣ - Split the original Roman numeral at those two indices
  • ߀ # ‎⁢⁤- Call the helper link for each of the two or three groups generated
  • N1¦ # ‎⁣⁡- Negate first group
  • S # ‎⁣⁢- Sum
  • Ḣ # ‎⁣⁣Else: Head
  • ‎⁣⁤
  • “¢¦½2d‘ṗⱮ8ẎÇ€iị⁸ʋ%35ị“I LXVC # ‎⁤⁡Main link: takes an integer and returns the shortest numeral that evaluates to it
  • “¢¦½2d‘ # ‎⁤⁢Integers 1,5,10,50,100
  • ṗⱮ8 # ‎⁤⁣Cartesian power of these for each of 1 to 8
  • Ẏ # ‎⁤⁤Join outer lists
  • ʋ # ‎⁢⁡⁡Following as a dyad using the list of lists of integers as the left argument and the main link’s argument as the right
  • Ç€ # ‎⁢⁡⁢- Call helper link for each of the list of lists of integers
  • i # ‎⁢⁡⁣- Index of the original main link argument in these (so find the index of the shortest Roman numeral)
  • ị⁸ # ‎⁢⁡⁤- Index into the list of lists of integers
  • %35 # ‎⁢⁢⁡Mod 35
  • ị“I LXVC # ‎⁢⁢⁢Modular index into "I LXVC" (Jelly will terminate the unterminated string at the end of the program)
  • 💎
  • ```
  • Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).
  • # [Jelly], 45 bytes
  • L‘ḃ5MḢ;‘$œṖƲḅ5aị¥⁸N1¦Sṭ
  • “¢¦½2d‘Çȷ6¡iḃ5ị“IVXLC
  • [Try it online!][TIO-lpkzui3t]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkzui3t]: https://tio.run/##y0rNyan8/9/nUcOMhzuaTX0f7lhkDWSrHJ38cOe0Y5se7mg1TXy4u/vQ0keNO/wMDy0LfrhzLdejhjmHFh1admivUQpQ7eH2E9tNDi3MBOkHKgVKeoZF@Dj////fxAQA "Jelly – Try It Online"
  • A pair of links that takes an integer as its argument and returns a string with the shortest generalised Roman numeral. The TIO link is limited to finding the first 10,000 (rather than million) Roman numerals because it times out otherwise.
  • Full explanation to follow, but this is a more efficient approach than the recursive one below because each substring of the Roman numerals is effectively memoised and used to calculate the longer ones. It also saves a couple of bytes which is nice!
  • ## Previous version [Jelly], 47 bytes
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ?
  • “¢¦½2d‘ṗⱮ6ẎÇ€iị⁸ʋ%35ị“I LXVC
  • [Try it online!][TIO-lpkf59rf]
  • [Jelly]: https://github.com/DennisMitchell/jelly
  • [TIO-lpkf59rf]: https://tio.run/##y0rNyan8/9/34Y5F1o8aZqgcnfxw57Rjmw7Pf9S0xs/w0LLgY5uAUg93dNlzPWqYc2jRoWWH9hqlAFU@3Dn90cZ1Zg939R1uB6rNfLi7@1HjjlPdqsamIGbDHE8Fn4gw5////@saGgAA "Jelly – Try It Online"
  • (only checks Roman numerals up to length 6 since otherwise times out)
  • A full program that takes an integer argument and prints a string. Brute forces the answer by generating all possible Roman numerals with digits IVXLC up to length 8, evaluating them using the described method, and then finding the first which has the correct value.
  • ## Explanation
  • ```
  • MḢ;‘$œṖƲ߀N1¦SƲḢḊ? # ‎⁡Helper link: takes a Roman numeral (but made of the integer values of each digit) and returns its value
  • Ʋ Ḋ? # ‎⁢If length >1 then following as a monad:
  • Ʋ # ‎⁣- Following as a monad:
  • Ḣ # ‎⁤ - Head (get first index)
  • M # ‎⁢⁡ - Indices of maximal values
  • ;‘$ # ‎⁢⁢ - Concatenate to index plus 1
  • œṖ # ‎⁢⁣ - Split the original Roman numeral at those two indices
  • ߀ # ‎⁢⁤- Call the helper link for each of the two or three groups generated
  • N1¦ # ‎⁣⁡- Negate first group
  • S # ‎⁣⁢- Sum
  • Ḣ # ‎⁣⁣Else: Head
  • ‎⁣⁤
  • “¢¦½2d‘ṗⱮ8ẎÇ€iị⁸ʋ%35ị“I LXVC # ‎⁤⁡Main link: takes an integer and returns the shortest numeral that evaluates to it
  • “¢¦½2d‘ # ‎⁤⁢Integers 1,5,10,50,100
  • ṗⱮ8 # ‎⁤⁣Cartesian power of these for each of 1 to 8
  • Ẏ # ‎⁤⁤Join outer lists
  • ʋ # ‎⁢⁡⁡Following as a dyad using the list of lists of integers as the left argument and the main link’s argument as the right
  • Ç€ # ‎⁢⁡⁢- Call helper link for each of the list of lists of integers
  • i # ‎⁢⁡⁣- Index of the original main link argument in these (so find the index of the shortest Roman numeral)
  • ị⁸ # ‎⁢⁡⁤- Index into the list of lists of integers
  • %35 # ‎⁢⁢⁡Mod 35
  • ị“I LXVC # ‎⁢⁢⁢Modular index into "I LXVC" (Jelly will terminate the unterminated string at the end of the program)
  • 💎
  • ```
  • Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).
#1: Initial revision by user avatar NikNakk‭ · 2023-11-30T00:29:07Z (about 1 year ago)
# [Jelly], 47 bytes

    MḢ;‘$œṖƲ߀N1¦SƲḢḊ?
    “¢¦½2d‘ṗⱮ6ẎÇ€iị⁸ʋ%35ị“I LXVC

[Try it online!][TIO-lpkf59rf]

[Jelly]: https://github.com/DennisMitchell/jelly
[TIO-lpkf59rf]: https://tio.run/##y0rNyan8/9/34Y5F1o8aZqgcnfxw57Rjmw7Pf9S0xs/w0LLgY5uAUg93dNlzPWqYc2jRoWWH9hqlAFU@3Dn90cZ1Zg939R1uB6rNfLi7@1HjjlPdqsamIGbDHE8Fn4gw5////@saGgAA "Jelly – Try It Online" 

(only checks Roman numerals up to length 6 since otherwise times out)

A full program that takes an integer argument and prints a string. Brute forces the answer by generating all possible Roman numerals with digits IVXLC up to length 8, evaluating them using the described method, and then finding the first which has the correct value.

## Explanation

```
MḢ;‘$œṖƲ߀N1¦SƲḢḊ?            # ‎⁡Helper link: takes a Roman numeral (but made of the integer values of each digit) and returns its value
              Ʋ Ḋ?            # ‎⁢If length >1 then following as a monad:
       Ʋ                      # ‎⁣- Following as a monad:
 Ḣ                            # ‎⁤  - Head (get first index)
M                             # ‎⁢⁡  - Indices of maximal values
  ;‘$                         # ‎⁢⁢  - Concatenate to index plus 1
     œṖ                       # ‎⁢⁣  - Split the original Roman numeral at those two indices
        ߀                    # ‎⁢⁤- Call the helper link for each of the two or three groups generated
          N1¦                 # ‎⁣⁡- Negate first group
             S                # ‎⁣⁢- Sum
               Ḣ              # ‎⁣⁣Else: Head
‎⁣⁤
“¢¦½2d‘ṗⱮ8ẎÇ€iị⁸ʋ%35ị“I LXVC  # ‎⁤⁡Main link: takes an integer and returns the shortest  numeral that evaluates to it
“¢¦½2d‘                       # ‎⁤⁢Integers 1,5,10,50,100
       ṗⱮ8                    # ‎⁤⁣Cartesian power of these for each of 1 to 8
          Ẏ                   # ‎⁤⁤Join outer lists
                ʋ             # ‎⁢⁡⁡Following as a dyad using the list of lists of integers as the left argument and the main link’s argument as the right
           Ç€                 # ‎⁢⁡⁢- Call helper link for each of the list of lists of integers
             i                # ‎⁢⁡⁣- Index of the original main link argument in these (so find the index of the shortest Roman numeral)
              ị⁸              # ‎⁢⁡⁤- Index into the list of lists of integers
                 %35          # ‎⁢⁢⁡Mod 35
                    ị“I LXVC  # ‎⁢⁢⁢Modular index into "I LXVC" (Jelly will terminate the unterminated string at the end of the program)
💎
```
Created with the help of [Luminespire](https://vyxal.github.io/Luminespire).