Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class OddSum {
  4.  
  5.  
  6. public static void main(String[] args) {
  7. Scanner input = new Scanner(System.in);
  8. int odd = 15;
  9. int AckM = 3;
  10. int AckN = 1;
  11. int power = 2;
  12. boolean positive;
  13. double base = 6.5;
  14.  
  15. /* ALL USER INPUTS ARE CHECKED ONCE CAUSE I DIDN'T WANT TO IMPLEMENT MORE METHODS OR LOOPS IN MAIN */
  16.  
  17. System.out.println();
  18. System.out.print("Input an ODD positive integer: ");
  19. odd = input.nextInt();
  20. if(odd % 2 == 0){ //check ONCE if the number is odd by checking if there's a remainder
  21. System.out.print("(Error) try again with an ODD NUMBER!!");
  22. odd = input.nextInt();
  23. System.out.print(oddSum(odd)); //send user input to odd sum for calculation
  24. }
  25. else{
  26. System.out.print(oddSum(odd)); //send user input to odd sum for calculation
  27. }
  28. System.out.println();
  29. System.out.print("Input Ackerman M (<= 3): ");
  30. AckM = input.nextInt(); //take in M
  31. System.out.print("Input Ackerman N (<= 3): ");
  32. AckN = input.nextInt(); //take in N
  33. System.out.print(Ackerman(AckM, AckN)); //send user inputs to Ackerman for calculations
  34. System.out.println();
  35. System.out.print("Input Base (double): ");
  36. base = input.nextDouble(); //take in the base
  37. System.out.print("Input Power (int): ");
  38. power = input.nextInt(); // take in exponent
  39. if(power < 0){ //Are they tryna trick us??
  40. System.out.print("(Error) try a POSITIVE power!!");
  41. power = input.nextInt();
  42. System.out.print(RaiseToPower(base, power)); //send user inputs to calculate answer
  43. }
  44. else{
  45. System.out.print(RaiseToPower(base, power)); //send user inputs to calculate answer
  46. }
  47.  
  48.  
  49. }
  50.  
  51. public static int oddSum(int oddNumber){
  52. int result = oddNumber;
  53.  
  54. int recurse = oddNumber / 2; //Figure out # of odd numbers between input and 1
  55.  
  56. for (int a =0; a < recurse; a++){
  57. result = result + (oddNumber - 2); //Iterate through the odd numbers between input and 1 and add them to the input
  58. oddNumber = oddNumber - 2; //prepare f(n+1)
  59. }
  60.  
  61. // result++;
  62. return result; //return resut
  63. }
  64.  
  65. public static int Ackerman(int m, int n){
  66. if(m == 0){
  67. return n + 1;
  68. }
  69. else{
  70. if(n == 0){ //if n = 0 and m = 0
  71. return Ackerman(m-1, 1);
  72. }
  73. else{ //if m and n are > 0
  74. return Ackerman(m-1, Ackerman(m, n -1));
  75. }
  76. }
  77. }
  78.  
  79. public static double RaiseToPower(double base, int power){
  80. double result = base;
  81. for(int a = 0; a < power - 1; a ++){ //Iterations = exponent
  82. result = result * base; //Iterate through exponents, multiplying by base each time (f(n) = f(n-1) * base)
  83. }
  84.  
  85. return result;
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement