Skip to content

Instantly share code, notes, and snippets.

@UmarIqbal
Last active December 20, 2015 16:09
Show Gist options
  • Save UmarIqbal/6159612 to your computer and use it in GitHub Desktop.
Save UmarIqbal/6159612 to your computer and use it in GitHub Desktop.
static void Main(string[] args)
{
long result=0;
result+=3*multtiple(999, 3); // it will first sum of multiples of 3 then multiply it by 3
result+=5*multtiple(999, 5); // same for 5
result-=15*multtiple(999, 15); // to remove repeting values(multiples of both 3 and 5)
Console.WriteLine("Sum of multiples of 3 and 5 :" +result );
}
static long multiple(int range, int num)
{
long n=0;
for (int i = range; i > 0; i--) //it will give us largest number within the range that is
if (i % num == 0) //either the multiple of 3 or 5
{
n = i/num;
break;
}
return (n*(n+1))/2; //It will five sumation of series till the range(explained below in series)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment