Count Arithmetic Progressions having sum S and common difference equal to D | C ++ program for the above approach ; Function to count the number of APs with sum S and common difference D ; Multiply S by 2 ; Stores the count of APs ; Iterate over the factors of 2 * S ; Check if i is the factor or not ; Conditions to check if AP can be formed using factor F ; Return the total count of APs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countAPs ( int S , int D ) { S = S * 2 ; int answer = 0 ; for ( int i = 1 ; i <= sqrt ( S ) ; i ++ ) { if ( S % i == 0 ) { if ( ( ( S / i ) - D * i + D ) % 2 == 0 ) answer ++ ; if ( ( D * i - ( S / i ) + D ) % 2 == 0 ) answer ++ ; } } return answer ; } int main ( ) { int S = 12 , D = 1 ; cout << countAPs ( S , D ) ; return 0 ; }