Problem 1 From Project Euler .net

Project 1 

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 N

using System;
using System.Collections.Generic;
using System.IO;
class Solution 
{
    static void Main(String[] args) 
    {
        int t = Convert.ToInt32(Console.ReadLine());
        for(int a0 = 0; a0 < t; a0++)
        {
            int n = Convert.ToInt32(Console.ReadLine());
            
            var sum = calculate(3, n);
            sum += calculate(5, n);
            sum -= calculate(15, n);
            
            Console.WriteLine(sum);
        }
    }
    static long calculate(int a, int N)
    {
        long m = (N-1) / a;
        long sum = m * (m + 1) / 2; 
        return a * sum;
    }
 }
Live Code on paiza.io

Comments

Post a Comment

Popular Posts