Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created January 20, 2016 05:05
Show Gist options
  • Save deyindra/dc142379a426360612ae to your computer and use it in GitHub Desktop.
Save deyindra/dc142379a426360612ae to your computer and use it in GitHub Desktop.
Given 3 postive number n1, n2 and n3 where n2 and n3 always less than n1 find the sum of all numbers till n1 which are divisible by n2 and n3
public static int findGCD(int a, int b){
if(a==0)
return b;
return findGCD(b%a,a);
}
public static int findLCM(int a, int b){
return a*b/findGCD(a,b);
}
public static long calculateAP(long start, long totalNumber, long difference){
return (totalNumber*((2*start)+(totalNumber-1)*difference))/2;
}
public static long sumOfDivisbleOfn10rn2(int number, int n1, int n2){
long totalNumberDivisiblebyn1=number/n1;
long totalNumberDivisiblebyn2 = number/n2;
long lcm = findLCM(n1,n2);
long totalNumberDivisiblebyLcm = number/lcm;
return calculateAP(n1,totalNumberDivisiblebyn1,n1)
+calculateAP(n2,totalNumberDivisiblebyn2,n2)
-calculateAP(lcm,totalNumberDivisiblebyLcm,lcm);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment