Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This was made specific in range of 1-1000 inclusive
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class solution17
- {
- public static void main()
- {
- final int START = 1;
- final int MAX = 1000;
- int totalLetter = 0;
- for (int i=START; i<=MAX; i++)
- {
- totalLetter+= noOfLetter(i);
- }
- System.out.println("Number of letter from "+START+" to "+MAX+" inclusive:"+totalLetter);
- }
- public static int noOfLetter(int i)
- {
- if (i==0)
- return 0;//this is for others function and stuff.
- else if (i==1000)//1000
- return 11;
- else if (i/100>0)//100-999
- return noOfLetterHundred(i);
- else if ((i/10)>0)//11-99
- return noOfLetterTen(i);
- //return value for units [1-10]
- else if (i==1||i==2||i==6)
- return 3;
- else if (i==4||i==5||i==9)
- return 4;
- else if (i==3 || i==7 || i==8)
- return 5;
- else
- return -9999999;
- }
- //for 11-99 (Tens)
- public static int noOfLetterTen(int i)
- {
- if (i==0)
- return 0;//this is for others function and stuff.
- else if (i==10)
- return 3;
- else if (i==50||i==60||i==40)
- return 5;
- else if (i==11|i==12||i==20||i==30||i==80||i==90)
- return 6;
- else if (i==15||i==16||i==70)//not complete
- return 7;
- else if (i==13||i==14||i==18||i==19)
- return 8;
- else if (i==17)
- return 9;
- else
- return noOfLetterTen(i-i%10) + noOfLetter(i%10);
- }
- //for 100-999 (Hundreds)
- public static int noOfLetterHundred(int i)
- {
- int and=0;
- if (i%100!=0)
- {
- and = 3;
- }
- return noOfLetter(i/100) + 7 + and + noOfLetterTen(i%100);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement