Sum of last digit of all integers from 1 to N divisible by M

Given two integer N and N. The task is to compute the sum of last digit of all integer from 1 to N that is divisible by M.

Examples:

Input: N = 12, M = 1
Output: 48
Number divisible by M = 1 from 1 to 12 is :
{1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 + 2} = 48



Input: N = 100, M = 3
Output: 153

Approach: The problem is based on simple observation.

Let k = floor(N/M) be the number of integers from 1 to N divisible by M.

Since the last digits are needed to be added in the original sum. So, the last digit can be in the range of 0 to 9 and the cycle can be formed by observing the array pattern.

So for each cycle, sum = sum of first 10 last digits, after this, we can divide k by 10 and add the last digit of remaining numbers from starting.

So, Sum = (no. of the cycle * sum of the last digit of first 10 integers divisible by M) + (sum of the last digit of k%10 integers divisible by M).

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation
// of the approach
#include<iostream>
using namespace std;
  
#define long long long
  
// Function to return the 
// required sum
long sumOfLastDig(long n, long m) {
  
    long sum = 0, k;
  
    // Number of element beetween
    // 1 to n divisible by m
    k = n/m;
  
    // Array to store the last digit
    // of elements in a cycle
    long arr[10];
  
    // Storing and adding last
    // digit of cycle
    for (int i = 0; i < 10; i++) {
        arr[i] = m*(i+1) % 10;
        sum += arr[i];
    }
  
    // Number of elements
    // present in last cycle
    long rem = k % 10;
  
    // Sum of k/10 cycle
    long ans = (k/10)*sum;
  
    // Adding value of digits
    // of last cycle to the answer
    for (int i = 0; i < rem; i++) {
        ans += arr[i];
    }
  
    return ans;
}
  
// Driver Code
int main() {
  
    // input n and m
    long n = 100, m = 3;
  
    cout<<sumOfLastDig(n,m);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to return the required sum
static long sumOfLastDig(long n, long m) 
{
    long sum = 0, k;
  
    // Number of element beetween
    // 1 to n divisible by m
    k = n / m;
  
    // Array to store the last digit
    // of elements in a cycle
    long []arr = new long[10];
  
    // Storing and adding last
    // digit of cycle
    for (int i = 0; i < 10; i++) 
    {
        arr[i] = m * (i + 1) % 10;
        sum += arr[i];
    }
  
    // Number of elements
    // present in last cycle
    long rem = k % 10;
  
    // Sum of k/10 cycle
    long ans = (k / 10) * sum;
  
    // Adding value of digits
    // of last cycle to the answer
    for (int i = 0; i < rem; i++) 
    {
        ans += arr[i];
    }
    return ans;
}
  
// Driver Code
public static void main (String[] args)
{
  
    // input n and m
    long n = 100, m = 3;
      
    System.out.println(sumOfLastDig(n, m));
}
}
  
// This code is contributed by jit_t

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the 
# required sum 
def sumOfLastDig(n, m) : 
  
    sum = 0
  
    # Number of element beetween 
    # 1 to n divisible by m 
    k = n // m;
      
    # Array to store the last digit
    # of elements in a cycle
    arr = [0] * 10
  
    # Storing and adding last 
    # digit of cycle 
    for i in range(10) :
        arr[i] = m * (i + 1) % 10
        sum += arr[i]; 
  
    # Number of elements 
    # present in last cycle 
    rem = k % 10
  
    # Sum of k/10 cycle 
    ans = (k // 10) * sum
  
    # Adding value of digits 
    # of last cycle to the answer 
    for i in range(rem) :
        ans += arr[i]; 
  
    return ans; 
  
# Driver Code 
if __name__ == "__main__"
  
    # input n and m 
    n = 100; m = 3
  
    print(sumOfLastDig(n, m)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
          
// Function to return the required sum
static long sumOfLastDig(long n, long m) 
{
    long sum = 0, k;
  
    // Number of element beetween
    // 1 to n divisible by m
    k = n / m;
  
    // Array to store the last digit
    // of elements in a cycle
    long []arr = new long[10];
  
    // Storing and adding last
    // digit of cycle
    for (int i = 0; i < 10; i++) 
    {
        arr[i] = m * (i + 1) % 10;
        sum += arr[i];
    }
  
    // Number of elements
    // present in last cycle
    long rem = k % 10;
  
    // Sum of k/10 cycle
    long ans = (k / 10) * sum;
  
    // Adding value of digits
    // of last cycle to the answer
    for (int i = 0; i < rem; i++) 
    {
        ans += arr[i];
    }
    return ans;
}
  
// Driver Code
static public void Main ()
{
      
    // input n and m
    long n = 100, m = 3;
      
    Console.Write(sumOfLastDig(n, m));
}
}
  
// This code is contributed by ajit.

chevron_right


Output:

153


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : AnkitRai01, jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.