fork(38) download
  1. public class Main
  2. {
  3. static final int MAX_LENGTH = 13; // about the biggest that will fit into long, use BigInteger for bigger values
  4. static long pow(long a, int b)
  5. {
  6. if (b == 0) return 1;
  7. if (b == 1) return a;
  8. if (b%2 == 0) return pow(a*a, b/2); //even a=(a^2)^b/2
  9. else return a*pow(a*a, b/2); //odd a=a*(a^2)^b/2
  10. }
  11.  
  12. static long printValue(String s)
  13. {
  14. long result = 0;
  15. for (int i = 0; i < s.length(); i++)
  16. result += pow(27, MAX_LENGTH - i - 1)*(1 + s.charAt(i) - 'a');
  17. System.out.printf("%-20s = %20d\n", s, result);
  18. return result;
  19. }
  20.  
  21. public static void main(String[] args)
  22. {
  23. printValue("a");
  24. printValue("aa");
  25. printValue("aaa");
  26. printValue("aaaa");
  27. printValue("aaaaa");
  28. printValue("abacus");
  29. printValue("abbreviation");
  30. printValue("account");
  31. printValue("accuracy");
  32. printValue("announcement");
  33. printValue("z");
  34. }
  35. }
Success #stdin #stdout 0.1s 27984KB
stdin
Standard input is empty
stdout
a                    =   150094635296999121
aa                   =   155653695863554644
aaa                  =   155859586995649293
aaaa                 =   155867212593134280
aaaaa                =   155867495022670761
abacus               =   161447654121636735
abbreviation         =   161763445236432690
account              =   167509959568845165
accuracy             =   167554723653128367
announcement         =   230924421746611173
z                    =  3902460517721977146