Project Euler : A 1000 Digit Fibonacci Number

The 25th problem in Project Euler asks the user to find the first fibonacci number which has 1000 digits. I brute forced the solution using the BigInteger class of java to find the solution. Here’s the code:

package problems_21_30;

import java.math.BigInteger;

public class Problem_25 {
	public static void main(String[] args) {
		BigInteger[] number = new BigInteger[3];
		number[0] = BigInteger.ONE;
		number[1] = BigInteger.ONE;
		int index = 1;
		int count = 2;
		while (number[index].toString().length() < 1000) {
			index = (index + 1) % 3;
			number[index] = number[(index + 2) % 3].add(number[(index + 1) % 3]);
			count++;
		}
		System.out.println(count);
	}
}

Popularity: 25% [?]

Related posts:

  1. Project Euler : Compute ( p * a^b + q ) mod c
  2. Project Euler : Find Hexagonal Pentagonal Number
  3. Project Euler : Adding 100 50-Digit Numbers
  4. Project Euler : Sum Of Digits In a^b
  5. Project Euler : Palindrome Made From Product Of Three Digit Numbers

  • Pifella

    … and what is the answer? Ehn! (ryhmes with Lisa Simpson’s “meh.” Was anticipating a new delicious number to learn, now that thousands of digits of pi are now routine practice. If you post the answer, I will post a video reciting this number at high speed with eyes closed, etc., from memory, honoring the slow-motion thinking required for you to solve this. Sincere good on you. 

    • http://blog.singhanuvrat.com Anuvrat Singh

      It was never my intention to give away the answers. I have always only posted my thought process and code snippets hoping that it might be of help someone else.

  • Thom Jenkinson

    Being slightly nitpicky, but what happens when the size of the number overflows the size of ‘BigInteger’? Having just solved this, might be an idea to think about storing the number as an array of n digits and calculating it this way.