递归太慢直接淘汰但最简单,而且无法保存状态每次 get 都要重新使用递归,还是使用非递归好一点
使用Integer,Long会溢出,只能使用BigInteger但比较麻烦
public class Code06 { public static void main(String[] args) { final Stream<BigInteger> generate = Stream.generate(new FBNQSupplier()); final List<BigInteger> collect = generate.limit(100).collect(Collectors.toList()); System.out.println(collect); } } class FBNQSupplier implements Supplier<BigInteger>{ BigInteger count = new BigInteger("1"); BigInteger preInt = new BigInteger("1"); BigInteger nextInt = new BigInteger("2"); @Override public BigInteger get() { if (count.compareTo(new BigInteger("3")) < 0) { BigInteger temp; temp = count; count = count.add(new BigInteger("1")); return temp; } BigInteger temp; temp = preInt; preInt = nextInt; nextInt = nextInt.add(temp); return nextInt; } public static int func(int n){ if (n < 3) return n; return func(n-2) + func(n-1); } }
Sign in to make a reply
a学习苦学习累68604
递归太慢直接淘汰但最简单,而且无法保存状态每次 get 都要重新使用递归,还是使用非递归好一点
使用Integer,Long会溢出,只能使用BigInteger但比较麻烦