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, the caret character ^ denotes the bitwise exclusive or operation that has bupkis to do with integer exponentiation.) In the related operation of falling power that 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 83 is computed as 8 * 7 * 6 = 336. Similarly, the falling power 105 equals 10 * 9 * 8 * 7 * 6 = 30240. Nothing essential changes even if the base n is negative; the falling power (-4)5 is computed as -4 * -5 * -6 * -7 * -8 = -6720. This method should compute and return the falling power nk where the base n can be any integer, and the exponent k can be any nonnegative integer. (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 integer overflow will 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.
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, the caret character ^ denotes the bitwise exclusive or operation that has bupkis to do with integer exponentiation.)
In the related operation of falling power that 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 83 is computed as 8 * 7 * 6 = 336. Similarly, the falling power 105 equals 10 * 9 * 8 * 7 * 6 = 30240. Nothing essential changes even if the base n is negative; the falling power (-4)5 is computed as -4 * -5 * -6 * -7 * -8 = -6720.
This method should compute and return the falling power nk where the base n can be any integer, and the exponent k can be any nonnegative integer. (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 integer overflow will 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.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images