Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class OddSum {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int odd = 15;
- int AckM = 3;
- int AckN = 1;
- int power = 2;
- boolean positive;
- double base = 6.5;
- /* ALL USER INPUTS ARE CHECKED ONCE CAUSE I DIDN'T WANT TO IMPLEMENT MORE METHODS OR LOOPS IN MAIN */
- System.out.println();
- System.out.print("Input an ODD positive integer: ");
- odd = input.nextInt();
- if(odd % 2 == 0){ //check ONCE if the number is odd by checking if there's a remainder
- System.out.print("(Error) try again with an ODD NUMBER!!");
- odd = input.nextInt();
- System.out.print(oddSum(odd)); //send user input to odd sum for calculation
- }
- else{
- System.out.print(oddSum(odd)); //send user input to odd sum for calculation
- }
- System.out.println();
- System.out.print("Input Ackerman M (<= 3): ");
- AckM = input.nextInt(); //take in M
- System.out.print("Input Ackerman N (<= 3): ");
- AckN = input.nextInt(); //take in N
- System.out.print(Ackerman(AckM, AckN)); //send user inputs to Ackerman for calculations
- System.out.println();
- System.out.print("Input Base (double): ");
- base = input.nextDouble(); //take in the base
- System.out.print("Input Power (int): ");
- power = input.nextInt(); // take in exponent
- if(power < 0){ //Are they tryna trick us??
- System.out.print("(Error) try a POSITIVE power!!");
- power = input.nextInt();
- System.out.print(RaiseToPower(base, power)); //send user inputs to calculate answer
- }
- else{
- System.out.print(RaiseToPower(base, power)); //send user inputs to calculate answer
- }
- }
- public static int oddSum(int oddNumber){
- int result = oddNumber;
- int recurse = oddNumber / 2; //Figure out # of odd numbers between input and 1
- for (int a =0; a < recurse; a++){
- result = result + (oddNumber - 2); //Iterate through the odd numbers between input and 1 and add them to the input
- oddNumber = oddNumber - 2; //prepare f(n+1)
- }
- // result++;
- return result; //return resut
- }
- public static int Ackerman(int m, int n){
- if(m == 0){
- return n + 1;
- }
- else{
- if(n == 0){ //if n = 0 and m = 0
- return Ackerman(m-1, 1);
- }
- else{ //if m and n are > 0
- return Ackerman(m-1, Ackerman(m, n -1));
- }
- }
- }
- public static double RaiseToPower(double base, int power){
- double result = base;
- for(int a = 0; a < power - 1; a ++){ //Iterations = exponent
- result = result * base; //Iterate through exponents, multiplying by base each time (f(n) = f(n-1) * base)
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement