The Myriad System is an alternative system for naming large numbers proposed by Donald Knuth in 1981.
In this system, numbers up to 999 are named as they are normally, but 1000 is not given a special name and is instead named as "ten hundred"; the next benchmark number being 10000, or "one myriad" (which, of course, comes from an ancient greek word meaning 10000).
From here one myllion is defined as one myriad * one myriad, one byllion as one myllion * one myllion, one tryllion as one byllion * one byllion, and so forth.
The challenge here is to take a base-10 integer from the input and determine its name in this system.
Your input is guaranteed to be nonnegative and less than 10212=104096 (one decyllion)
Either a full program (in which case input can be taken from either stdin or a command line argument, and output must be sent to stdout) or a function (which takes the input as an argument and returns the output) is permissible.
The returned string should be formatted as per this Java program:
import java.math.*;
public class Myriadizer{
static BigInteger b=new java.util.Scanner(System.in).nextBigInteger(),c=b,TEN=BigInteger.TEN,d=TEN.pow(4),f=TEN.pow(2),e=BigInteger.ZERO;
static String[]strings={"six","seven","eight","nine","four","thir","fif"};
static String output="";
static void t(){
int i=0;
while(b.compareTo(d)>=0){
b=b.divide(d);
i++;
}
int k=b.intValue();
char[]binary=Integer.toString(i,2).toCharArray();
int q=48,bits=binary.length;
String r="yllion ";
s(k);
if(bits>0&&binary[bits-1]!=q)output+="myriad ";
if(bits>1&&binary[bits-2]!=q)output+="m"+r;
if(bits>2&&binary[bits-3]!=q)output+="b"+r;
if(bits>3&&binary[bits-4]!=q)output+="tr"+r;
if(bits>4&&binary[bits-5]!=q)output+="quadr"+r;
if(bits>5&&binary[bits-6]!=q)output+="quint"+r;
if(bits>6&&binary[bits-7]!=q)output+="sext"+r;
if(bits>7&&binary[bits-8]!=q)output+="sept"+r;
if(bits>8&&binary[bits-9]!=q)output+="oct"+r;
if(bits>9&&binary[bits-10]!=q)output+="non"+r;
if(bits>10&&binary[bits-11]!=q)output+="dec"+r;
b=c=c.subtract(d.pow(i).multiply(b));
if(b.compareTo(f)>=0)output+=",";
output+=" ";
if(b.compareTo(e)>0&&b.compareTo(f)<0)output+="and ";
}
static void s(int t){
if(t>99){
s(t/100);
output+="hundred ";
if(t%100>0)output+="and ";
s(t%100);
return ;
}
if(t==0)return;
if(t<13){
output+=(new String[]{
"one","two","three",
strings[4],"five",strings[0],strings[1],strings[2],strings[3],
"ten","eleven","twelve"
})[t-1]+" ";
return ;
}
if(t<20){
output+=(new String[]{
strings[5],strings[4],strings[6],strings[0],strings[1],strings[2],strings[3]
})[t-13]+"teen ";
return ;
}
output+=new String[]{"twen",strings[5],"for",strings[6],strings[0],strings[1],strings[2],strings[3]}[t/10-2]+"ty";
if(t%10>0){
output+="-";
s(t%10);
return;
}
output+=" ";
}
public static void main(String[]a){if(b.equals(e)){System.out.println("zero");return;}while(c.compareTo(e)>0)t();System.out.println(output.replaceAll("\\s+$","").replaceAll("\\s\\s+"," ").replaceAll("\\s,",","));}
}
Some details of this that should be noted:
- The terms in the main sum should be arranged in descending order
- The terms in any given product should be arranged in ascending order, and exactly one term in any given product should be under one myriad
- The terms in the main sum should be comma-separated, unless the last is under one hundred in which case the last two should be separated by the conjunction "and"
This is code golf, so the shortest program wins.
Some test cases:
0 = zero
100 = one hundred
1000 = ten hundred
10014 = one myriad and fourteen
19300 = one myriad, ninety-three hundred
100000 = ten myriad
1000000 = one hundred myriad
1041115 = one hundred and four myriad, eleven hundred and fifteen
100000000 = one myllion
1000000501801 = one myriad myllion, fifty myriad, eighteen hundred and one
94400000123012 = ninety-four myriad myllion, forty hundred myllion, twelve myriad, thirty hundred and twelve
10000000200000003000000040000000 = ten hundred myriad myllion byllion, twenty hundred myriad byllion, thirty hundred myriad myllion, forty hundred myriad
100000002000000030000000400000000 = one tryllion, two myllion byllion, three byllion, four myllion
and
s have to be used? The spec should be in English, not Java. This has always been a rule here, reference implementations do not constitute a spec. Please clarify in the question. \$\endgroup\$