Count composite fibonacci numbers from given array | Python3 program to implement the above approach ; Function to find all Fibonacci numbers up to Max ; Store all Fibonacci numbers upto Max ; Stores previous element of Fibonacci sequence ; Stores previous element of Fibonacci sequence ; Insert prev into hashmap ; Insert all the Fibonacci numbers up to Max ; Insert curr into hashmap ; Stores curr into temp ; Update curr ; Update prev ; Function to find all Composite numbers up to Max ; isPrime [ i ] : Stores if i is a prime number or not ; Calculate all prime numbers up to Max using Sieve of Eratosthenes ; If P is a prime number ; Set all multiple of P as non - prime ; Function to find the numbers which is both a composite and Fibonacci number ; Stores the largest element of the array ; Traverse the array arr [ ] ; Update Max ; isPrim [ i ] check i is a prime number or not ; Stores all the Fibonacci numbers ; Traverse the array arr [ ] ; Current element is not a composite number ; If current element is a Fibonacci and composite number ; Print current element ; Driver Code
import math NEW_LINE def createhashmap ( Max ) : NEW_LINE INDENT hashmap = { " " } NEW_LINE curr = 1 NEW_LINE prev = 0 NEW_LINE hashmap . add ( prev ) NEW_LINE while ( curr <= Max ) : NEW_LINE INDENT hashmap . add ( curr ) NEW_LINE temp = curr NEW_LINE curr = curr + prev NEW_LINE prev = temp NEW_LINE DEDENT return hashmap NEW_LINE DEDENT def SieveOfEratosthenes ( Max ) : NEW_LINE INDENT isPrime = [ 1 for x in range ( Max + 1 ) ] NEW_LINE isPrime [ 0 ] = 0 NEW_LINE isPrime [ 1 ] = 0 NEW_LINE for p in range ( 0 , int ( math . sqrt ( Max ) ) ) : NEW_LINE INDENT if ( isPrime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , Max , p ) : NEW_LINE INDENT isPrime [ i ] = 0 NEW_LINE DEDENT DEDENT DEDENT return isPrime NEW_LINE DEDENT def cntFibonacciPrime ( arr , N ) : NEW_LINE INDENT Max = arr [ 0 ] NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT Max = max ( Max , arr [ i ] ) NEW_LINE DEDENT isPrime = SieveOfEratosthenes ( Max ) NEW_LINE hashmap = createhashmap ( Max ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if arr [ i ] == 1 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( ( arr [ i ] in hashmap ) and ( not ( isPrime [ arr [ i ] ] ) ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 13 , 55 , 7 , 3 , 5 , 21 , 233 , 144 , 89 ] NEW_LINE N = len ( arr ) NEW_LINE cntFibonacciPrime ( arr , N ) NEW_LINE