Sum of multiples of A and B less than N | Python 3 program to find the sum of all multiples of A and B below N ; Function to find sum of AP series ; Number of terms ; Function to find the sum of all multiples of A and B below N ; Since , we need the sum of multiples less than N ; common factors of A and B ; Driver code
from math import gcd , sqrt NEW_LINE def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) NEW_LINE return ( n ) * ( 1 + n ) * d / 2 NEW_LINE DEDENT def sumMultiples ( A , B , n ) : NEW_LINE INDENT n -= 1 NEW_LINE common = int ( ( A * B ) / gcd ( A , B ) ) NEW_LINE return ( sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 NEW_LINE A = 5 NEW_LINE B = 10 NEW_LINE print ( " Sum ▁ = " , int ( sumMultiples ( A , B , n ) ) ) NEW_LINE DEDENT