3
\$\begingroup\$

Given an input, calculate the correct suffix and output the number in a readable format in short scale (where 1 billion is 10^9). The suffixes must go to at least 10^3000, in which the rules for calculating them can be found here, or a list can be found here.

There is no need to output the exact numbers, this is focused on getting a huge number into a readable format.

For example:

10000 = 10.0 thousand
135933445 = 135.93 million
-2 = -2.0
-2.36734603 = -2.37
'1'+'9'*3000 = 2.0 nongennovemnonagintillion

 // out of range examples
'1'+'9'*3010 = 20000000000.0 nongennovemnonagintillion
'-1'+'9'*5000 = -inf nongennovemnonagintillion

I am aware it is similar to this question, though with 10x as many numbers and not having to read the names, there's potentially a lot more room for golfing. For the record, my result is 578 characters in Python.

Rules:

  • No getting things from external resources - it must all be calculated within the code.
  • External modules are fine, just don't break the above rule.
  • The code should work when input is either a string, integer or float (eg. you may wish to input the super large numbers as strings).
  • The output must always contain a decimal place.
  • The output must be rounded if above 2 decimal places. Using the inbuilt round function is fine, I am aware there are some floating point errors, so if the occasional '.045' doesn't round up to '.05' for example, don't worry about it.
  • Leaving multiple zeroes at the end is optional, as long as it doesn't go above 2 decimals (1.00 or 1.0 are both fine) and is consistent for all inputs (an input of 1 and 1.0 should output the same result).
  • An input too large shouldn't cause an error, inf is a valid value since it's part of float.

Scoring:

  • Score is the length of the code, including indents.
  • Lowest score wins.
  • Output can be either printed or returned.
  • Setting the input number does not count towards length.

As a starting point, here is an ungolfed version of some code in Python to generate the list of suffixes. Feel free to build upon this or start from scratch.

a = ['', 'un','duo','tre','quattor','quin','sex','septen','octo','novem']
c = ['tillion', 'decillion', 'vigintillion', 'trigintillion', 'quadragintillion', 'quinquagintillion', 'sexagintillion', 'septuagintillion', 'octogintillion', 'nonagintillion']
d = ['', 'cen', 'duocen', 'trecen', 'quadringen', 'quingen', 'sescen', 'septingen', 'octingen', 'nongen']

num_dict = ['']
num_dict.append('thousand')
num_dict.append('million')
num_dict.append('billion')
num_dict.append('trillion')
num_dict.append('quadrillion')
num_dict.append('quintillion')
num_dict.append('sextillion')
num_dict.append('septillion')
num_dict.append('octillion')
num_dict.append('nonillion')

for prefix_hundreds in d:

    #tillion can't be used first time round
    if not prefix_hundreds:
        b = c[1:]
    else:
        b = c

    for prefix_tens in b:
        for prefix in a:
            num_dict.append(prefix_hundreds+prefix+prefix_tens)
\$\endgroup\$
1
\$\begingroup\$

To get the ball rolling then, this is my version. Only the final number is converted to a float so there is no problem with large numbers if they're within range.

In terms of speed, it takes about 0.85 seconds to calculate 1000 random integers between -1e3000 and 1e3000.

Python 2 (574)

N=str(X).split('.');K=len;E=min(1000,(K(str(abs(int(N[0]))))-1)/3);P=N[0][:(-E*3if E else K(N[0]))];L=K(P);B=''.join(N);O='%.2f'%float((P+'.'+((B[L:L+3])or'0')));m,n,o,p,q,r,s,t,u,v,w,x,y,z='illion quadr quin sex sept oct non ing ag g tre duo c t'.split();a=[''];l=a+['thousand']+[i+m for i in['m','b','tr',n,o+z,p+z,q,r,s]]+[i+j+k for i in a+[i+'en'for i in[y,x+y,w+y,n+t,o+v,'sesc',q+t,r+t,s+v]]for k in[C+m for C in[z,'dec']+[B+'int'for B in['vig','trig',n+u,o+'quag',p+u,q+'uag',r+'og',s+u]]][not i:]for j in a+['un',x,w,'quattor',o,p,q+'en',r+'o','novem']];print O,l[E]

Then with some test cases:

>>> X = 100
100.0
>>> X = '100.0'
100.0
>>> X = '1037093920'
1.04 billion
>>> X = '-1540420004036040042.400642'
-1.54 quintillion
>>> X = '1540420' + '0'*153
1.54 duoquinquagintillion
>>> X = '1540496' + '0'*1553
154.05 quingenoctodecillion

Past the limit (just to show it doesn't throw an error and still handles negative value):
>>> X = '-10935' + '6'*3005
-1093566666.67 nongennovemnonagintillion
>>> X = '10935' + '6'*3250
1.09356666667e+254 nongennovemnonagintillion
>>> X = '10935' + '6'*3500
inf nongennovemnonagintillion
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.