Friday, December 28, 2018

Project Euler Problem 1


Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Sinppet
public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Problem1.GetMultiplesOf3or5(1000));
    }
}
class Problem1
{               
    internal static long GetMultiplesOf3or5(long limit)
    {           
        long sum = 0;
        for (long i = 3; i< limit; i++)
        {
            if ((i % 3 == 0) || (i % 5 == 0))
            {
                sum += i;
            }
        }
        return sum;
    }
}


Steps
  • Loop from 3 to 1000
  • Check if number is multiple of 3 or 5
  • If true, add it to the sum variable

No comments:

Post a Comment

Labels

How to take screenshot of a Webpage using C# Selenium

           ChromeOptions options = new ChromeOptions();             options.AddArgument("headless");//Comment if we want to see ...