Sum of first N natural numbers which are divisible by X or Y
Last Updated :
17 Oct, 2022
Given a number N. Given two numbers X and Y, the task is to find the sum of all those numbers from 1 to N that are divisible by X or by Y.
Examples:
Input : N = 20
Output : 98
Input : N = 14
Output : 45
Approach: To solve the problem, follow the below steps:
->Find the sum of numbers that are divisible by X upto N. Denote it by S1.
->Find the sum of numbers that are divisible by Y upto N. Denote it by S2.
->Find the sum of numbers that are divisible by both X and Y upto N. Denote it by S3.
->The final answer will be S1 + S2 – S3.
In order to find the sum, we can use the general formula of A.P. which is:
Sn = (n/2) * {2*a + (n-1)*d}
For S1: The total numbers that will be divisible by X upto N will be N/X and the sum will be:
Hence,
S1 = ((N/X)/2) * (2 * X + (N/X - 1) * X)
For S2: The total numbers that will be divisible by Y upto N will be N/Y and the sum will be:
Hence,
S2 = ((N/Y)/2) * (2 * Y + (N/Y - 1) * Y)
For S3: The total numbers that will be divisible by both X and Y upto N will be N/lcm(X, Y) and the sum will be:
Hence,
S2 = ((N/lcm(X, Y))/2) * ((2 * lcm(X,Y)) + (N/lcm(X,Y) - 1) * lcm(X,Y))/2
Therefore, the result will be:
S = S1 + S2 - S3
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
int sum( int N, int X, int Y)
{
int S1, S2, S3;
S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2;
S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2;
S3 = ((N / lcm(X,Y))) * (2 * (lcm(X,Y))
+ (N / (lcm(X,Y)) - 1) * (lcm(X,Y)))/ 2;
return S1 + S2 - S3;
}
int main()
{
int N = 24;
int X = 4, Y = 6;
cout << sum(N, X, Y);
return 0;
}
|
Java
public class GFG{
static int gcd( int a, int b)
{
if (a == 0 )
return b;
return gcd(b % a, a);
}
static int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int sum( int N, int X, int Y)
{
int S1, S2, S3;
S1 = ((N / X)) * ( 2 * X + (N / X - 1 ) * X) / 2 ;
S2 = ((N / Y)) * ( 2 * Y + (N / Y - 1 ) * Y) / 2 ;
S3 = ((N / ( lcm(X, Y)))) * ( 2 * ( lcm(X, Y))
+ (N / ( lcm(X, Y)) - 1 ) * ( lcm(X, Y)))/ 2 ;
return S1 + S2 - S3;
}
public static void main(String []args)
{
int N = 14 ;
int X = 3 , Y = 5 ;
System.out.println(sum(N, X, Y));
}
}
|
Python3
from math import ceil, floor
def gcd(a,b):
if a = = 0 :
return b
return gcd(b % a, a)
def lcm(a,b):
return (a / gcd(a,b)) * b
def sum (N, X, Y):
S1 = floor(floor(N / X) * floor( 2 * X +
floor(N / X - 1 ) * X) / 2 )
S2 = floor(floor(N / Y)) * floor( 2 * Y +
floor(N / Y - 1 ) * Y) / 2
S3 = floor(floor(N / (lcm(X,Y)))) * floor ( 2 * (lcm(X,Y)) +
floor(N / (lcm(X,Y)) - 1 ) * (lcm(X,Y))) / 2
return S1 + S2 - S3
if __name__ = = '__main__' :
N = 14
X = 3
Y = 5
print ( int ( sum (N, X, Y)))
|
C#
using System;
public class GFG{
static int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int sum( int N, int X, int Y)
{
int S1, S2, S3;
S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2;
S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2;
S3 = ((N / (lcm(X, Y)))) * (2 * (lcm(X, Y))
+ (N / (lcm(X, Y)) - 1) * (lcm(X, Y)))/ 2;
return S1 + S2 - S3;
}
public static void Main()
{
int N = 14;
int X = 3, Y = 5;
Console.Write(sum(N, X, Y));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $a == 0)
return $b ;
return gcd( $b % $a , $a );
}
function lcm( $a , $b )
{
return ( $a / gcd( $a , $b )) * $b ;
}
function sum( $N , $X , $Y )
{
$S1 ; $S2 ; $S3 ;
$S1 = floor (((int) $N / $X )) * (2 * $X + (int)((int) $N / $X - 1) * $X ) / 2;
$S2 = floor (((int) $N / $Y )) * (2 * $Y + (int)((int) $N / $Y - 1) * $Y ) / 2;
$S3 = floor (((int) $N / (lcm( $X , $Y )))) * (2 * (lcm( $X , $Y ))
+ ((int) $N / (lcm( $X , $Y )) - 1) * (int)(lcm( $X , $Y )))/ 2;
return ceil ( $S1 + ( $S2 - $S3 ));
}
$N = 14;
$X = 3;
$Y = 5;
echo sum( $N , $X , $Y );
#This code is contributed by ajit.
?>
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function lcm(a, b)
{
return (a / gcd(a, b)) * b;
}
function sum(N , X , Y)
{
var S1, S2, S3;
S1 = (parseInt(N / X)) * (2 * X + parseInt(N / X - 1) * X) / 2;
S2 = (parseInt(N / Y)) * (2 * Y + parseInt(N / Y - 1) * Y) / 2;
S3 = (parseInt(N / (lcm(X, Y)))) * (2 * (lcm(X, Y))
+ parseInt(N / (lcm(X, Y)) - 1) * (lcm(X, Y)))/ 2;
return S1 + S2 - S3;
}
var N = 14;
var X = 3, Y = 5;
document.write(sum(N, X, Y));
</script>
|
C
#include <stdio.h>
int gcd( int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
int sum( int N, int X, int Y)
{
int S1, S2, S3;
S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2;
S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2;
S3 = ((N / (lcm(X, Y)))) * (2 * (lcm(X, Y)) + (N / (lcm(X, Y)) - 1) * (lcm(X, Y)))/ 2;
return S1 + S2 - S3;
}
int main()
{
int N = 14;
int X = 3, Y = 5;
printf ( "%d " ,sum(N, X, Y));
return 0;
}
|
Time Complexity: O(log(min(X,Y)), for calculating gcd
Auxiliary Space: O(log(min(X,Y))
Share your thoughts in the comments
Please Login to comment...