NumberUtilities.java 2.0KB

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