# Project Euler #1: Multiples of 3 and 5 any solutions in c# with better runtime?

using System;
using System.IO;
class Solution {

static void Main(String[] args) {
for(int a = 0; a < t; a++){
int allSum = 0;
for(int i = 3;i<currentNumber;i+=3)**Calculate sum of 3**
{
allSum+=i;
}
for(int i = 5;i<currentNumber;i+=5)**Calculate sum of 5.**
{
if(i%3!=0)
allSum+=i;
}

Console.WriteLine(allSum);
}
}
}


Project Euler #1: Multiples of 3 and 5 any solutions in c# with better runtime? Project Euler #1: Multiples of 3 and 5 any solutions in c# with better runtime?

• Did you solve the Project Euler problem? In that case you can look at the solution overview which gives the essential hints how to solve this efficiently. – It can also be helpful to look at similar questions – Martin R Jul 11 at 11:27
• Who cares about runtime on something this small? – Mast Jul 11 at 12:14
• If i ask you how many integers Are there between x and y, do you count them one by one? Use some maths! You dont need loops at all. – slepic Jul 11 at 16:31
• @Mast I am just trying to figure out what better approach can be there to pass all the test cases. – swapnil kumar Jul 12 at 8:33
• @MartinR thanks for the link to solution archive and yes I tried similar solutions the best I solution I found according to votes was actually returning wrong answers in some edge cases. – swapnil kumar Jul 12 at 8:36

A few things:

Only itemize the using's you are actually using. using System.IO; is not used.

Be consistent with your braces. You have some starting at the end of the control line and some starting on a new line. Pick one style and stick with it. You can even force the style in Visual Studio and Visual Code. So, that if you forget running the format command will correct it for you.

Be consistent with your spacing. Generally, operators are before and after, punctuation is after.

Use proper comment symbols(//) for your comments.

As for your algorithm, you're efficiency can be improved by calculating both sums in the same loop. Something like this should work:

using System;
public class Solution
{

public static void Main(String[] args)
{
for(int a = 0; a < t; a++)
{
int allSum = 0;
for(int threes = 3, fives = 5; threes < currentNumber || fives < currentNumber; threes += 3, fives += 5)//**Calculate sum of 3 & 5**
{
if(threes < currentNumber)
{
allSum += threes;
}
if(fives < currentNumber && fives % threes != 0)
{
allSum += fives;
}
}
Console.WriteLine(allSum);
}
}
}


A version that doesn't use the modulous operator:

using System;

public class Solution
{
public static void Main(String[] args)
{
for (int a = 0; a < t; a++)
{
int allSum = 0;
for (int threes = 3, fives = 5, fCounter = 0; threes < currentNumber || fives < currentNumber; threes += 3, fives += 5, ++fCounter)
{
if (threes < currentNumber)
{
allSum += threes;
}

if (fives < currentNumber)
{
if (fCounter != 2)
{
allSum += fives;
}
else
{
fCounter = -1;
}
}
}

Console.WriteLine(allSum);
}
}
}

• I already did try the same approach but 2 test cases(same ones as before) still did not pass. so any approach that is more efficient would be great. – swapnil kumar Jul 12 at 8:31
• Which 2 test cases are you referring to? – tinstaafl Jul 12 at 11:21

In general, a+(n-1)d=x, gives n=int((x-a)/d+1).

But for this problem we can improve even further, as a=d we get n=int(x/d-d/d+1)=int(x/d).

The nth (last) term, l=a+(n-1)d=d+(int(x/d)-1)d=dint(x/d).

Combining this to find the sum, S=(n/2)(a+l)=(int(x/d)/2)(d+dint(x/d)).

Simplifying, S=dint(x/d)(1+int(x/d))/2.

As the problem asks for the sum of multiples of 3 and 5 we find the sum of each series, but as 3,6,9,... and 5,10,15,... have multiples of 15 in common, we need to subtract the series for 15,30,45,...

However, caution is needed. For eg for the number 1000, so we must use 999 in the formula (otherwise it would include 1000 in the sum, as a multiple of 5):

T = 3int(999/3)(1+int(999/3))/2 + 5int(999/5)(1+int(999/5))/2 - 15int(999/15)(1+int(999/15))/2

Therefore, T = 3333(1+333)/2 + 5199(1+199)/2 - 1566(1+66)/2 = 233168.

using System;
class Solution {
static void Main(String[] args) {