 # artemkorsakov's solution

## to Sum Of Multiples in the C# Track

Published at Feb 06 2019 · 0 comments
Instructions
Test suite
Solution

Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number.

If we list all the natural numbers below 20 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12, 15, and 18.

The sum of these multiples is 78.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit SumOfMultiples.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

## Source

A variation on Problem 1 at Project Euler http://projecteuler.net/problem=1

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### SumOfMultiplesTest.cs

``````// This file was auto-generated based on version 1.5.0 of the canonical data.

using System;
using Xunit;

public class SumOfMultiplesTest
{
[Fact]
public void No_multiples_within_limit()
{
Assert.Equal(0, SumOfMultiples.Sum(new[] { 3, 5 }, 1));
}

[Fact(Skip = "Remove to run test")]
public void One_factor_has_multiples_within_limit()
{
Assert.Equal(3, SumOfMultiples.Sum(new[] { 3, 5 }, 4));
}

[Fact(Skip = "Remove to run test")]
public void More_than_one_multiple_within_limit()
{
Assert.Equal(9, SumOfMultiples.Sum(new[] { 3 }, 7));
}

[Fact(Skip = "Remove to run test")]
public void More_than_one_factor_with_multiples_within_limit()
{
Assert.Equal(23, SumOfMultiples.Sum(new[] { 3, 5 }, 10));
}

[Fact(Skip = "Remove to run test")]
public void Each_multiple_is_only_counted_once()
{
Assert.Equal(2318, SumOfMultiples.Sum(new[] { 3, 5 }, 100));
}

[Fact(Skip = "Remove to run test")]
public void A_much_larger_limit()
{
Assert.Equal(233168, SumOfMultiples.Sum(new[] { 3, 5 }, 1000));
}

[Fact(Skip = "Remove to run test")]
public void Three_factors()
{
Assert.Equal(51, SumOfMultiples.Sum(new[] { 7, 13, 17 }, 20));
}

[Fact(Skip = "Remove to run test")]
public void Factors_not_relatively_prime()
{
Assert.Equal(30, SumOfMultiples.Sum(new[] { 4, 6 }, 15));
}

[Fact(Skip = "Remove to run test")]
public void Some_pairs_of_factors_relatively_prime_and_some_not()
{
Assert.Equal(4419, SumOfMultiples.Sum(new[] { 5, 6, 8 }, 150));
}

[Fact(Skip = "Remove to run test")]
public void One_factor_is_a_multiple_of_another()
{
Assert.Equal(275, SumOfMultiples.Sum(new[] { 5, 25 }, 51));
}

[Fact(Skip = "Remove to run test")]
public void Much_larger_factors()
{
Assert.Equal(2203160, SumOfMultiples.Sum(new[] { 43, 47 }, 10000));
}

[Fact(Skip = "Remove to run test")]
public void All_numbers_are_multiples_of_1()
{
Assert.Equal(4950, SumOfMultiples.Sum(new[] { 1 }, 100));
}

[Fact(Skip = "Remove to run test")]
public void No_factors_means_an_empty_sum()
{
Assert.Equal(0, SumOfMultiples.Sum(Array.Empty<int>(), 10000));
}

[Fact(Skip = "Remove to run test")]
public void The_only_multiple_of_0_is_0()
{
Assert.Equal(0, SumOfMultiples.Sum(new[] { 0 }, 1));
}

[Fact(Skip = "Remove to run test")]
public void The_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors()
{
Assert.Equal(3, SumOfMultiples.Sum(new[] { 3, 0 }, 4));
}

[Fact(Skip = "Remove to run test")]
public void Solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3()
{
Assert.Equal(39614537, SumOfMultiples.Sum(new[] { 2, 3, 5, 7, 11 }, 10000));
}
}``````
``````﻿using System;
using System.Collections.Generic;
using System.Linq;

public static class SumOfMultiples
{
public static int Sum(IEnumerable<int> multiples, int max)
{
if (!multiples.Any())
{
return 0;
}

if (multiples.Contains(1))
{
return max * (max - 1) / 2;
}

var multiplesLessThanMax = new bool[max + 1];
foreach (var multiple in multiples.Distinct().Where(x => 1 < x && x < max))
{
for (var i = 1; i <= max / multiple; i++)
{
multiplesLessThanMax[i * multiple] = true;
}
}

var sum = 0;
for (var i = 2; i < max; i++)
{
if (multiplesLessThanMax[i])
{
sum += i;
}
}

return sum;
}
}``````