You are viewing a single comment's thread. Return to all comments →
I am getting 3rd testcase wrong. what is wrong with my code?
public class Solution { private static BigDecimal PHI = new BigDecimal("1.6180339887498948482045868"); private static BigDecimal SQRT_5_VAL = new BigDecimal("2.23606797749979"); private static List<BigInteger> list = new ArrayList<>(); public static void main(String[] args) { prepareArray(); Scanner sc = new Scanner(System.in); long t = sc.nextLong(); while (t-- > 0) { BigInteger in = sc.nextBigInteger(); BigInteger out = new BigInteger("0"); int index = 0; while (list.get(index).compareTo(in) <= 0) { out = out.add(list.get(index++)); } System.out.println(out); } } private static void prepareArray() { BigInteger max = new BigInteger("400000000000000000"); BigInteger index = new BigInteger("3"); BigInteger nextFiboValue = new BigInteger("0"); nextFiboValue = fiboValue(index); while (nextFiboValue.compareTo(max) <= 0) { list.add(nextFiboValue); index = index.add(new BigInteger("3")); nextFiboValue = fiboValue(index); } } private static BigInteger fiboValue(BigInteger n) { return PHI.pow(n.intValue()).divide(SQRT_5_VAL, RoundingMode.HALF_UP).setScale(0, RoundingMode.HALF_UP).toBigInteger(); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #2: Even Fibonacci numbers
You are viewing a single comment's thread. Return to all comments →
I am getting 3rd testcase wrong. what is wrong with my code?