Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | Javascript Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array a [ ] which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Driver Code
function __gcd ( a , b ) { if ( ! b ) { return a ; } return __gcd ( b , a % b ) ; } function isComposite ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; var i ; for ( i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } function isPerfectSquare ( x ) { var s = Math . sqrt ( x ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } function ifgcdFibonacci ( a , n , k ) { var compositeset = [ ] ; var i ; for ( i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . push ( a [ i ] ) ; } } var gcd = compositeset [ 0 ] ; for ( i = 1 ; i < compositeset . length ; i ++ ) { gcd = __gcd ( gcd , compositeset [ i ] ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { document . write ( " " ) ; return ; } document . write ( " " ) ; return ; } var arr = [ 34 , 2 , 4 , 8 , 5 , 7 , 11 ] ; var n = arr . length ; var k = 2 ; ifgcdFibonacci ( arr , n , k ) ;