human
Series with largest GCD and sum equals to n | Python3 code to find the series with largest GCD and sum equals to n.
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.
x = x + 1.
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.
x = x + 1.
traverses and prints d , 2d , 3d , ... , ( k - 1 ) d ,.
main driver"
How can the above be solved in Python?
assistant
#Here is code to series with largest GCD and sum equals to n and the other steps required.
 def print_sequence ( n , k ) :
	b = int ( n / ( k * ( k + 1 ) / 2 ) ) ;
	if b == 0 :
		print ( " - 1" )
	else :
		r = 1 ;
		x = 1
		while x ** 2 <= n :
			if n % x != 0 :
				continue ;
			elif x <= b and x > r :
				r = x
			elif n / x <= b and n / x > r :
				r = n / x
			x = x + 1
		i = 1
		while i < k :
			print ( r * i , end = " ▁ " )
			i = i + 1
		last_term = n - ( r * ( k * ( k - 1 ) / 2 ) )
		print ( last_term )
print_sequence ( 24 , 4 )
print_sequence ( 24 , 5 )
print_sequence ( 6 , 4 )