Count of Fibonacci divisors of a given number | C ++ program to count number of divisors of N which are Fibonacci numbers ; Function to create hash table to check Fibonacci numbers ; Function to count number of divisors of N which are fibonacci numbers ; If divisors are equal , check and count only one ; Otherwise check and count both ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void createHash ( set < int > & 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 ; } } int countFibonacciDivisors ( int n ) { set < int > hash ; createHash ( hash , n ) ; int cnt = 0 ; for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i == i ) && ( hash . find ( n / i ) != hash . end ( ) ) ) cnt ++ ; else { if ( hash . find ( n / i ) != hash . end ( ) ) cnt ++ ; if ( hash . find ( n / ( n / i ) ) != hash . end ( ) ) cnt ++ ; } } } return cnt ; } int main ( ) { int n = 12 ; cout << countFibonacciDivisors ( n ) ; return 0 ; }