Series with largest GCD and sum equals to n | function to generate and print the sequence ; stores the maximum gcd that can be possible of sequence . ; if maximum gcd comes out to be zero then not possible ; the smallest gcd possible is 1 ; traverse the array to find out the max gcd possible ; checks if the number is divisible or not ; checks if x is smaller then the max gcd possible and x is greater then the resultant gcd till now , then r = x ; checks if n / x is smaller than the max gcd possible and n / x is greater then the resultant gcd till now , then r = x ; traverses and prints d , 2d , 3d , ... , ( k - 1 ) d , ; computes the last element of the sequence n - s . ; prints the last element ; Driver Code
< ? php function print_sequence ( $ n , $ k ) { $ b = ( int ) ( $ n / ( $ k * ( $ k + 1 ) / 2 ) ) ; if ( $ b == 0 ) { echo ( -1 ) ; } else { $ r = 1 ; for ( $ x = 1 ; $ x * $ x <= $ n ; $ x ++ ) { if ( $ n % $ x != 0 ) continue ; if ( $ x <= $ b && $ x > $ r ) $ r = $ x ; if ( $ n / $ x <= $ b && $ n / $ x > $ r ) $ r = $ n / $ x ; } for ( $ i = 1 ; $ i < $ k ; $ i ++ ) echo ( $ r * $ i . " ▁ " ) ; $ res = $ n - ( $ r * ( $ k * ( $ k - 1 ) / 2 ) ) ; echo ( $ res . " " ) ; } } $ n = 24 ; $ k = 4 ; print_sequence ( $ n , $ k ) ; $ n = 24 ; $ k = 5 ; print_sequence ( $ n , $ k ) ; $ n = 6 ; $ k = 4 ; print_sequence ( $ n , $ k ) ; ? >