Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  * This was made specific in range of 1-1000 inclusive
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class solution17
  9. {
  10.     public static void main()
  11.     {
  12.         final int START = 1;
  13.         final int MAX = 1000;
  14.         int totalLetter = 0;
  15.        
  16.         for (int i=START; i<=MAX; i++)
  17.         {
  18.             totalLetter+= noOfLetter(i);
  19.         }
  20.        
  21.         System.out.println("Number of letter from "+START+" to "+MAX+" inclusive:"+totalLetter);
  22.     }
  23.    
  24.     public static int noOfLetter(int i)
  25.     {
  26.         if (i==0)
  27.             return 0;//this is for others function and stuff.
  28.         else if (i==1000)//1000
  29.             return 11;
  30.         else if (i/100>0)//100-999
  31.             return noOfLetterHundred(i);
  32.         else if ((i/10)>0)//11-99
  33.             return noOfLetterTen(i);
  34.         //return value for units [1-10]
  35.         else if (i==1||i==2||i==6)
  36.             return 3;
  37.         else if (i==4||i==5||i==9)
  38.             return 4;
  39.         else if (i==3 || i==7 || i==8)
  40.             return 5;
  41.         else
  42.             return -9999999;
  43.     }
  44.    
  45.     //for 11-99 (Tens)
  46.     public static int noOfLetterTen(int i)
  47.     {
  48.         if (i==0)
  49.             return 0;//this is for others function and stuff.
  50.         else if (i==10)
  51.             return 3;
  52.         else if (i==50||i==60||i==40)
  53.             return 5;
  54.         else if (i==11|i==12||i==20||i==30||i==80||i==90)
  55.             return 6;
  56.         else if (i==15||i==16||i==70)//not complete
  57.             return 7;
  58.         else if (i==13||i==14||i==18||i==19)
  59.             return 8;
  60.         else if (i==17)
  61.             return 9;
  62.         else
  63.             return noOfLetterTen(i-i%10) + noOfLetter(i%10);
  64.     }
  65.    
  66.     //for 100-999 (Hundreds)
  67.     public static int noOfLetterHundred(int i)
  68.     {
  69.         int and=0;
  70.         if (i%100!=0)
  71.         {    
  72.             and = 3;
  73.         }
  74.             return noOfLetter(i/100) + 7 + and + noOfLetterTen(i%100);
  75.     }
  76. }