【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
来源:
https://www.codewars.com/kata/563f0c54a22b9345bf000053/train/java
参考:
http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int)
Given u0 = 1, u1 = 2
and the relation 6unun+1-5unun+2+un+1un+2 = 0
calculate un for any integer n >= 0
.
Examples
fct(n) returns un
: fct(17) -> 131072, fct(21) -> 2097152
Remark: You can take two points of view to do this kata:
-
the first one purely algorithmic from the definition of
un
-
the second one - not at all mandatory, but as a complement - is to get a bit your head around and find which sequence is hidden behind
un
.
package codewars;
import java.math.BigInteger;
class HiddenSeq {
public static BigInteger fcn(int n) {
return new BigInteger("2").pow(n);
}
}
package codewars;
import static org.junit.Assert.*;
import org.junit.Test;
import java.math.BigInteger;
public class HiddenSeqTest {
private static void testing(BigInteger actual, BigInteger expected) {
assertEquals(expected, actual);
}
@Test
public void test1() {
System.out.println("Fixed Tests: fcn");
testing(HiddenSeq.fcn(17), BigInteger.valueOf(131072));
testing(HiddenSeq.fcn(21), BigInteger.valueOf(2097152));
testing(HiddenSeq.fcn(14), BigInteger.valueOf(16384));
testing(HiddenSeq.fcn(43), BigInteger.valueOf(8796093022208L));
testing(HiddenSeq.fcn(19), BigInteger.valueOf(524288));
}
}
来源:oschina
链接:https://my.oschina.net/u/553266/blog/800448