Ad

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.

Code
Diff
  • 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');
    • }
    • }
    • }
Algorithms
Logic
Algebra
Mathematics

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;
    }
  }
}