# Sum of integers upto N with given unit digit (Set 2)

Given two integer **N** and **D** where **1 ≤ N ≤ 10 ^{18}**, the task is to find the sum of all the integers from

**1**to

**N**whose unit digit is

**D**.

**Examples:**

Input:N = 30, D = 3

Output:39

3 + 13 + 23 = 39

Input:N = 5, D = 7

Output:0

**Approach:** In Set 1 we saw two basic approaches to find the required sum, but the complexity is **O(N)** which will take more time for larger **N**. Here’s an even efficient approach, suppose we are given **N = 30** and **D = 3**:

sum = 3 + 13 + 23

sum = 3 + (10 + 3) + (20 + 3)

sum = 3 * (3) + (10 + 20)

From the above observation, we can find the sum following the steps below:

- Decrement
**N**until**N % 10 != D**. - Find
**K = N / 10**. - Now,
**sum = (K + 1) * D + (((K * 10) + (10 * K * K)) / 2)**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to return the required sum ` `ll getSum(ll n, ` `int` `d) ` `{ ` ` ` `if` `(n < d) ` ` ` `return` `0; ` ` ` ` ` `// Decrement N ` ` ` `while` `(n % 10 != d) ` ` ` `n--; ` ` ` ` ` `ll k = n / 10; ` ` ` ` ` `return` `(k + 1) * d + (k * 10 + 10 * k * k) / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll n = 30; ` ` ` `int` `d = 3; ` ` ` `cout << getSum(n, d); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the required sum ` `static` `long` `getSum(` `long` `n, ` `int` `d) ` `{ ` ` ` `if` `(n < d) ` ` ` `return` `0` `; ` ` ` ` ` `// Decrement N ` ` ` `while` `(n % ` `10` `!= d) ` ` ` `n--; ` ` ` ` ` `long` `k = n / ` `10` `; ` ` ` ` ` `return` `(k + ` `1` `) * d + (k * ` `10` `+ ` `10` `* k * k) / ` `2` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `long` `n = ` `30` `; ` ` ` `int` `d = ` `3` `; ` ` ` `System.out.println(getSum(n, d)); } ` `} ` `//This code is contributed by inder_verma.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required sum ` `def` `getSum(n, d) : ` ` ` ` ` `if` `(n < d) : ` ` ` `return` `0` ` ` ` ` `# Decrement N ` ` ` `while` `(n ` `%` `10` `!` `=` `d) : ` ` ` `n ` `-` `=` `1` ` ` ` ` `k ` `=` `n ` `/` `/` `10` ` ` ` ` `return` `((k ` `+` `1` `) ` `*` `d ` `+` ` ` `(k ` `*` `10` `+` `10` `*` `k ` `*` `k) ` `/` `/` `2` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `30` ` ` `d ` `=` `3` ` ` `print` `(getSum(n, d)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` ` ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the required sum ` `static` `int` `getSum(` `int` `n, ` `int` `d) ` `{ ` ` ` `if` `(n < d) ` ` ` `return` `0; ` ` ` ` ` `// Decrement N ` ` ` `while` `(n % 10 != d) ` ` ` `n--; ` ` ` ` ` `int` `k = n / 10; ` ` ` ` ` `return` `(k + 1) * d + (k * 10 + 10 * k * k) / 2; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `n = 30; ` ` ` `int` `d = 3; ` ` ` `System.Console.WriteLine(getSum(n, d)); } ` `} ` `//This code is contributed by mits. ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

39

## Recommended Posts:

- Sum of integers upto N with given unit digit
- Count 'd' digit positive integers with 0 as a digit
- Find unit digit of x raised to power y
- Find the unit place digit of sum of N factorials
- Count numbers with unit digit k in given range
- Number of N digit integers with weight W
- Count of m digit integers that are divisible by an integer n
- Integers from the range that are composed of a single distinct digit
- Count positive integers with 0 as a digit and maximum 'd' digits
- Median in a stream of integers (running integers)
- Count of Numbers in Range where first digit is equal to last digit of the number
- Largest number less than N with digit sum greater than the digit sum of N
- Check if frequency of each digit is less than the digit
- Count n digit numbers not having a particular digit
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.