public static long fallingPower(int n, int k)
(please use Java to solve)
Python has the integer exponentation operator ** conveniently built in the language, whereas Java unfortunately does not offer that operator that would be mostly useless anyway in a language with fixed size integers that silently hide the overflows easily produced by integer exponentiation. (In both languages, thecaretcharacter ^ denotes thebitwise exclusive oroperation that has bupkis to do with integer exponentiation.)
In the related operation offalling powerthat is useful in many combinatorial formulas and denoted syntactically by underlining the exponent, each term that gets multiplied into the product is always one less than the previous term. For example, the falling power 83is computed as 8 * 7 * 6 = 336. Similarly, the falling power 105equals 10 * 9 * 8 * 7 * 6 = 30240. Nothing essential changes even if the base n is negative; the falling power (-4)5is computed as -4 * -5 * -6 * -7 * -8 = -6720.
This method should compute and return the falling power nkwhere the base n can be any integer, and the exponent k can be anynonnegativeinteger. (Analogous to ordinary powers, n0= 1 for any integer n.) The JUnit fuzz tests are designed so that your method does not have to worry about potential integer overflow... provided that you perform your arithmetic calculations with the long kind of 64-bit integers! If you use the bare 32-bit int type, a silent integeroverflowwill make your method occasionally return incorrect results and fail the JUnit tests, even if that method totes worked correctly when you tried it with your own small values of n.
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here