Posted: 13th Jun 2007 8:40
Here's the problem:
If the numbers 1 to 5 are written out in words: one, two, three, four, five; there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters.

And my solution:
+ Code Snippet
sync on
total = 11
for i = 1 to 999
cls

if len(str$(i)) = 1
   place1$ = mid$(str$(i),1)
endif

if len(str$(i)) = 2
   place1$ = mid$(str$(i),2)
   place2$ = mid$(str$(i),1)
endif

if len(str$(i)) = 3
   place1$ = mid$(str$(i),3)
   place2$ = mid$(str$(i),2)
   place3$ = mid$(str$(i),1)
endif

if len(str$(i)) = 4
   place1$ = mid$(str$(i),4)
   place2$ = mid$(str$(i),3)
   place3$ = mid$(str$(i),2)
   place4$ = mid$(str$(i),1)
endif

text 70,10,place1$
text 50,10,place2$
text 30,10,place3$
text 10,10,place4$

if place1$ = "1" then inc total,3
if place1$ = "2" then inc total,3
if place1$ = "3" then inc total,5
if place1$ = "4" then inc total,4
if place1$ = "5" then inc total,4
if place1$ = "6" then inc total,3
if place1$ = "7" then inc total,5
if place1$ = "8" then inc total,5
if place1$ = "9" then inc total,4


if place2$ = "1"
   IF place1$ = "0" then inc total,3
   IF place1$ = "8" then inc total,3
   IF place1$ = "1" then inc total,3
   if place1$ = "2" then inc total,3
   if place1$ = "3" then inc total,3
   if place1$ = "5" then inc total,3
   IF place1$ <> "0" AND place1$ <> "8" AND place1$ <> "1" AND place1$ <> "2" AND place1$ <> "3" AND place1$ <> "5" then inc total,4
endif

if place2$ = "2" then inc total,6
if place2$ = "3" then inc total,6
if place2$ = "4" then inc total,5
if place2$ = "5" then inc total,5
if place2$ = "6" then inc total,5
if place2$ = "7" then inc total,7
if place2$ = "8" then inc total,6
if place2$ = "9" then inc total,6

if place3$ <> "" AND place3$ <> "0"
   if place2$ <> "0" OR place1$ <> "0" then inc total,3
endif

if place3$ = "1" then inc total,10
if place3$ = "2" then inc total,10
if place3$ = "3" then inc total,12
if place3$ = "4" then inc total,11
if place3$ = "5" then inc total,11
if place3$ = "6" then inc total,10
if place3$ = "7" then inc total,12
if place3$ = "8" then inc total,12
if place3$ = "9" then inc total,11
`should be good


text 50,50,"TOTAL: " + str$(total)
sync : sync
next i

text 50,50,"TOTAL: " + str$(total)
sync : sync
wait key


That seems like a really hard way to have solved it but it seems to be the only way to code the solution. Anyone else know any otherways to do it that are more direct and dont use as many long lists?
Posted: 13th Jun 2007 12:49
Not tested, but this would probably do it...

+ Code Snippet
count = 0
For n = 1 to 999
  txt$ = str$(n)
  for c = 1 to len(txt$)
  t$ = mid$(txt$,n)
  if t$ = "1" or t$ = "2" or t$ = "6"
     inc count , 3
  endif
  if t$ = "3" ot t$ = "7" or t$ = "8"
     inc count, 5
  endif
  if t$ = "4" ot t$ = "5" or t$ = "9" or t$ = "0"
     inc count, 4
  endif

next n

print str$(count)
sync
wait key
Posted: 13th Jun 2007 14:30
This is how I think it should be done. I get a total of 21223 characters.

+ Code Snippet
c_1=3+3+5+4+4+3+5+5+4                                 `1-9
c_2=3+6+6+8+8+7+7+9+9+8                               `10-19
c_3=((5*10+c_1)*2)+((6*10+c_1)*5)+(7*10+c_1)          `20-99
c_4=(7*9+c_1)+((10*9+c_1)*99)+(c_1*9)+(c_2*9)+(c_3*9) `100-999
c_F=c_4+c_3+c_2+c_1                                   `FINAL
c_F$="TOTAL CHARACTERS: "+str$(c_F)

text screen width()/2-text width(c_F$)/2,screen height()/2-text height(c_F$)/2,c_F$

sync
wait key


The first line 'c_1' simply adds the character from 1-9 together. 'c_2' does the same for '10-19', as these a special words not in character with the others. 'c_3' uses 'fifty' and 'sixty' as 5, 'twenty', 'thirty' etc. as 6 and 'seventy' as 7. The first calculation in 'c_3' does 10 lots of the 5 character word, plus 'c_1', which gives all the characters in, for example, 'fifty' to 'fifty nine'. This is then multiplied by the number of occurences of that length of worded number. The same then goes for 'c_4', just one stage higher. Hope that makes sense.

I get an extra 99 characters with this however, not sure why...
Posted: 13th Jun 2007 18:50
how do u know u get 99 extra characters if u dont know that answer? or do u?

I think niether of those would work properly since with 100 it would be "one hundred" but with 101 it would be "one hundred AND one" so you have the extra 3 letters in there... but you cant add the 3 for all of em since whole hundreds dont have an "and" in them...

Plus things like eighteen not having a second "t" and the 10-13 numbers not ending in teen.


im not sure... maybe im wrong...
Posted: 13th Jun 2007 19:18
Ah, OK, I oversimplified it. I'm sure you could google for a Cheque-writing routine. For you non-UK residents, a cheque is an arcane system whereby you write a promise to pay the bearer a specific amount, and they walk to a bank and hand it over in person. It worked well, when people walked and talked to one another in person.

I digress...all UK-enabled financial packages need a routine to convert a value to words.
Posted: 13th Jun 2007 20:47
LMAO they have cheques in the States too except they call em "Checks" lol

Thats a good idea, I'd never thought of doing that before.
Posted: 13th Jun 2007 20:58
You shouldn't use AND between words unless there is a decimal point.

315 = Three hundred fifteen.
315.1 = Three hundred fifteen AND one tenth.
Posted: 13th Jun 2007 21:10
You shouldn't use AND between words unless there is a decimal point.

Unless you want to be speaking correct (ie. British) English.
Posted: 13th Jun 2007 21:25
You Brits think you're all that and a sack of crisps
(translated from the american "all that and a bag of chips").

I didn't realize that British English differed in the numbers as well. My apologies.
Posted: 13th Jun 2007 22:19
Try this :
(count 0 as ZERO)
+ Code Snippet
count = 0
For num = 1 to 999
  txt$ = str$(num)
  for cmp = 1 to len(txt$)
    T$ = mid$(txt$,cmp)
    Count=Count+(3*(T$="1" or T$="2" Or T$="6"))+(5*(T$="3" Or T$="7" Or T$="8"))+(4*(T$="4" Or T$="5" Or T$="9" Or T$="0"))
   Next cmp
 next num

print str$(count)
sync
wait key

Does not count 0 as it is not said ?
+ Code Snippet
count = 0
For num = 1 to 999
  txt$ = str$(num)
  for cmp = 1 to len(txt$)
    T$ = mid$(txt$,cmp)
    Count=Count+(3*(T$="1" or T$="2" Or T$="6"))+(5*(T$="3" Or T$="7" Or T$="8"))+(4*(T$="4" Or T$="5" Or T$="9"))
   Next cmp
 next num

print str$(count)
sync
wait key
Posted: 13th Jun 2007 22:42
You Brits think you're all that and a sack of crisps

It's a bag of crisps A sack of crisps would take days to eat. Santa carries a sack.

But yes, British English uses "and" in numbers
Posted: 14th Jun 2007 0:04
It's a bag of crisps

Thanks. I'll learn all the differences some day
Posted: 14th Jun 2007 0:49
My code contains the 'AND' as you stated. You can work it out from my code as the fact the it has the multiple 10 (because 'HUNDRED AND' is 10 characters), whereas the first line has 7 (because 'HUNDRED' has 7). It also includes the 100's, as in 'ONE HUNDRED', 'TWO HUNDRED' etc. I know my code is compressed, but you should be able to work out how it has been done.

The reason I say 99 characters more, is that it is 99 characters more than the solution your program generates. This is either an incorrect solution form your or my program, or both.

I think I have included all number combinations in my code, simply read it. I also included all the special character numbers such as ten through nineteen, it is commented in my code.