📜  给定数的斐波那契除数的数量

📅  最后修改于: 2021-05-04 13:02:07             🧑  作者: Mango

给定数字N ,任务是找到属于斐波那契数列的N的除数的数量。

例子:

高效的方法

  1. 创建一个哈希表以存储直到N为止的所有斐波那契数,以检入O(1)时间。
  2. 查找O(∛N)中N的所有除数
  3. 对于每个除数,还要检查它是否也是斐波那契数。计算此类除数的数量并打印出来。

下面是上述方法的实现:

C++
// C++ program to count number of divisors
// of N which are Fibonacci numbers
  
#include 
using namespace std;
  
// Function to create hash table
// to check Fibonacci numbers
void createHash(
    set& hash, int maxElement)
{
    int prev = 0, curr = 1;
    hash.insert(prev);
    hash.insert(curr);
  
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.insert(temp);
        prev = curr;
        curr = temp;
    }
}
  
// Function to count number of divisors
// of N which are fibonacci numbers
int countFibonacciDivisors(int n)
{
    set hash;
    createHash(hash, n);
  
    int cnt = 0;
    for (int i = 1; i <= sqrt(n); i++) {
        if (n % i == 0) {
  
            // If divisors are equal,
            // check and count only one
            if ((n / i == i)
                && (hash.find(n / i)
                    != hash.end()))
                cnt++;
  
            // Otherwise check and count both
            else {
                if (hash.find(n / i)
                    != hash.end())
                    cnt++;
                if (hash.find(n / (n / i))
                    != hash.end())
                    cnt++;
            }
        }
    }
    return cnt;
}
  
// Driver code
int main()
{
    int n = 12;
  
    cout << countFibonacciDivisors(n);
  
    return 0;
}


Java
// Java program to count number of divisors
// of N which are Fibonacci numbers
import java.util.*;
  
class GFG{
   
// Function to create hash table
// to check Fibonacci numbers
static void createHash(
    HashSet hash, int maxElement)
{
    int prev = 0, curr = 1;
    hash.add(prev);
    hash.add(curr);
   
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.add(temp);
        prev = curr;
        curr = temp;
    }
}
   
// Function to count number of divisors
// of N which are fibonacci numbers
static int countFibonacciDivisors(int n)
{
    HashSet hash = new HashSet();
    createHash(hash, n);
   
    int cnt = 0;
    for (int i = 1; i <= Math.sqrt(n); i++) {
        if (n % i == 0) {
   
            // If divisors are equal,
            // check and count only one
            if ((n / i == i)
                && (hash.contains(n / i)))
                cnt++;
   
            // Otherwise check and count both
            else {
                if (hash.contains(n / i))
                    cnt++;
                if (hash.contains(n / (n / i)))
                    cnt++;
            }
        }
    }
    return cnt;
}
   
// Driver code
public static void main(String[] args)
{
    int n = 12;
   
    System.out.print(countFibonacciDivisors(n)); 
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 program to count number of divisors
# of N which are Fibonacci numbers
from math import sqrt,ceil,floor
  
# Function to create hash table
# to check Fibonacci numbers
def createHash(maxElement):
    prev = 0
    curr = 1
    d = dict()
    d[prev] = 1
    d[curr] = 1
  
    while (curr <= maxElement):
        temp = curr + prev
        d[temp] = 1
        prev = curr
        curr = temp
    return d
  
# Function to count number of divisors
# of N which are fibonacci numbers
def countFibonacciDivisors(n):
    hash = createHash(n)
  
    cnt = 0
    for i in range(1, ceil(sqrt(n))):
        if (n % i == 0):
  
            # If divisors are equal,
            # check and count only one
            if ((n // i == i)
                and (n // i in hash)):
                cnt += 1
  
            # Otherwise check and count both
            else:
                if (n // i in hash):
                    cnt += 1
                if (n // (n // i) in hash):
                    cnt += 1
    return cnt
  
# Driver code
n = 12
  
print(countFibonacciDivisors(n))
  
# This code is contriuted by mohit kumar 29


C#
// C# program to count number of divisors
// of N which are Fibonacci numbers
using System;
using System.Collections.Generic;
  
class GFG{
    
// Function to create hash table
// to check Fibonacci numbers
static void createHash(
    HashSet hash, int maxElement)
{
    int prev = 0, curr = 1;
    hash.Add(prev);
    hash.Add(curr);
    
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.Add(temp);
        prev = curr;
        curr = temp;
    }
}
    
// Function to count number of divisors
// of N which are fibonacci numbers
static int countFibonacciDivisors(int n)
{
    HashSet hash = new HashSet();
    createHash(hash, n);
    
    int cnt = 0;
    for (int i = 1; i <= Math.Sqrt(n); i++) {
        if (n % i == 0) {
    
            // If divisors are equal,
            // check and count only one
            if ((n / i == i)
                && (hash.Contains(n / i)))
                cnt++;
    
            // Otherwise check and count both
            else {
                if (hash.Contains(n / i))
                    cnt++;
                if (hash.Contains(n / (n / i)))
                    cnt++;
            }
        }
    }
    return cnt;
}
    
// Driver code
public static void Main(String[] args)
{
    int n = 12;
    
    Console.Write(countFibonacciDivisors(n)); 
}
}
   
// This code is contributed by PrinciRaj1992


输出:
3

时间复杂度: O(∛N)