NumberUtilities.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. public class NumberUtilities {
  2. public static String getRange(int start) {
  3. StringBuilder sb = new StringBuilder();
  4. for (int i = 0; i < start; i++) {
  5. sb.append(i);
  6. }
  7. return sb.toString();
  8. }
  9. public static String getRange(int start, int stop) {
  10. StringBuilder sb = new StringBuilder();
  11. int i = 0;
  12. for (i = start; i < stop; i++) {
  13. sb.append(i);
  14. }
  15. return sb.toString();
  16. }
  17. public static String getRange(int start, int stop, int step) {
  18. StringBuilder sb = new StringBuilder();
  19. for (int i = start; i < stop; i += step) {
  20. sb.append(i);
  21. }
  22. return sb.toString();
  23. }
  24. public static String getEvenNumbers(int start, int stop) {
  25. StringBuilder sb = new StringBuilder();
  26. for (int i = start; i < stop; i++) {
  27. if (i % 2 == 0) {
  28. sb.append(i);
  29. }
  30. }
  31. return sb.toString();
  32. }
  33. public static String getOddNumbers(int start, int stop) {
  34. StringBuilder sb = new StringBuilder();
  35. for (int i = start; i < stop; i++) {
  36. if (i % 2 != 0) {
  37. sb. append(i);
  38. }
  39. }
  40. return sb.toString();
  41. }
  42. public static String getExponentiations(int start, int stop, int exponent) {
  43. StringBuilder sb = new StringBuilder();
  44. for (int i = start; i <= stop; i++) {
  45. sb.append((int)Math.pow(i, exponent));
  46. }
  47. return sb.toString();
  48. }
  49. }