Sum of N | Function to calculate the sum recursively ; Base cases ; If n is odd ; If n is even ; Function to print the value of Sum ; Driver Program ; first element ; common difference ; Number of elements ; Mod value
def SumGPUtil ( r , n , m ) : NEW_LINE INDENT if n == 0 : return 1 NEW_LINE if n == 1 : return ( 1 + r ) % m NEW_LINE if n % 2 == 1 : NEW_LINE INDENT ans = ( 1 + r ) * SumGPUtil ( r * r % m , ( n - 1 ) // 2 , m ) NEW_LINE DEDENT else : NEW_LINE INDENT ans = 1 + r * ( 1 + r ) * SumGPUtil ( r * r % m , n // 2 - 1 , m ) NEW_LINE DEDENT return ans % m NEW_LINE DEDENT def SumGP ( a , r , N , M ) : NEW_LINE INDENT answer = a * SumGPUtil ( r , N , M ) NEW_LINE answer = answer % M NEW_LINE print ( answer ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE a = 1 NEW_LINE r = 4 NEW_LINE N = 10000 NEW_LINE M = 100000 NEW_LINE INDENT SumGP ( a , r , N , M ) NEW_LINE DEDENT