NumberUtilities.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import java.lang.StringBuilder;
  2. public class NumberUtilities {
  3. public static String getRange(int stop) {
  4. StringBuilder sb = new StringBuilder();
  5. //for each int between 0 and stop var append i
  6. for (int i = 0; i < stop; i++) {
  7. sb.append(i);
  8. }
  9. return sb.toString();
  10. }
  11. public static String getRange(int start, int stop) {
  12. StringBuilder sb = new StringBuilder();
  13. // for each in bw start and stop append i
  14. for (int i = start; i < stop; i++) {
  15. sb.append(i);
  16. }
  17. return sb.toString();
  18. }
  19. public static String getRange(int start, int stop, int step) {
  20. StringBuilder sb = new StringBuilder();
  21. // for each in bw start and stop append i at each interval step
  22. for (int i = start; i < stop; i += step) {
  23. sb.append(i);
  24. }
  25. return sb.toString();
  26. }
  27. public static String getEvenNumbers(int start, int stop) {
  28. StringBuilder sb = new StringBuilder();
  29. // for each in bw start and stop append i if even
  30. for (int i = start; i < stop; i++) {
  31. if (i % 2 == 0) {
  32. sb.append(i);
  33. }
  34. }
  35. return sb.toString();
  36. }
  37. public static String getOddNumbers(int start, int stop) {
  38. StringBuilder sb = new StringBuilder();
  39. // for each in bw start and stop append i if odd
  40. for (int i = start; i < stop; i++) {
  41. if (i % 2 != 0) {
  42. sb.append(i);
  43. }
  44. }
  45. return sb.toString();
  46. }
  47. public static String getExponentiations(int start, int stop, int exponent) {
  48. StringBuilder sb = new StringBuilder();
  49. // for each in bw start and stop append i^exp
  50. for (int i = start; i <= stop; i++) {
  51. sb.append((int)Math.pow(i, exponent));
  52. }
  53. return sb.toString();
  54. }
  55. }