import 'dart:math';
const ONES = [
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten'
];
const TEN_TWENTY = [
'Eleven',
'Twelve',
'Thirteen',
'Fourteen',
'Fifteen',
'Sixteen',
'Seventeen',
'Eighteen',
'Nineteen'
];
const TENS = [
'Ten',
'Twenty',
'Thirty',
'Forty',
'Fifty',
'Sixty',
'Seventy',
'Eighty',
'Ninety'
];
const HUNDRED = 'Hundred';
const THOUSDAND = 'Thousand';
const AND = 'And';
String inWords = "";
convertToWords(int number) {
String numString = number.toString();
int length = numString.length;
int place = pow(10, length - 1);
if (number == 0) return inWords = "Zero";
if (length == 1) {
inWords = ONES[number - 1];
}
if (length == 2) {
inWords = TENS[(number / place).floor() - 1];
if (number % place != 0) {
if (number < 20) {
return inWords = TEN_TWENTY[(number % place) - 1];
}
inWords += convertToWords(number % place);
}
}
if (length == 3) {
inWords = convertToWords((number / place).floor()) + HUNDRED;
if (number % place != 0) {
inWords += AND + convertToWords(number % place);
}
}
if (length == 4) {
inWords = convertToWords((number / place).floor()) + THOUSDAND;
if (number % place != 0) {
inWords += AND + convertToWords(number % place);
}
}
return inWords;
}
calculateWithRange({int start = 1, end = 1000}) {
int count = 0;
if (end > 1000) {
print("Max safe range is 0 - 1000 inclusive");
return;
}
for (int i = start; i <= end; i++) {
count += convertToWords(i).length;
}
print(count);
}
void main() {
calculateWithRange(start: 1, end: 1000);
}