NumberUtilities.java 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. public class NumberUtilities {
  2. public static String getRange(int stop) {
  3. StringBuilder range = new StringBuilder();
  4. for(int i=0; i<stop; i++){
  5. range.append(i);
  6. }
  7. return range.toString();
  8. }
  9. public static String getRange(int start, int stop) {
  10. StringBuilder range = new StringBuilder();
  11. for(int i=start; i<stop; i++){
  12. range.append(i);
  13. }
  14. return range.toString();
  15. }
  16. public static String getRange(int start, int stop, int step) {
  17. StringBuilder range = new StringBuilder();
  18. for(int i=start; i<stop; i += step){
  19. range.append(i);
  20. }
  21. return range.toString();
  22. }
  23. public static String getEvenNumbers(int start, int stop) {
  24. StringBuilder range = new StringBuilder();
  25. if (start % 2 == 0){
  26. for(int i=start; i<stop; i += 2){
  27. range.append(i);
  28. }
  29. } else {
  30. for(int i=start+1; i<stop; i += 2){
  31. range.append(i);
  32. }
  33. }
  34. return range.toString();
  35. }
  36. public static String getOddNumbers(int start, int stop) {
  37. StringBuilder range = new StringBuilder();
  38. if (start % 2 == 0){
  39. for(int i=start+1; i<stop; i += 2){
  40. range.append(i);
  41. }
  42. } else {
  43. for(int i=start; i<stop; i += 2){
  44. range.append(i);
  45. }
  46. }
  47. return range.toString();
  48. }
  49. public static String getSquareNumbers(int start, int stop){
  50. StringBuilder range = new StringBuilder();
  51. for(int i=start; i<=stop; i ++){
  52. range.append(i*i);
  53. }
  54. return range.toString();
  55. }
  56. public static String getExponentiations(int start, int stop, int exponent) {
  57. StringBuilder range = new StringBuilder();
  58. for(int i=start; i<=stop; i++){
  59. range.append((int)Math.pow(i, exponent));
  60. }
  61. return range.toString();
  62. }
  63. }