Codewar-006: A disguised sequence (I)

痞子三分冷 提交于 2020-01-08 16:18:09

【推荐】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 unfct(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));
        
    }

}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!