Summary
Given an integer, sum all of its digits.
Example
Let's take, for example, the integer 10023.
Its digits are 1, 0, 0, 2, and 3.
Summing them up yields 1 + 0 + 0 + 2 + 3 = 6.
using System; using System.Linq; namespace Kumite { public class Problem { public static int SumDigitsOf(long integer) { return Math.Abs(integer).ToString().Sum(digit => digit - '0'); } } }
def digit_sum(number: int) -> int:return(sum([int(num) for num in str(abs(number))]))- using System;
- using System.Linq;
- namespace Kumite
- {
- public class Problem
- {
- public static int SumDigitsOf(long integer)
- {
- return Math.Abs(integer).ToString().Sum(digit => digit - '0');
- }
- }
- }
namespace TestCases { using NUnit.Framework; using System; using System.Collections.Generic; using System.Linq; [TestFixture] public class SolutionTests { readonly Dictionary<long, int> expected = new Dictionary<long, int> { { 234, 9 }, { 366, 15 }, { -741, 12 }, { 2021, 5 }, { 1998, 27 }, { 1882, 19 }, { -1492, 16 }, { 999999999, 81 }, { 12345678901234, 55 }, { 99999999999999, 126 } }; [Test] public void TestForProblemDescription() { Assert.AreEqual(6, Kumite.Problem.SumDigitsOf(10023)); } [Test] public void Test10Samples() { foreach (var pair in expected) { Assert.AreEqual(pair.Value, Kumite.Problem.SumDigitsOf(pair.Key)); } } } }
from unittest import TestCasefrom solution import digit_sum- namespace TestCases
- {
- using NUnit.Framework;
- using System;
- using System.Collections.Generic;
- using System.Linq;
import codewars_test as testfrom solution import digit_sum@test.describe('Tests')def tests():@test.it('Test Cases')def test_sums():test.assert_equals(digit_sum(10000000000000000000000000000),1)test.assert_equals(digit_sum(55001),11)test.assert_equals(digit_sum(104),5)test.assert_equals(digit_sum(0),0)test.assert_equals(digit_sum(-1204),7)- [TestFixture]
- public class SolutionTests
- {
- readonly Dictionary<long, int> expected = new Dictionary<long, int>
- {
- { 234, 9 },
- { 366, 15 },
- { -741, 12 },
- { 2021, 5 },
- { 1998, 27 },
- { 1882, 19 },
- { -1492, 16 },
- { 999999999, 81 },
- { 12345678901234, 55 },
- { 99999999999999, 126 }
- };
- [Test]
- public void TestForProblemDescription()
- {
- Assert.AreEqual(6, Kumite.Problem.SumDigitsOf(10023));
- }
- [Test]
- public void Test10Samples()
- {
- foreach (var pair in expected)
- {
- Assert.AreEqual(pair.Value, Kumite.Problem.SumDigitsOf(pair.Key));
- }
- }
- }
- }
Summary
Given an integer number - let's call it N -, calculate the sum of the first positive integers up to N (inclusive) that are divisible by 3 or 7.
Example
Let's take N = 30:
Sum of integers divisible by 3 : 3 + 6 + 9 + 12 + 15 + 18 + 21 + 24 + 27 + 30 + 33 = 165 (#1)
Sum of integers divisible by 7 : 7 + 14 + 21 + 28 + 35 = 70 (#2)
Sum of integers divisible by 3 and 7 : 21 + 42 = 21 (#3)
Total : #1 + #2 - #3 = 165 + 70 - 21 = 214
Attention
Please be wary that N may be a large number (N larger than 1,000,000). This requires brushing up on optimization.
Caveat
There is one way of resolving this problem in O(1) complexity. For further details, please refer to this wiki.
using System;
namespace Kumite
{
public class Problem
{
public static long Sum(int N)
{
// Add your code here.
return 0;
}
}
}
namespace Solution {
using NUnit.Framework;
using System;
[TestFixture]
public class SolutionTest
{
[Test]
public void TestForProblemDescription()
{
Assert.AreEqual(214, Kumite.Problem.Sum(30));
}
[Test]
public void TestForNEquals1()
{
Assert.AreEqual(0, Kumite.Problem.Sum(1));
}
[Test]
public void TestForNEquals2()
{
Assert.AreEqual(0, Kumite.Problem.Sum(2));
}
[Test]
public void TestForNEquals3()
{
Assert.AreEqual(3, Kumite.Problem.Sum(3));
}
[Test]
public void TestForExtremeN()
{
Assert.IsTrue(498404749818 == Kumite.Problem.Sum(1525087));
}
[Test]
public void TestForNEquals8()
{
Assert.AreEqual(16, Kumite.Problem.Sum(8));
}
[Test]
public void TestForNEqualsGreaterThan300()
{
Assert.AreEqual(36346, Kumite.Problem.Sum(412));
}
}
}