r/dailyprogrammer • u/oskar_s • Jul 30 '12
[7/30/2012] Challenge #83 [easy] (Long scale and short scale)
One of the most annoying and confusing differences between English and basically every other language in the world is that English uses a weird way to name very large numbers.
For instance, if you wanted to translate "one trillion" from English to French, you might think it would be "un trillion", but that is wrong. The correct translation of "one trillion" to French is "un billion". Well, then, you might ask, what is "one billion" in French? It is, in fact, "un milliard". And "un trillion" in French is equal to english "one quintillion". Confusing, no?
The reason for this is that there are two so-called scales for large numbers, the long scale and the short scale. English uses the short scale, almost everyone else uses the long scale (though the Arabic languages also apparently use the short scale). The two systems can be summarized as follows:
- In the short scale, you get a "new word" for the numbers every time the number increases by a factor of 1,000. So "a thousand millions" is "one billion" and "a thousand billions" is "one trillion".
- In the long scale, you get a "new word" for the numbers every time the number increases by a factor of 1,000,000. So "a million millions" is the same as "one billion" and "a million billions" is the same as "one trillion". If we just increase by a factor of 1,000, the "-on" ending on the word is replaced by "-ard". So "a thousand millions" is "one milliard", "a thousand billions" is "one billiard".
Here's a table summarizing the words for different numbers:
| Actual number | Short scale name | Long scale name |
|---|---|---|
| 106 | million | million |
| 109 | billion | milliard |
| 1012 | trillion | billion |
| 1015 | quadrillion | billiard |
| 1018 | quintillion | trillion |
| 1021 | sextillion | trilliard |
And it goes on like that.
Your task today is to write a program that will print out the name of a number in both long-scale and short-scale. So, for instance, given 1234567891111, it should print out
Short scale: 1 trillion, 234 billion, 567 million, 891 thousand and 111
Long scale: 1 billion, 234 milliard, 567 million, 891 thousand and 111Bonus points if it prints out everything in letters, i.e.:
Short scale: one trillion, two hundred and thirty-four billion, five hundred
and sixty-seven million, eight hundred and ninety-one thousand and one
hundred and eleven
Long scale: one billion, two hundred and thirty-four milliard, five hundred
and sixty-seven million, eight hundred and ninety-one thousand and one
hundred and elevenThe program should be able to handle all numbers that can fit into an unsigned 64-bit integers, i.e. all numbers up to 264 - 1 ("18 trillion, 446 billiard, 744 billion, 73 milliard, 709 million, 551 thousand and 615" in long scale, though it's something completely different in short scale.), or 263 - 1 if you're using signed 64-bit integers. Of course, you can write your program so it can handle bigger numbers if you want, but it's not necessary.
5
u/[deleted] Jul 30 '12 edited Jul 31 '12
J
2
u/lawlrng 0 1 Jul 30 '12 edited Jul 30 '12
Without the bonus for the moment. I've done it before for Project Euler, but that code's at home, and I don't want to re-write it. Also, got yelled at for having input in solve(). Moved it out. =)
Output:
3
u/Wedamm Jul 30 '12 edited Jul 30 '12
Haskell:
My solution with bonus. Could probably be done cleverer.
Usage:
Edit: Added support for even larger numbers.
Extrabonus: Implement the latin number system on top so that you can use even bigger numbers. ;)
Edit: Indentation.
2
u/5outh 1 0 Jul 30 '12 edited Jul 30 '12
Not feeling the programming today, so my solution is kind of bland. Also, trailing 0's still give you "thousand and ...", which is sort of lame. It works for most inputs, but that's not really the spirit. Maybe I'll come back and clean this up when I feel a little better, but I wanted to get something done today.
Edit: Minor formatting changes.
2
u/Eddonarth Jul 30 '12 edited Jul 31 '12
Oh I have this code with the bonus but in spanish -.-
2
u/oskar_stephens Aug 01 '12 edited Aug 01 '12
Python:
I'm a bit rusty on python, so i hope my approach hasn't been poisoned by 3 years of java programming.
4
u/paininthebass Jul 30 '12 edited Jul 30 '12
My solution in C++, without bonus:
EDIT: removed volatile, made global variables local.
1
u/ander1dw Jul 30 '12
Java:
Usage:
Output:
1
u/[deleted] Jul 30 '12
Java with bonus
Output
1
u/goldjerrygold_music Jul 30 '12
python, kinda brute forceish:
1
u/semicolondash Jul 30 '12
Scala, no bonus, and doesn't drop unused sections (1,000,000 returns 1 million 000 thousand 000) I suppose this is the part where I should've learned regex so I could drop all substrings that match range ["000" , next integer), but alas, I really need to learn regex.
1
u/ademus4 Jul 31 '12
Python: (not pretty but gets the job done)
Output:
1
u/abraxas235_work Aug 01 '12 edited Aug 01 '12
I didn't see any web based solutions (not that it needs to be :), so I decided to provide one. The input is given through a form and the resulting strings are written out through HTML
PHP:
1
u/cdelahousse Aug 05 '12
Javascript: Close enough....
1
u/anhyzer_way Aug 24 '12
1
u/nagasgura 0 0 Sep 09 '12
Python (not pretty, but it works.)
1
u/[deleted] Jul 30 '12
[removed] — view removed comment