Turing machine |x-y| - algorithm

The problem is not about programming, but about algorithm. We need to solve |x-y| in unary code on the Turing machine. For example if we have 111 * 11 on the tape. * as a number separator, then we need to leave 1. I understand that when we have a space left or right near the *, the program should remove the * and terminate, but I always get it to work if only the numbers on the right are larger or vice versa. To make the same algorithm work and if the ribbon is 11 * 1 and 1 * 11 does not work.
If it is possible to write a system command:
q1 1 -> q2 _ R
Or with a function table as in the screenshot

I would apply this algorithm:
Move to the right and ensure that there is a "1" at both sides of the "*"
Wipe out the rightmost "1" and then walk back to wipe out the leftmost "1"
Repeat
When the condition in the first scan is not true, then wipe out the "*" and stop.
This leads to the following transition table:
State
In
Out
Move
New State
start
1
1
→
toright
start
*
_
→
stop
toright
1
1
→
toright
toright
*
*
→
toright
toright
_
_
←
delright
delright
1
_
←
toleft
delright
*
_
←
stop
toleft
1
1
←
toleft
toleft
*
*
←
toleft
toleft
_
_
→
delleft
delleft
1
_
→
start
Here is a little implementation in JavaScript:
class Turing {
constructor(str, idx=0) {
this.tape = Array.from(str);
this.idx = idx;
}
read() {
return this.tape[this.idx] || " ";
}
writeAndMove(chr, move) {
this.tape[this.idx] = chr;
this.idx += (move > 0) - (move < 0);
}
toString() {
return this.tape.join("").trim();
}
static run(input, transitions, state, endState) {
const turing = new Turing(input);
let move, chr;
while (state !== endState) {
chr = turing.read();
[,,chr, move, state] = transitions.find(t => t[0] === state && t[1] === chr);
turing.writeAndMove(chr, move);
}
return turing.toString();
}
}
const transitions = [
// STATE IN OUT MOVE NEWSTATE
// --------- ---- --- -- ----------
["start" , "1", "1", +1, "toright" ],
["start" , "*", " ", +1, "stop" ],
["toright" , "1", "1", +1, "toright" ],
["toright" , "*", "*", +1, "toright" ],
["toright" , " ", " ", -1, "delright"],
["delright", "1", " ", -1, "toleft" ],
["delright", "*", " ", -1, "stop" ],
["toleft" , "1", "1", -1, "toleft" ],
["toleft" , "*", "*", -1, "toleft" ],
["toleft" , " ", " ", +1, "delleft" ],
["delleft" , "1", " ", +1, "start" ],
];
// Demo run
const output = Turing.run("1*111", transitions, "start", "stop");
console.log(output);

Related

Find the combination of a number set so that the total difference between two number sets is minimal

Find the combinations of lots assigned from newLots into oldLots, so that the sum of differences between newLotCombinations and oldLots is optimal.
If a difference is less than 0, it counts as 0.
All lots from newLot must be assigned into newLotCombinations.
/**
*
* e.g.
*
* newLots = [20.16, 9.95, 12.62, 7.44, 11.18, 9.02, 8.21, 8.22, 6.57, 6.63]
* oldLots = [12.03, 14.03, 16.04, 17.8, 18.04, 22.05]
*
* newLotCombinations | oldLot | Difference (newLotCombinations - oldLot)
* 20.16, 9.95 | 12.03 | 18.03 (20.16 + 9.95 - 12.03)
* 12.62, 7.44 | 14.03 | 6.03 (12.62 + 7.44 - 14.03)
* 11.18 | 16.04 | 0
* ...
* ...
* Sum of differences = 18.03 + 6.03 + 0 + ...
*/
I think this should involve memoizing the paths that I have gone through, like a map, and walking backward when a path is not correct (total sum larger than the assumption)
This is what I have so far:
const newLots = [20.16, 9.95, 12.62, 7.44, 11.18, 9.02, 8.21, 8.22, 6.57, 6.63]; // stack
const oldLots = [12.03, 14.03, 16.04, 17.8, 18.04, 22.05];
// newLotCombinations is an array of array [[], [], [], [], [], []] // i and j
const newLotCombinations = oldLots.map(() => []);
// Assuming the max. sum of differences is 5.
const MAX_SUM_OF_DIFFERENCES = 7;
const sum = 0;
// store information about a path?
const paths = {};
const loopLots = (i = 0, j = 0) => {
if (i === -1) {
console.log('it should not come to here');
console.log(
"No possible combination for max sum of differences:",
MAX_SUM_OF_DIFFERENCES
);
} else if (!newLots.length) {
console.log(
"Combination that works with max sum of difference ",
MAX_SUM_OF_DIFFERENCES,
newLotCombinations
);
}
newLotCombinations[i][j] = newLots.pop();
if (getSumOfDifferences() > MAX_SUM_OF_DIFFERENCES) {
// put it back to stack
newLots.push(newLotCombinations[i].pop());
if (i + 1 < newLotCombinations.length) {
loopLots(i + 1, newLotCombinations[i+ 1].length);
} else {
// It keeps popping until an array has more than 1 element.
// But this is incorrect, as it will loop with 3 combinations of numbers
// It should walk backward until last divergence
while (newLotCombinations[i] && (i === newLotCombinations.length - 1 || newLotCombinations[i].length < 2)) {
newLots.push(newLotCombinations[i].pop());
i--;
}
if (newLotCombinations[i]) {
newLots.push(newLotCombinations[i].pop());
newLotCombinations[i + 1][newLotCombinations[i + 1].length] = newLots.pop();
// loopLots(i + 1, newLotCombinations[i + 1].length);
loopLots(0, newLotCombinations[0].length);
} else {
console.log(
"No possible combination for max sum of differences:",
MAX_SUM_OF_DIFFERENCES
);
}
}
} else {
loopLots(0, newLotCombinations[0].length);
}
};
const getSumOfDifferences = () => {
let sumOfDifferences = 0;
newLotCombinations.forEach((lots, i) => {
const lotSum = lots.reduce((sum, lot) => {
sum += lot;
return sum;
}, 0);
const difference = lotSum - oldLots[i];
if (difference > 0) {
sumOfDifferences += difference;
}
});
return sumOfDifferences;
};
loopLots();
The logic of using newLotCombinations[i].length < 2 is incorrect, because it keeps pushing in the same alternating numbers. If I memoize the paths to check whether I should go further, how can I know when I am walking backward or forward if simply saving the paths that I have walked through?
I am thinking I also should not save a subset path. I should save a path that reaches the end (i.e. 6), because a subset of path contains unknown paths ahead.

Mongo Map-Reduce: group documents in a certain distance by value

I'm trying to convert my aggregation query to Map-Reduce one in MongoDB (using Ruby driver). In my original query, I search for elements in a certain distance from a point and then group them by NEIGHBORHOOD value.
Sample document
{
"_id":"5b01a2c77b61e58732920f86",
"YEAR":2004,
"NEIGHBOURHOOD":"Grandview-Woodland",
"LOC":{"type":"Point","coordinates":[-123.067654,49.26773386]}
}
Aggregation query
crime.aggregate([
{ "$geoNear": {
"near": {
"type": "Point",
"coordinates": [ -123.0837633, 49.26980201 ]
},
"query": { "YEAR": 2004 },
"distanceField": "distance",
"minDistance": 10,
"maxDistance": 10000,
"num": 100000,
"spherical": true
}},
{ "$group": {
"_id": "$NEIGHBOURHOOD",
"count": { "$sum": 1 }
}}
])
So the fragment of the output looks like this:
Output
{"_id"=>"Musqueam", "count"=>80}
{"_id"=>"West Point Grey", "count"=>651}
{"_id"=>"Marpole", "count"=>1367}
Now I'm trying to make something like this in MapReduce. In my map function I try to check if documents are in the correct distance (based on the answer to THIS QUESTION) and if so pass them along to reduce function in which they will be counted. But something is not right and I don't get the desired result - count values are too big. What am I doing wrong?
Map function
map = "function() {" +
"var rad_per_deg = Math.PI/180;" +
"var rm = 6371 * 1000;" +
"var dlat_rad = (this.LOC.coordinates[0] - (-123.0837633)) * rad_per_deg;" +
"var dlon_rad = (this.LOC.coordinates[1] - (49.26980201)) * rad_per_deg;" +
"var lat1_rad = -123.0837633 * rad_per_deg;" +
"var lon1_rad = 49.26980201 * rad_per_deg;" +
"var lat2_rad = this.LOC.coordinates[0] * rad_per_deg;" +
"var lon2_rad = this.LOC.coordinates[1] * rad_per_deg;" +
"var a = Math.pow(Math.sin(dlat_rad/2), 2) + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.pow(Math.sin(dlon_rad/2), 2);" +
"var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));" +
"if( rm * c < 10000) { " +
" emit(this.NEIGHBOURHOOD, {count: 1});" +
"}" +
"};"
Reduce function
reduce = "function(key, values) { " +
"var sum = 0; " +
"values.forEach(function(f) { " +
" sum += f.count; " +
"}); " +
"return {count: sum};" +
"};"
Query
opts = {
query:{ "YEAR": 2004 },
:out => "results",
:raw => true
}
Output
crime.find().map_reduce(map, reduce, opts)
{"_id"=>"", "value"=>{"count"=>2257.0}}
{"_id"=>"Arbutus Ridge", "value"=>{"count"=>6066.0}}
{"_id"=>"Central Business District", "value"=>{"count"=>110947.0}}

Generate maximum HH:MM from a list of 4 integers [duplicate]

This question already has answers here:
Find maximum possible time HH:MM by permuting four given digits
(25 answers)
Closed 5 years ago.
Note: Another question is the duplicate of this one. If I posted this question 6 months earlier than that one, then how can mine be a duplicate?
I have 4 non-negative integers a,b,c,d. All of them are less than or equal to 9. I have to return the maximum time that can be shown on the clock in 24-hour format.
For example: a=9, b=4, c=3, d=1, would yield 19:43
I have so far only been able to come up with brute-force approach which kind of tests all 24-possible combinations. Although this isn't too bad, I was wondering if there are more elegant approaches. All ideas better than brute force are welcome.
Note: It is not a homework question. I got this from an interview prep site but has no solutions there.
Generating all 24 permutations would work, but you don't have to do that. Your validation for the brute force method would require you to validate each digit, and you may as well build up the permutation as you validate each digit. Start with big numbers.
sort numbers in high -> low
iterate over each digit [i] and search through the list until you find the highest number that fits the requirements.
[0] must be <=2
[1] must be <=3 if [0]==2
[2] must be <=5
[3] can be anything
Remove that number from the list, and place it in position [i]
repeat for each digit
Each of those conditions could be expressed as lambda function in a list, making it easy to separate the search loop from the conditions.
The key is to sort the data and then apply these simple rules:
At least one element has to be <= 2
A second element has to be <= 5
If there are only two elements meeting the first two rules then one of them must be < 2
If the element selected for the first value is 2 then the element selected for the second value must be less than 4
The rules are easy to implement by using three counters:
less_than_3 - this must always be at least 1
less_than_6 - this must always be at least 2
less_than_4 - if a == 2 then less_than_4 must be at least 2
Here's a solution in JavaScript that could be further refactored.
function find_the_time(data) {
var arr = data.slice(), ans = {};
var count_less_than_three = 0, count_less_than_four = 0, count_less_than_six = 0;
console.log(arr);
arr.sort(function(a,b) { return a - b; });
if ((arr[0] > 2) || (arr[1] > 5)) {
// Rule 1 - Hh:mm must be <= 2
// Rule 2 - hh:Mm must be <= 5
console.log('No solution');
return -1;
}
for (var el of arr) {
if (el < 3) {
// count_less_than_three will be at least 1
count_less_than_three++;
}
if (el < 4) {
// count_less_than_four will be at least 1
count_less_than_four++;
}
if (el < 6) {
// count_less_than_six will be at least 2
count_less_than_six++;
}
}
if (count_less_than_three === count_less_than_six) {
if (count_less_than_three == 2) {
// Two elements have values less than 3
// so the time must be earlier than 20:00
// Rule 3 - Hh:mm must be <= 1
if (arr[0] > 1) {
console.log('No solution');
return -1;
} else {
ans.c = arr.splice((count_less_than_three - 1), 1);
ans.a = arr.splice((count_less_than_three - 2), 1);
ans.b = arr.splice(1, 1);
ans.d = arr.splice(0, 1);
}
} else {
ans.a = arr.splice((count_less_than_three - 1), 1);
ans.b = arr.splice((count_less_than_three - 2), 1);
if (arr[1] < 6) {
ans.c = arr.splice(1, 1);
ans.d = arr.splice(0, 1);
} else {
ans.d = arr.splice(1, 1);
ans.c = arr.splice(0, 1);
}
}
} else {
ans.a = arr.splice((count_less_than_three - 1), 1);
if (ans.a < 2) {
// b can have any value so select the largest available
ans.b = arr.splice(2, 1);
} else {
// a == 2 so count_less_than_four comes into play
// Rule 4 - hH:mm must be <= 3
// Array size has been reduced so decrement count_less_than_four
count_less_than_four--;
ans.b = arr.splice((count_less_than_four - 1), 1);
}
if (arr[1] < 6) {
ans.c = arr.splice(1, 1);
ans.d = arr.splice(0, 1);
} else {
ans.d = arr.splice(1, 1);
ans.c = arr.splice(0, 1);
}
}
console.log('Answer: ' + ans.a + '' + ans.b + ':' + ans.c + '' + ans.d);
return ans.a + '' + ans.b + ':' + ans.c + '' + ans.d;
}
var test_data = [
[ 2, 1, 2, 1 ],
[ 9, 5, 7, 1 ],
[ 2, 2, 7, 6 ],
[ 2, 6, 6, 1 ],
[ 0, 5, 9, 8 ],
[ 0, 6, 9, 8 ],
[ 2, 5, 9, 3 ]
];
test_data.forEach(find_the_time);
You can change time to minutes.
Then you can compare it.
//23:59 ==> 23*60+59 ==1439 min
//(10a+b)*60+(10c+d)<=1439
This is my code.
String function(int[] numbers){
int num[] = numbers;
int temp = 0;
int cnt=0;
int numA=0;
int numB=0;
int numC=0;
int numD=0;
for(int a=0;a<num.length; a++){
for(int b=0;b<num.length; b++){
for(int c=0;c<num.length; c++){
for(int d=0;d<num.length; d++){
if(a!=b && a!=c && a!=d
&& b!=c && b!=d
&& c!=d){
if((10*num[c]+num[d])<60) {
int cal = (10 * num[a] + num[b]) * 60 + (10 * num[c] + num[d]);
Log.d("Joon1979", "Input Numbers [ " + num[a] + ", " + num[b] + ", " + num[c] + ", " + num[d] + " ]");
if (cal <= 1439) {
cnt++;
if (temp < cal) {
temp = cal;
numA = num[a];
numB = num[b];
numC = num[c];
numD = num[d];
}
}
}
}
}
}
}
}
if(cnt==0){
return "impossible";
}else {
return numA+""+numB+" : "+numC+""+numD;
}
}

Lua Sort Table by Two Values?

So I have the following table:
servers = {"ProtectedMethod" = {name = "ProtectedMethod", visits = 20, players = 2}, "InjecTive" = {name = "InjecTive", visits = 33, players = 1}};
How would I sort the sub-tables in the servers table, into a new array based on players first, and then number of visits, meaning that you don't sort by visits unless two tables have the same value for players.
For example if the sorting code was put into a function called tableSort, I should be able to call the following code:
sorted = sort();
print(sorted[1].name .. ": " sorted[1].players .. ", " .. sorted[1].visits); --Should print "ProtectedMethod: 2, 20"
print(sorted[2].name .. ": " sorted[2].players .. ", " .. sorted[2].visits); --Should print "InjecTive: 1, 33"
TIA
You have a hash, so you need to convert it to an array and then sort:
function mysort(s)
-- convert hash to array
local t = {}
for k, v in pairs(s) do
table.insert(t, v)
end
-- sort
table.sort(t, function(a, b)
if a.players ~= b.players then
return a.players > b.players
end
return a.visits > b.visits
end)
return t
end
servers = {
ProtectedMethod = {
name = "ProtectedMethod", visits = 20, players = 2
},
InjecTive = {
name = "InjecTive", visits = 33, players = 1
}
}
local sorted = mysort(servers)
print(sorted[1].name .. ": " .. sorted[1].players .. ", " .. sorted[1].visits)
print(sorted[2].name .. ": " .. sorted[2].players .. ", " .. sorted[2].visits)

Algorithm that converts numeric amount into English words

What is the most efficient way to convert numeric amount into English words
e.g. 12 to twelve
127 to one hundred twenty-seven
That didn't take long. This is an implementation written in Java.
http://snippets.dzone.com/posts/show/3685
Code
public class IntToEnglish {
static String[] to_19 = { "zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
static String[] tens = { "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
static String[] denom = { "",
"thousand", "million", "billion", "trillion", "quadrillion",
"quintillion", "sextillion", "septillion", "octillion", "nonillion",
"decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
"sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion" };
public static void main(String[] argv) throws Exception {
int tstValue = Integer.parseInt(argv[0]);
IntToEnglish itoe = new IntToEnglish();
System.out.println(itoe.english_number(tstValue));
/* for (int i = 0; i < 2147483647; i++) {
System.out.println(itoe.english_number(i));
} */
}
// convert a value < 100 to English.
private String convert_nn(int val) throws Exception {
if (val < 20)
return to_19[val];
for (int v = 0; v < tens.length; v++) {
String dcap = tens[v];
int dval = 20 + 10 * v;
if (dval + 10 > val) {
if ((val % 10) != 0)
return dcap + "-" + to_19[val % 10];
return dcap;
}
}
throw new Exception("Should never get here, less than 100 failure");
}
// convert a value < 1000 to english, special cased because it is the level that kicks
// off the < 100 special case. The rest are more general. This also allows you to
// get strings in the form of "forty-five hundred" if called directly.
private String convert_nnn(int val) throws Exception {
String word = "";
int rem = val / 100;
int mod = val % 100;
if (rem > 0) {
word = to_19[rem] + " hundred";
if (mod > 0) {
word = word + " ";
}
}
if (mod > 0) {
word = word + convert_nn(mod);
}
return word;
}
public String english_number(int val) throws Exception {
if (val < 100) {
return convert_nn(val);
}
if (val < 1000) {
return convert_nnn(val);
}
for (int v = 0; v < denom.length; v++) {
int didx = v - 1;
int dval = new Double(Math.pow(1000, v)).intValue();
if (dval > val) {
int mod = new Double(Math.pow(1000, didx)).intValue();
int l = val / mod;
int r = val - (l * mod);
String ret = convert_nnn(l) + " " + denom[didx];
if (r > 0) {
ret = ret + ", " + english_number(r);
}
return ret;
}
}
throw new Exception("Should never get here, bottomed out in english_number");
}
}
One way to accomplish this would be to use look up tables. It would be somewhat brute force, but easy to setup. For instance, you could have the words for 0-99 in one table, then a table for tens, hundreds, thousands, etc. Some simple math will give you the index of the word you need from each table.
This is some old python code on my hard drive. There might be bugs but it should show the basic idea:
class Translator:
def transformInt(self, x):
translateNumbers(0,[str(x)])
def threeDigitsNumber(self,number):
snumber=self.twoDigitsNumber(number/100)
if number/100!=0:
snumber+=" hundred "
snumber+self.twoDigitsNumber(number)
return snumber+self.twoDigitsNumber(number)
def twoDigitsNumber(self,number):
snumber=""
if number%100==10:
snumber+="ten"
elif number%100==11:
snumber+="eleven"
elif number%100==12:
snumber+="twelve"
elif number%100==13:
snumber+="thirteen"
elif number%100==14:
snumber+="fourteen"
elif number%100==15:
snumber+="fifteen"
elif number%100==16:
snumber+="sixteen"
elif number%100==17:
snumber+="seventeen"
elif number%100==18:
snumber+="eighteen"
elif number%100==19:
snumber+="nineteen"
else:
if (number%100)/10==2:
snumber+="twenty-"
elif (number%100)/10==3:
snumber+="thirty-"
elif (number%100)/10==4:
snumber+="forty-"
elif (number%100)/10==5:
snumber+="fifty-"
elif (number%100)/10==6:
snumber+="sixty-"
elif (number%100)/10==7:
snumber+="seventy-"
elif (number%100)/10==8:
snumber+="eighty-"
elif (number%100)/10==9:
snumber+="ninety-"
if (number%10)==1:
snumber+="one"
elif (number%10)==2:
snumber+="two"
elif (number%10)==3:
snumber+="three"
elif (number%10)==4:
snumber+="four"
elif (number%10)==5:
snumber+="five"
elif (number%10)==6:
snumber+="six"
elif (number%10)==7:
snumber+="seven"
elif (number%10)==8:
snumber+="eight"
elif (number%10)==9:
snumber+="nine"
elif (number%10)==0:
if snumber!="":
if snumber[len(snumber)-1]=="-":
snumber=snumber[0:len(snumber)-1]
return snumber
def translateNumbers(self,counter,words):
if counter+1<len(words):
self.translateNumbers(counter+1,words)
else:
if counter==len(words):
return True
k=0
while k<len(words[counter]):
if (not (ord(words[counter][k])>47 and ord(words[counter][k])<58)):
break
k+=1
if (k!=len(words[counter]) or k==0):
return 1
number=int(words[counter])
from copy import copy
if number==0:
self.translateNumbers(counter+1,copy(words[0:counter]+["zero"]+words[counter+1:len(words)]))
self.next.append(copy(words[0:counter]+["zero"]+words[counter+1:len(words)]))
return 1
if number<10000:
self.translateNumbers(counter+1,copy(words[0:counter]
+self.seperatewords(self.threeDigitsNumber(number))
+words[counter+1:len(words)]))
self.next.append(copy(words[0:counter]
+self.seperatewords(self.threeDigitsNumber(number))
+words[counter+1:len(words)]))
if number>999:
snumber=""
if number>1000000000:
snumber+=self.threeDigitsNumber(number/1000000000)+" billion "
number=number%1000000000
if number>1000000:
snumber+=self.threeDigitsNumber(number/1000000)+" million "
number=number%1000000
if number>1000:
snumber+=self.threeDigitsNumber(number/1000)+" thousand "
number=number%1000
snumber+=self.threeDigitsNumber(number)
self.translateNumbers(counter+1,copy(words[0:counter]+self.seperatewords(snumber)
+words[counter+1:len(words)]))
self.next.append(copy(words[0:counter]+self.seperatewords(snumber)
+words[counter+1:len(words)]))
This solution doesn't attempt to account for trailing spaces, but it is pretty fast.
typedef const char* cstring;
using std::string;
using std::endl;
std::ostream& GetOnes(std::ostream &output, int onesValue)
{
cstring ones[] = { "zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine" };
output << ones[onesValue];
return output;
}
std::ostream& GetSubMagnitude(std::ostream &output, int subMagnitude)
{
cstring tens[] = { "zeroty", "ten", "twenty", "thirty", "fourty", "fifty",
"sixty", "seventy", "eighty", "ninety"};
if (subMagnitude / 100 != 0)
{
GetOnes(output, subMagnitude / 100) << " hundred ";
GetSubMagnitude(output, subMagnitude - subMagnitude / 100 * 100);
}
else
{
if (subMagnitude >= 20)
{
output << tens[subMagnitude / 10] << " ";
GetOnes(output, subMagnitude - subMagnitude / 10 * 10);
}
else if (subMagnitude >= 10)
{
cstring teens[] = { "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen",
"eighteen", "nineteen" };
output << teens[subMagnitude - 10] << " ";
}
else
{
GetOnes(output, subMagnitude) << " ";
}
}
return output;
}
std::ostream& GetLongNumber(std::ostream &output, double input)
{
cstring magnitudes[] = {"", "hundred", "thousand", "million", "billion",
"trillion"};
double magnitudeTests[] = {1, 100.0, 1000.0, 1000000.0, 1000000000.0,
1000000000000.0 };
int magTestIndex = 0;
while (floor(input / magnitudeTests[magTestIndex++]) != 0);
magTestIndex -= 2;
if (magTestIndex >= 0)
{
double subMagnitude = input / magnitudeTests[magTestIndex];
GetSubMagnitude(output, (int)subMagnitude);
if (magTestIndex) {
output << magnitudes[magTestIndex] << " ";
double remainder = input - (floor(input /
magnitudeTests[magTestIndex]) *
magnitudeTests[magTestIndex]);
if (floor(remainder) > 0)
{
GetLongNumber(output, remainder);
}
}
}
else
{
output << "zero";
}
return output;
}
Start by solving 1-99, using a list of numbers for 1-20, and then 30, 40, ..., 90. Then add hundreds to get 1-999. Then use that routine to give the number of each power of 1,000 for as high as you want to go (I think the highest standard nomenclature is for decillion, which is 10^33).
One slight caveat is that it's a little tricky to get the blanks right in all cases if you're trying to start and end without an excess blank. The easy solution is to put a blank after every word, and then strip off the trailing blank when you're all done. If you try to be more precise while building the string, you're likely to end up with missing blanks or excess blanks.
.) make a library of all numbers & positions (e.g. 1 has other notation than 10, another than 100 etc)
.) make a list of exceptions (e.g. for 12) and be aware, that in your algorythm, the same exception are for 112, 1012 etc.
if you want even more speed, make a cached set of numbers that you need.
Note some rules:
Tens numbers (twenty, thirty, etc.) ending in y are followed by hyphens.
Teens are special (except 15-19, but they're still special).
Everything else is just some combination of digit place like "three thousand".
You can get the place of a number by using floor division of integers: 532 / 100 -> 5
This is part of Common Lisp!
Here's how GNU CLISP does it, and here's how CMUCL does it (easier to read, IMHO).
Doing a code search for "format million billion" will turn up lots of them.

Resources