來源:html
https://www.codewars.com/kata/563f0c54a22b9345bf000053/train/javajava
參考:api
http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#BigInteger(java.lang.String,%20int)oracle
Given u0 = 1, u1 = 2
and the relation 6unun+1-5unun+2+un+1un+2 = 0
calculate un for any integer n >= 0
.this
fct(n) returns un
: fct(17) -> 131072, fct(21) -> 2097152
spa
Remark: You can take two points of view to do this kata:code
the first one purely algorithmic from the definition of un
htm
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
.get
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)); } }