\#include <iostream>
\#include <sstream>
\#include <stdint.h>
\#include <vector>

std::string tenstostring(const unsigned long i)
{
  std::string s;
  if ( i >= 10)
    return std::string("xxx tenstostring i >= 10 xxx");
  if ( i = 1 )
    return std::string("xxx tenstostring i = 1 xxx");
  switch (i)
    {
    case 9: s = "ninety"; break;
    case 8: s = "eighty"; break;
    case 7: s = "seventy"; break;
    case 6: s = "sixty"; break;
    case 5: s = "fifty"; break;
    case 4: s = "forty"; break;
    case 3: s = "thirty"; break;
    case 2: s = "twenty"; break;
    default: s = ""; break;
    }
  return s;
}


std::string lessthantwentytostring(const unsigned long i)
{
  if ( i > 20 )
    return std::string("xxx lessthantwnetytostring i >= 20 xxx");
  std::string s;
  switch (i)
    {
    case 20: s = "twenty"; break;
    case 19: s = "nineteen"; break;
    case 18: s = "eighteen"; break;
    case 17: s = "seventeen"; break;
    case 16: s = "sixteen"; break;
    case 15: s = "fifteen"; break;
    case 14: s = "fourteen"; break;
    case 13: s = "thirteen"; break;
    case 12: s = "twelve"; break;
    case 11: s = "eleven"; break;
    case 10: s = "ten"; break;
    case 9:  s = "nine"; break;
    case 8:  s = "eight"; break;
    case 7:  s = "seven"; break;
    case 6:  s = "six"; break;
    case 5:  s = "five"; break;
    case 4:  s = "four"; break;
    case 3:  s = "three"; break;
    case 2:  s = "two"; break;
    case 1:  s = "one"; break;
    case 0:  s = ""; break;
    default: s = ""; break;
    }
  return s;
}

std::string numberstring(const unsigned long i)
{
  if( i  20 )
    {
      return lessthantwentytostring(i);
    }
  else if ( i  100 )
    {
      unsigned long tens;
      tens = i/10;
      return tenstostring(tens) + lessthantwentytostring(i - tens*10);
    }
  else if ( i  1000 )
    {
      unsigned long hundreds;
      hundreds = i / 100;
      return numberstring(hundreds) + "hundred" + numberstring(i - hundreds*100);
    }
  else if ( i  1000000 )
    {
      unsigned long thousands;
      thousands = i / 1000;
      return numberstring(thousands) + "thousand" + numberstring(i-thousands*1000);
    }
  else if ( i  1000000000 )
    {
      long millions;
      millions = i / 1000000;
      return numberstring(millions) + "million" + numberstring(i-millions*1000000);
    }
}

uint64_t substringlength( unsigned long i, unsigned long multiple)
{
  std::string s = numberstring(i);
  uint64_t ret = 0;
  // factor of 1,000,000
  if ( multiple == 1000000 )
    {
      ret = 1000000;
      ret *=s.length();
      ret += 44872000;
    }
  // factor of 1,000
  else if ( multiple == 1000 )
    {
      ret = 1000;
      ret *= s.length();
      ret += 18440;
    }
  else if ( multiple == 1 )
    {
      ret = s.length();
    }
  return ret;
}

uint64_t partialsum(unsigned long i, unsigned long multiple)
{
  uint64_t ret = 0;
  if ( multiple == 1000000 )
    {
      ret = 1000000;
      ret *= i;
      uint64_t remainder = 500000;
      remainder *= 999999;// sum of 1...999,999
      ret += remainder; 
    }
  else if ( multiple == 1000 )
    {
      ret = 1000;
      ret *= i;
      ret += 499500; // sum of 1...999
    }
  else if ( multiple == 1 )
    {
      ret = i;
    }
  return ret;
}

class substringminder
{
public:
  substringminder(unsigned long i, unsigned long multiple);
  bool operator(const substringminder&) const;

  std::string m_s;
  uint64_t m_substringlength;
  uint64_t m_partialsum;
};

substringminder::substringminder(unsigned long i, unsigned long multiple)
{
  m_s = numberstring(i);
  m_substringlength = substringlength(i, multiple);
  m_partialsum = partialsum(i, multiple);
}

bool substringminder::operator(const substringminder& sstring) const
{
  return m_s  sstring.m_s;
}

int main(int argc, char* argv[])
{
  long i, start, end, incr;
  unsigned long substringlen;
  std::vector substrings;
  if ( argc  2 )
    {
      std::cout  argv[0]  " start-end,increment [start-end,increment ...]"  std::endl;
      return 0;
    }

  int a = 1;
  do
    {
      std::string arg=argv[a];
      int dash=arg.find('-');
      int comma=arg.find(',');
      if ( dash  1 || comma = dash)
    {
      std::cout  argv[0]  "start-end,increment [start-end,increment ...]"  std::endl;
      return 0;
    }
      start = atoi(&(std::string(arg,0,dash)[0]));
      end = atoi(&(std::string(arg,dash+1,comma-dash)[0]));
      incr = atoi(&(std::string(arg,comma+1)[0]));
      for (i=start; i argc);

  // sort on string
  std::sort(substrings.begin(), substrings.end());

  std::vector::iterator sstring;
  uint64_t partialsum = 0;

  for (sstring = substrings.begin(); sstring != substrings.end(); sstring++)
    {
      partialsum += sstring->m_substringlength;
      std::cout  sstring->m_s  ' '  sstring->m_substringlength   ' '  partialsum  std::endl;
    }

  return 1;
}

changed September 19, 2007