Problem
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?
Solution
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);
}
}
}
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);
}
}
}