Find The 46th Fibonacci Number

Consider the Fibonacci sequence, defined as follows:

Fibonacci(1) = 1
Fibonacci(2) = 1
Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

The first two Fibonacci numbers are 1, 1 . The following elements are computed by adding the prior two.

The first 6 Fibonacci numbers are: 1, 1, 2, 3, 5, 8 .

Let F be the 4 6 th 46^\text{th} Fibonacci number. What are the last 3 digits of F ?


The answer is 903.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

11 solutions

Discussions for this problem are now closed

Enamul Hassan
Dec 19, 2013

Given that,

Fibonacci(1) = 1

Fibonacci(2) = 1

and Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

So, this is a recursive relation and we have to compute only the last three digits of Fibonacci(46).

We know the property of number theory, if a = b + c then a%m = (b + c)%m .

Lets declare an array of size 50 (just for safety! because some programming language starts from index 0, some from index 1) named f i b [ 50 ] fib[50] .

now, f i b [ 1 ] : = 1 , f i b [ 2 ] : = 2 fib[1] := 1, fib[2] := 2 .

then a loop from 3 to 46 should run: lets the loop counter be i .

f i b [ i ] : = ( f i b [ i 1 ] + f i b [ i 2 ] ) % m . fib[i] := (fib[i-1] + fib[i-2])\%m.

then p r i n t f i b [ 46 ] print fib[46] .

It is the desired F !

You don't need to use array to store values. We just need prior two.

def fibo():
    a, b = 1, 1
    i = 1
    while True:
        a, b = b, a + b
        i += 1
        if i == 46:
            return a

print(fibo()%1000)

DreamRunner Moshi - 7 years, 5 months ago

Or if you are feeling pretty lazy about writing a program, do this : Open a new tab; go to Google and type "46th Fibonacci no.". Easily done :P

Abhishek Paul - 7 years, 4 months ago

long int fib(long int n) { if(n==0) return 1; if(n==1) return 1; else return fib(n-1) + fib(n-2); }

int main(){ printf("%lu\n",fib(46)); }

Saurav Tomar - 7 years, 5 months ago

int a=1; int b=1; int c,i; for(i=3;i<=46;i++) { c=a+b; a=b;b=c; } printf("%d",c);

Prasanna Kumar Marru - 7 years, 2 months ago
Mayank Shreshtha
Dec 20, 2013

program Q1

implicit none
integer :: F ,s ,N,F1,i
N = 46
do i=1,N
  s = F1(i)
  print*,i,s
enddo

end program Q1

recursive function F1(N) result(F)

implicit none
integer :: F
INTEGER ,intent(in) :: N
if (N <=2) then 
    F = 1
else  
    F = F1(N-2) + F1(N-1)
endif

end function F1

The final answer that i got was 1836311903. sO, THE ANSWER IS 903 \boxed{903} .

Mateo Torres
Dec 19, 2013

I decided to go and learn some Python, did this:

array = [None] * 100

array[0] = 1

array[1] = 1

#

for x in range(2,100):

array[x] = array[x-2] + array[x-1]

#

print(array[45])

package logics;

import java.math.BigInteger; import java.util.ArrayList;

public class MyFibonocci {

static ArrayList<BigInteger>  listSeries= new ArrayList<BigInteger>();

int indexTo;

MyFibonocci(int index){
    this.indexTo = index;
}

public BigInteger getFibonocci(){
    return getFibonocc(this.indexTo);
}

private BigInteger getFibonocc(int n){

    if (listSeries.size() >= n ){
        if ( listSeries.get(n-1) != null ){
            return listSeries.get(n-1);
        }
    }

    if ( n==1 ){
        listSeries.add(new BigInteger("1"));
    }else if (n==2){
        listSeries.add(new BigInteger("1"));
        listSeries.add(new BigInteger("1"));
    }else{
        listSeries.add(getFibonocc(n-1).add(getFibonocc(n-2)));
    }
    return listSeries.get(n-1);
}

public static void main(String[] args) {
    MyFibonocci getFib = new MyFibonocci(46);
    System.out.println(getFib.getFibonocci().toString());
}

}

Branislav Dinic
Mar 18, 2014

You could use recursion:

var fib = function (nth) {
if(nth === 0) {
return 0;
}
if(nth === 1){
return 1;
}
return fib(nth-2) + fib(nth-1);
};
console.log(fib(46));

But it will brake you're environment (both console and node builds), after the 40 it's getting slow and eventuly it cant get out of function calls. You shoud use the array aproach:

var fib = [0, 1];
for (var i = 2; i <= 46; i++) {
fib[ i ] = fib[ i - 1 ] + fib[ i - 2 ];
}
console.log(fib);

[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903 ] [Finished in 0.1s]

Plus it's fast -), and you can try other high numbers.

Sandeep Thakur
Mar 1, 2014

Here is the JS Solution by sandy and aman .

Jacob Prud'homme
Feb 23, 2014

Putting the details given by the question directly into code, we get:

def fibonacci(n):
if n == 1:
    return 1
elif n == 2:
    return 1
else:
    return fibonacci(n-2) + fibonacci(n-1)

print fibonacci(46)

This calculates and returns the 46th Fibonacci sequence number in a much simpler manner than the other solutions. From there, we need only look at the last 3 digits (we have eyes for a reason) to see that the answer is 903 .

from math import *

def F(n): return ((1+sqrt(5)) * * n-(1-sqrt(5)) * * n)/(2 * * n * sqrt(5))

def last 3 digits(n): return int(F(n)%1000)

print last 3 digits(46)

Ye Ibig
Dec 21, 2013

A bit about me: i am a high school student, before i study maths, english and now i'm a specialize student in CS but i hardly know about it and there is a big exam coming so I need a lot of practice and advice. I will always appreciate all yours comment about my solution, my english and whatever about me. Thanks I uses free pascal so it may a little bit differences. uses crt; var i,h:integer; a:array[1..2]of ansistring; begin a[1]:=1; a[2]:=1; h:=1; for i:=1 to 46 do begin a[h]:={sum (a[3-h]+a[h]) plus them by string};// or you can use SHL, copy and change ansistring into string.... h:=3-h; end; end.

I used pascal and c++. but why did you use string instead of just using int/longint/int64 but i don't see you used val? here my code but it is recurtion so it will be TLE-.- var
n: integer;
function Fibo(n: longint): longint;
begin
if (n = 1) or (n = 2) then
Fibo := 1
else
Fibo := Fibo(n - 1) + Fibo(n - 2);
end;
begin
readln(n);
writeln(Fibo(n));
end.
but instead of that tiring code you may just use array to save your fibo and immadiately printout it like this :
var
n,i : longint;
data : array[1..100] of longint;
begin
readln(n);
data[1] := 1;
data[2] := 1;
for i := 3 to n do begin
data[i] := data[i-1] + data[i-2];
end;
writeln(data[n]);
end.
this will be much fast because only required O(n) to executed.










Krisna Attayendra - 7 years, 2 months ago
David Kroell
Dec 19, 2013

import java.util.Scanner;

public class Fibonacci {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("Enter in the number to find the fibonacci function for it: ");
    int num = in.nextInt();

    System.out.println("it is: " + fibonacci(num));
}


public static int fibonacci(int num) {

    if (num < 2) {
        return num;
    }else{
        return fibonacci(num - 1) + fibonacci(num - 2);
    }

You can utilize this by running a simple recursive algorithm, by inputing num for 46 as it shows. You get the number: 1836311903 Therefor the number is 903

This algorithm's syntax is in Java by the way.

whoa mate! you didn't to do all that! just a simple for-loop in the main() would've done the trick!

SR Somayaji - 7 years, 5 months ago

My solution in C++ :

#include <iostream>

using namespace std;

void PrintFibonnaciTerm()
{
    int T_1 = 1, T_2 = 1, T_n;    // First term, 2nd term and nth term variables

    cout << T_1 << endl << T_2 << endl;    // Printing the first two terms of 
                                           // Fibonnaci sequence
    for (int I = 3; I <= 46; I++)          // iterating until 46th term shows up
    {
        T_n = T_1 + T_2;
        cout << T_n << " " << endl;        // displaying the nth term (here 3 <= n <= 46)
        T_1 = T_2;                         // updating variables T_1 and T_2
        T_2 = T_n;
    }
}

int main()
{
    PrintFibonnaciTerm();
    return 0;
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...