Skip to content

Instantly share code, notes, and snippets.

@ashutosh049
Created March 9, 2018 11:08
Show Gist options
  • Save ashutosh049/3c94fd8d3d24e4c64c65745078731b0a to your computer and use it in GitHub Desktop.
Save ashutosh049/3c94fd8d3d24e4c64c65745078731b0a to your computer and use it in GitHub Desktop.
public class RomanNumeralsGenerator {
enum Numeral {
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
int weight;
Numeral(int weight) {
this.weight = weight;
}
};
public String toRoman(long n) {
if( n <= 0) {
throw new IllegalArgumentException();
}
StringBuilder buf = new StringBuilder();
final Numeral[] values = Numeral.values();
for (int i = values.length - 1; i >= 0; i--) {
while (n >= values[i].weight) {
buf.append(values[i]);
n -= values[i].weight;
}
}
return buf.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment