-2
\$\begingroup\$
using System;
using System.IO;
class Solution {

static void Main(String[] args) {
    int t = Int32.Parse(Console.ReadLine());
    for(int a = 0; a < t; a++){
        int currentNumber = Int32.Parse(Console.ReadLine());
        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?

\$\endgroup\$
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Martin R Jul 11 at 11:27
  • \$\begingroup\$ Who cares about runtime on something this small? \$\endgroup\$ – Mast Jul 11 at 12:14
  • \$\begingroup\$ 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. \$\endgroup\$ – slepic Jul 11 at 16:31
  • \$\begingroup\$ @Mast I am just trying to figure out what better approach can be there to pass all the test cases. \$\endgroup\$ – swapnil kumar Jul 12 at 8:33
  • \$\begingroup\$ @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. \$\endgroup\$ – swapnil kumar Jul 12 at 8:36
1
\$\begingroup\$

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) 
    {
        int t = Int32.Parse(Console.ReadLine());
        for(int a = 0; a < t; a++)
        {
            int currentNumber = Int32.Parse(Console.ReadLine());
            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)
    {
        int t = Int32.Parse(Console.ReadLine());
        for (int a = 0; a < t; a++)
        {
            int currentNumber = Int32.Parse(Console.ReadLine());
            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);
        }
    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – swapnil kumar Jul 12 at 8:31
  • \$\begingroup\$ Which 2 test cases are you referring to? \$\endgroup\$ – tinstaafl Jul 12 at 11:21
0
\$\begingroup\$

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) {
    int t = Convert.ToInt32(Console.ReadLine());
    for(int a = 0; a < t; a++){
        long n = Convert.ToInt64(Console.ReadLine())-1;    
        long allSum = 0;
        allSum = 3*(n/3)*(1+(n/3))/2 + 5*(n/5)*(1+(n/5))/2 -15*(n/15)*(1+. 
        (n/15))/2;
        Console.WriteLine(allSum);
    }
  }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.