I am trying to calculate ((3 ^ n) - (3 * (2 ^ n)) + 3) for 1 <= N <109 in JAVA.
But it seems like it takes a long time to calculate this to describe the problem.
double mul = Math.pow(3,k);
double mul2 = Math.pow(2,k);
double res = ((mul - ((3* mul2)) + 3 ) % (1000000000 + 7));
The problems I am facing are
1) Time limit exceeded in java.(which should be less than 1 sec)
2) The result goes out of limit and thus provides wrong output.
Any suggestion for improving the code / calculation method would be helpful. As you can see, the result to be displayed must be modulo (1,000,000,000 + 7). I also tried to write my own power function, in which I do it modulo after each multiplication, which also does not help.
thank
source
share