NumberUtilities.java 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. public class NumberUtilities {
  2. public static String getRange(int stop) {
  3. return getRange(0, stop, 1);
  4. }
  5. public static String getRange(int start, int stop) {
  6. return getRange(start, stop, 1);
  7. }
  8. public static String getRange(int start, int stop, int step) {
  9. StringBuilder range = new StringBuilder();
  10. for (int i = start; i < stop; i += step) {
  11. range.append(i);
  12. }
  13. return range.toString();
  14. }
  15. public static String getEvenNumbers(int start, int stop) {
  16. //Start on an even number, step by 2
  17. start = (start % 2) == 1 ? start + 1 : start;
  18. return getRange(start, stop, 2);
  19. }
  20. public static String getOddNumbers(int start, int stop) {
  21. //Start on an odd number, step by 2
  22. start = (start % 2) == 0 ? start + 1 : start;
  23. return getRange(start, stop, 2);
  24. }
  25. public static String getExponentiations(int start, int stop, int exponent) {
  26. StringBuilder range = new StringBuilder();
  27. for (int i = start; i <= stop; i++) {
  28. range.append((int) Math.pow(i, exponent));
  29. }
  30. return range.toString();
  31. }
  32. }