📌  相关文章
📜  程序查找第n个斐波那契数字的最后一位

📅  最后修改于: 2021-04-24 17:52:46             🧑  作者: Mango

给定数字“ n”,编写一个输出第n个(斐波那契数字)斐波那契数的最后一位的函数(“ n”也可以是一个大数字)。

例子 :

Input : n = 0
Output : 0

Input: n = 2
Output : 1

Input : n = 7
Output : 3

方法1 :(幼稚方法)
一种简单的方法是计算第n个斐波那契数并打印最后一位。

C++
// C++ Program to find last digit 
// of nth Fibonacci number
#include 
using namespace std;
  
typedef long long int ll;
  
void multiply(ll F[2][2], ll M[2][2]);
void power(ll F[2][2], ll n);
  
// Function that returns 
// nth Fibonacci number
ll fib(int n)
{
    ll F[2][2] = {{1, 1}, {1, 0}};
    if (n == 0)
        return 0;
    power(F, n - 1);
    return F[0][0];
}
  
// Utility method to find 
// n'th power of F[][]
void power(ll F[2][2], ll n)
{
    // Base cases
    if (n == 0 || n == 1)
        return;
  
    ll M[2][2] = {{1, 1}, {1, 0}};
  
    power(F, n / 2);
    multiply(F, F);
  
    if (n % 2 != 0)
        multiply(F, M);
}
  
// Utility function to multiply two 
// matrices and store result in first.
void multiply(ll F[2][2], ll M[2][2])
{
    ll x = F[0][0] * M[0][0] + 
           F[0][1] * M[1][0];
    ll y = F[0][0] * M[0][1] +
           F[0][1] * M[1][1];
    ll z = F[1][0] * M[0][0] + 
           F[1][1] * M[1][0];
    ll w = F[1][0] * M[0][1] + 
           F[1][1] * M[1][1];
  
    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
}
  
// Returns last digit of 
// n'th Fibonacci Number
int findLastDigit(int n)
{
return fib(n) % 10;
}
  
// Driver code
int main()
{
    ll n = 1;
    cout << findLastDigit(n) << endl;
    n = 61;
    cout << findLastDigit(n) << endl;
    n = 7;
    cout << findLastDigit(n) << endl;
    n = 67;
    cout << findLastDigit(n) << endl;
    return 0;
}


Java
// Java program to find last digit
// of nth Fibonacci number
class GFG
{
    // Function that returns 
    // nth Fibonacci number 
    static long fib(long n)
    {
        long F[][] = new long[][] {{1, 1}, {1, 0}};
        if (n == 0)
            return 0;
        power(F, n - 1);
  
        return F[0][0];
    }
  
    // Utility function to multiply two
    // matrices and store result in first.
    static void multiply(long F[][], long M[][])
    {
        long x = F[0][0] * M[0][0] + 
                 F[0][1] * M[1][0];
        long y = F[0][0] * M[0][1] + 
                 F[0][1] * M[1][1];
        long z = F[1][0] * M[0][0] + 
                 F[1][1] * M[1][0];
        long w = F[1][0] * M[0][1] + 
                 F[1][1] * M[1][1];
  
        F[0][0] = x;
        F[0][1] = y;
        F[1][0] = z;
        F[1][1] = w;
    }
  
    // Optimized version of power() in method 4 
    static void power(long F[][], long n)
    {
        if( n == 0 || n == 1)
            return;
        long M[][] = new long[][] {{1, 1}, {1, 0}};
  
        power(F, n / 2);
        multiply(F, F);
  
        if (n % 2 != 0)
            multiply(F, M);
    }
  
    // Returns last digit of 
    // n'th Fibonacci Number
    long findLastDigit(long n)
    {
        return (fib(n) % 10);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n;
        GFG ob = new GFG();
        n = 1;
        System.out.println(ob.findLastDigit(n));
        n = 61;
        System.out.println(ob.findLastDigit(n));
        n = 7;
        System.out.println(ob.findLastDigit(n));
        n = 67;
        System.out.println(ob.findLastDigit(n));
    }
}


Python3
# Python3 program to find last digit of 
# nth Fibonacci number
  
# Function that returns nth Fibonacci number 
def fib(n):
  
    F = [[1, 1], [1, 0]];
    if (n == 0):
        return 0;
    power(F, n - 1);
  
    return F[0][0];
  
# Utility function to multiply two
# matrices and store result in first.
def multiply(F, M):
  
    x = F[0][0] * M[0][0] + F[0][1] * M[1][0];
    y = F[0][0] * M[0][1] + F[0][1] * M[1][1];
    z = F[1][0] * M[0][0] + F[1][1] * M[1][0];
    w = F[1][0] * M[0][1] + F[1][1] * M[1][1];
  
    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;
  
# Optimized version of power() in 
# method 4 
def power(F, n):
  
    if( n == 0 or n == 1):
        return;
    M = [[1, 1], [1, 0]];
  
    power(F, int(n / 2));
    multiply(F, F);
  
    if (n % 2 != 0):
        multiply(F, M);
  
# Returns last digit of 
# n'th Fibonacci Number
def findLastDigit(n):
  
    return (fib(n) % 10);
  
# Driver code
n = 1;
print(findLastDigit(n));
n = 61;
print(findLastDigit(n));
n = 7;
print(findLastDigit(n));
n = 67;
print(findLastDigit(n));
  
# This code is contributed
# by chandan_jnu


C#
// C# program to find last digit 
// of nth Fibonacci number
using System;
  
class GFG 
{
      
    // function that returns 
    // nth Fibonacci number
    static long fib(long n)
    {
        long [,]F = new long[,] {{1, 1}, {1, 0}};
          
        if (n == 0)
            return 0;
              
        power(F, n - 1);
  
        return F[0, 0];
    }
  
    // Utility function to multiply two 
    // matrices and store result in first.
    static void multiply(long [,]F, long [,]M)
    {
        long x = F[0, 0] * M[0, 0] + 
                 F[0, 1] * M[1, 0];
        long y = F[0, 0] * M[0, 1] + 
                 F[0, 1] * M[1, 1];
        long z = F[1, 0] * M[0, 0] + 
                 F[1, 1] * M[1, 0];
        long w = F[1, 0] * M[0, 1] + 
                 F[1, 1] * M[1, 1];
  
        F[0, 0] = x;
        F[0, 1] = y;
        F[1, 0] = z;
        F[1, 1] = w;
    }
  
    // Optimized version of power() in method 4
    static void power(long [,]F, long n)
    {
        if( n == 0 || n == 1)
            return;
        long [,]M = new long[,] {{1, 1}, {1, 0}};
  
        power(F, n / 2);
        multiply(F, F);
  
        if (n % 2 != 0)
            multiply(F, M);
    }
  
    // Returns last digit of 
    // n'th Fibonacci Number
    static long findLastDigit(long n)
    {
        return (fib(n) % 10);
    }
  
    // Driver code
    public static void Main()
    {
        int n;
        n = 1;
        Console.WriteLine(findLastDigit(n));
        n = 61;
        Console.WriteLine(findLastDigit(n));
        n = 7;
        Console.WriteLine(findLastDigit(n));
        n = 67;
        Console.WriteLine(findLastDigit(n));
    }
}
  
// This code is contributed by Sam007.


PHP


C++
// Optimized Program to find last 
// digit of nth Fibonacci number
#include
using namespace std;
  
typedef long long int ll;
  
// Finds nth fibonacci number
ll fib(ll f[], ll n)
{
    // 0th and 1st number of
    // the series are 0 and 1
    f[0] = 0;
    f[1] = 1;
  
    // Add the previous 2 numbers 
    // in the series and store 
    // last digit of result
    for (ll i = 2; i <= n; i++)
        f[i] = (f[i - 1] + f[i - 2]) % 10;
  
    return f[n];
}
  
// Returns last digit of n'th Fibonacci Number
int findLastDigit(int n)
{
    ll f[60] = {0};
  
    // Precomputing units digit of  
    // first 60 Fibonacci numbers
    fib(f, 60);
  
    return f[n % 60];
}
  
// Driver code
int main ()
{
    ll n = 1;
    cout << findLastDigit(n) << endl;
    n = 61;
    cout << findLastDigit(n) << endl;
    n = 7;
    cout << findLastDigit(n) << endl;
    n = 67;
    cout << findLastDigit(n) << endl;
    return 0;
}


Java
// Optimized Java program to find last 
// digit of n'th Fibonacci number
class GFG
{
    // Filongs f[] with first 
    // 60 Fibonacci numbers
    void fib(int f[])
    {
        // 0th and 1st number of 
        // the series are 0 and 1
        f[0] = 0;
        f[1] = 1;
  
        // Add the previous 2 numbers 
        // in the series and store 
        // last digit of result 
        for (int i = 2; i <= 59; i++)
            f[i] = (f[i - 1] + f[i - 2]) % 10;
    }
  
    // Returns last digit of n'th Fibonacci Number
    int findLastDigit(long n)
    {
        // In Java, values are 0 by default
        int f[] = new int[60];
  
        // Precomputing units digit of 
        // first 60 Fibonacci numbers
        fib(f);
      
        int index = (int)(n % 60L); 
  
        return f[index];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        long n;
        GFG ob = new GFG();
        n = 1;
        System.out.println(ob.findLastDigit(n));
        n = 61;
        System.out.println(ob.findLastDigit(n));
        n = 7;
        System.out.println(ob.findLastDigit(n));
        n = 67;
        System.out.println(ob.findLastDigit(n));
    }
}


Python3
# Optimized Python3 Program to find last 
# digit of nth Fibonacci number
  
# Finds nth fibonacci number
def fib(f, n):
  
    # 0th and 1st number of
    # the series are 0 and 1
    f[0] = 0;
    f[1] = 1;
  
    # Add the previous 2 numbers 
    # in the series and store 
    # last digit of result
    for i in range(2, n + 1):
        f[i] = (f[i - 1] + f[i - 2]) % 10;
  
    return f;
  
# Returns last digit of n'th 
# Fibonacci Number
def findLastDigit(n):
    f = [0] * 61;
  
    # Precomputing units digit of 
    # first 60 Fibonacci numbers
    f = fib(f, 60);
  
    return f[n % 60];
  
# Driver code
n = 1;
print(findLastDigit(n));
n = 61;
print(findLastDigit(n));
n = 7;
print(findLastDigit(n));
n = 67;
print(findLastDigit(n));
  
# This code is contributed 
# by chandan_jnu


C#
// Optimized C# program to find last
// digit of n'th Fibonacci number
using System;
  
class GFG { 
  
    // Filongs f[] with first 
    // 60 Fibonacci numbers
    static void fib(int []f)
    {
          
        // 0th and 1st number of 
        // the series are 0 and 1
        f[0] = 0;
        f[1] = 1;
  
        // Add the previous 2 numbers 
        // in the series and store
        // last digit of result 
        for (int i = 2; i <= 59; i++)
            f[i] = (f[i - 1] + f[i - 2]) % 10;
    }
  
    // Returns last digit of n'th 
    // Fibonacci Number
    static int findLastDigit(long n)
    {
        int []f = new int[60];
  
        // Precomputing units digit of 
        // first 60 Fibonacci numbers
        fib(f);
      
        int index = (int)(n % 60L); 
  
        return f[index];
    }
  
    // Driver Code
    public static void Main()
    {
        long n;
        n = 1;
        Console.WriteLine(findLastDigit(n));
        n = 61;
        Console.WriteLine(findLastDigit(n));
        n = 7;
        Console.WriteLine(findLastDigit(n));
        n = 67;
        Console.WriteLine(findLastDigit(n));
    } 
}
  
// This code is contributed by Sam007


PHP


输出:

1
1
3
3

复杂度: O(Log n)。

此实现的局限性:
斐波那契数呈指数增长。例如,第200个斐波那契数等于280571172992510140037611932413038677189525。而F(1000)不适合标准C++ int类型。
为了克服这个困难,没有计算第n个斐波那契数的方法,而是一种直接算法来仅计算其最后一位数字(即F(n)mod 10)。

方法2 :(直接方法)
查看每个斐波纳契数的最后一位数字-单位数字:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...

末位数是否有模式?

0, 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, ...

是的!
它需要一段时间才能引起注意。实际上,该系列只有60个数字,然后在Fibonacci系列中一直重复一次相同的序列-直到永远。最终数字系列以60的循环长度重复(有关此结果的说明,请参考此信息)。

因此,与其一遍又一遍地计算斐波那契数,不如先计算前60个斐波那契数的单位位数,并将其存储在数组中,然后将该数组值用于进一步的计算。

C++

// Optimized Program to find last 
// digit of nth Fibonacci number
#include
using namespace std;
  
typedef long long int ll;
  
// Finds nth fibonacci number
ll fib(ll f[], ll n)
{
    // 0th and 1st number of
    // the series are 0 and 1
    f[0] = 0;
    f[1] = 1;
  
    // Add the previous 2 numbers 
    // in the series and store 
    // last digit of result
    for (ll i = 2; i <= n; i++)
        f[i] = (f[i - 1] + f[i - 2]) % 10;
  
    return f[n];
}
  
// Returns last digit of n'th Fibonacci Number
int findLastDigit(int n)
{
    ll f[60] = {0};
  
    // Precomputing units digit of  
    // first 60 Fibonacci numbers
    fib(f, 60);
  
    return f[n % 60];
}
  
// Driver code
int main ()
{
    ll n = 1;
    cout << findLastDigit(n) << endl;
    n = 61;
    cout << findLastDigit(n) << endl;
    n = 7;
    cout << findLastDigit(n) << endl;
    n = 67;
    cout << findLastDigit(n) << endl;
    return 0;
}

Java

// Optimized Java program to find last 
// digit of n'th Fibonacci number
class GFG
{
    // Filongs f[] with first 
    // 60 Fibonacci numbers
    void fib(int f[])
    {
        // 0th and 1st number of 
        // the series are 0 and 1
        f[0] = 0;
        f[1] = 1;
  
        // Add the previous 2 numbers 
        // in the series and store 
        // last digit of result 
        for (int i = 2; i <= 59; i++)
            f[i] = (f[i - 1] + f[i - 2]) % 10;
    }
  
    // Returns last digit of n'th Fibonacci Number
    int findLastDigit(long n)
    {
        // In Java, values are 0 by default
        int f[] = new int[60];
  
        // Precomputing units digit of 
        // first 60 Fibonacci numbers
        fib(f);
      
        int index = (int)(n % 60L); 
  
        return f[index];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        long n;
        GFG ob = new GFG();
        n = 1;
        System.out.println(ob.findLastDigit(n));
        n = 61;
        System.out.println(ob.findLastDigit(n));
        n = 7;
        System.out.println(ob.findLastDigit(n));
        n = 67;
        System.out.println(ob.findLastDigit(n));
    }
}

Python3

# Optimized Python3 Program to find last 
# digit of nth Fibonacci number
  
# Finds nth fibonacci number
def fib(f, n):
  
    # 0th and 1st number of
    # the series are 0 and 1
    f[0] = 0;
    f[1] = 1;
  
    # Add the previous 2 numbers 
    # in the series and store 
    # last digit of result
    for i in range(2, n + 1):
        f[i] = (f[i - 1] + f[i - 2]) % 10;
  
    return f;
  
# Returns last digit of n'th 
# Fibonacci Number
def findLastDigit(n):
    f = [0] * 61;
  
    # Precomputing units digit of 
    # first 60 Fibonacci numbers
    f = fib(f, 60);
  
    return f[n % 60];
  
# Driver code
n = 1;
print(findLastDigit(n));
n = 61;
print(findLastDigit(n));
n = 7;
print(findLastDigit(n));
n = 67;
print(findLastDigit(n));
  
# This code is contributed 
# by chandan_jnu

C#

// Optimized C# program to find last
// digit of n'th Fibonacci number
using System;
  
class GFG { 
  
    // Filongs f[] with first 
    // 60 Fibonacci numbers
    static void fib(int []f)
    {
          
        // 0th and 1st number of 
        // the series are 0 and 1
        f[0] = 0;
        f[1] = 1;
  
        // Add the previous 2 numbers 
        // in the series and store
        // last digit of result 
        for (int i = 2; i <= 59; i++)
            f[i] = (f[i - 1] + f[i - 2]) % 10;
    }
  
    // Returns last digit of n'th 
    // Fibonacci Number
    static int findLastDigit(long n)
    {
        int []f = new int[60];
  
        // Precomputing units digit of 
        // first 60 Fibonacci numbers
        fib(f);
      
        int index = (int)(n % 60L); 
  
        return f[index];
    }
  
    // Driver Code
    public static void Main()
    {
        long n;
        n = 1;
        Console.WriteLine(findLastDigit(n));
        n = 61;
        Console.WriteLine(findLastDigit(n));
        n = 7;
        Console.WriteLine(findLastDigit(n));
        n = 67;
        Console.WriteLine(findLastDigit(n));
    } 
}
  
// This code is contributed by Sam007

的PHP


输出:

1
1
3
3

复杂度: O(1)。