Sum of nth terms of Modified Fibonacci series made by every pair of two arrays | PHP program to find sum of n - th terms of a Fibonacci like series formed using first two terms of two arrays . ; if sum of first term is required ; if sum of second term is required ; fibonacci series used to find the nth term of every series ; as every b [ i ] term appears m times and every a [ i ] term also appears m times ; m is the size of the array
< ? php function sumNth ( & $ A , & $ B , & $ m , & $ n ) { $ res = 0 ; if ( $ n == 1 ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = $ res + $ A [ $ i ] ; } else if ( $ n == 2 ) { for ( $ i = 0 ; $ i < $ m ; $ i ++ ) $ res = $ res + $ B [ $ i ] * $ m ; } else { $ f = array ( ) ; $ f [ 0 ] = 0 ; $ f [ 1 ] = 1 ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) $ f [ $ i ] = $ f [ $ i - 1 ] + $ f [ $ i - 2 ] ; for ( $ i = 0 ; $ i < $ m ; $ i ++ ) { $ res = $ res + ( $ m * ( $ B [ $ i ] * $ f [ $ n - 1 ] ) ) + ( $ m * ( $ A [ $ i ] * $ f [ $ n - 2 ] ) ) ; } } return $ res ; } $ A = array ( 1 , 2 , 3 ) ; $ B = array ( 4 , 5 , 6 ) ; $ n = 3 ; $ m = sizeof ( $ A ) ; echo ( sumNth ( $ A , $ B , $ m , $ n ) ) ; ? >