-2

I'm currently writing a program that helps me recognize functions in numerical sequences that simply writes out the difference between each number.

Like if I give it the sequence [21, 20, 18, 15, 11], it'll return [-1 -2 -3 -4] helping me to recognize that the next number in the sequence is probably 6.

As of now this is my code:

    ArrayList<Integer> terms = new ArrayList<Integer>();
    Scanner superScanner = new Scanner(System.in);
    System.out.println("Enter sequence, separate with comma");
    String input = superScanner.nextLine();
    String[] splitted = input.split(",");
    for(String string : splitted){
        int term = Integer.parseInt(string);
        terms.add(term);
    }
    System.out.println("Terms: "+terms);

    ArrayList<Integer> sequence = new ArrayList<Integer>();

    System.out.println("=========DIFFERENCE=========");
    for(int i = 0; i<terms.size(); i++){
        //System.out.println("index "+i);
        //System.out.println("size "+terms.size());
        if(i == terms.size()-1){
            //System.out.println("i == terms.size()");
        }
        else{
            int currentTerm = terms.get(i);
            int nextTerm = terms.get(i+1);
            int difference = nextTerm-currentTerm;
            System.out.print(" "+difference);
            sequence.add(difference);
        }
    }

It's pretty simple and straight forward (I'm still learning). However, I was wondering if there's anyway to make it so that the code also returns the "power to".

Like if I give it 16, 256, 65536 it should return the pattern [1, 1]. The two integers would represent the first term power by 1, the second term power by 1, etc. You know, as in 16*16 = 256 etc...

How could I construct a method that do this? Is there any power-to function in java?

Thanks in advance!

user1531921
  • 1,372
  • 4
  • 18
  • 36

1 Answers1

0

If you want to power a number to another power, use the

Math.pow(double a, double b) : double function

Rakshith Ravi
  • 365
  • 5
  • 16