NumberUtilities.java 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import java.util.*;
  2. public class NumberUtilities {
  3. public static String getRange(int stop) {
  4. String numRange = "";
  5. for (int i=0; i<stop; i++) {
  6. numRange += String.valueOf(i);
  7. }
  8. return numRange;
  9. }
  10. public static String getRange(int start, int stop) {
  11. String specNumRange = "";
  12. for (int i=start; i<stop; i++) {
  13. specNumRange += String.valueOf(i);
  14. }
  15. return specNumRange;
  16. }
  17. public static String getRange(int start, int stop, int step) {
  18. String stepNumRange = "";
  19. for (int i=start; i<stop; i+=step) {
  20. stepNumRange += String.valueOf(i);
  21. }
  22. return stepNumRange;
  23. }
  24. public static String getEvenNumbers(int start, int stop) {
  25. String evenNumRange = "";
  26. if (start%2 != 0) {
  27. start += 1;
  28. }
  29. for (int i=start; i<stop; i+=2) {
  30. evenNumRange += String.valueOf(i);
  31. }
  32. return evenNumRange;
  33. }
  34. public static String getOddNumbers(int start, int stop) {
  35. String oddNumRange = "";
  36. if (start%2 == 0) {
  37. start += 1;
  38. }
  39. for (int i=start; i<stop; i+=2) {
  40. oddNumRange += String.valueOf(i);
  41. }
  42. return oddNumRange;
  43. }
  44. public static String getSquareNumbers(int start, int stop) {
  45. String squareNumRange = "";
  46. for (int i=start; i<stop; i++) {
  47. squareNumRange += String.valueOf(i*i);
  48. }
  49. return squareNumRange;
  50. }
  51. public static String getExponentiations(int start, int stop, int exponent) {
  52. return getExponentiationsWithStep(start, stop, 1, exponent);
  53. }
  54. // getExponentiationsWithStep
  55. public static String getExponentiationsWithStep(int start, int stop, int step, int exponent) {
  56. String exNumRange = "";
  57. int exponentialNum = start;
  58. int result = 0;
  59. for (int i=start; i<=stop; i+=step) {
  60. result = (int)Math.pow(i,exponent);
  61. //System.out.println(result);
  62. exNumRange += String.valueOf(result);
  63. }
  64. return exNumRange;
  65. }
  66. }