Sum of last digit of all integers from 1 to N divisible by M
Last Updated :
29 Mar, 2022
Given two integer N and N. The task is to compute the sum of last digit of all integers 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++
#include<iostream>
using namespace std;
#define long long long
long sumOfLastDig( long n, long m) {
long sum = 0, k;
k = n/m;
long arr[10];
for ( int i = 0; i < 10; i++) {
arr[i] = m*(i+1) % 10;
sum += arr[i];
}
long rem = k % 10;
long ans = (k/10)*sum;
for ( int i = 0; i < rem; i++) {
ans += arr[i];
}
return ans;
}
int main() {
long n = 100, m = 3;
cout<<sumOfLastDig(n,m);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static long sumOfLastDig( long n, long m)
{
long sum = 0 , k;
k = n / m;
long []arr = new long [ 10 ];
for ( int i = 0 ; i < 10 ; i++)
{
arr[i] = m * (i + 1 ) % 10 ;
sum += arr[i];
}
long rem = k % 10 ;
long ans = (k / 10 ) * sum;
for ( int i = 0 ; i < rem; i++)
{
ans += arr[i];
}
return ans;
}
public static void main (String[] args)
{
long n = 100 , m = 3 ;
System.out.println(sumOfLastDig(n, m));
}
}
|
Python3
def sumOfLastDig(n, m) :
sum = 0 ;
k = n / / m;
arr = [ 0 ] * 10 ;
for i in range ( 10 ) :
arr[i] = m * (i + 1 ) % 10 ;
sum + = arr[i];
rem = k % 10 ;
ans = (k / / 10 ) * sum ;
for i in range (rem) :
ans + = arr[i];
return ans;
if __name__ = = "__main__" :
n = 100 ; m = 3 ;
print (sumOfLastDig(n, m));
|
C#
using System;
class GFG
{
static long sumOfLastDig( long n, long m)
{
long sum = 0, k;
k = n / m;
long []arr = new long [10];
for ( int i = 0; i < 10; i++)
{
arr[i] = m * (i + 1) % 10;
sum += arr[i];
}
long rem = k % 10;
long ans = (k / 10) * sum;
for ( int i = 0; i < rem; i++)
{
ans += arr[i];
}
return ans;
}
static public void Main ()
{
long n = 100, m = 3;
Console.Write(sumOfLastDig(n, m));
}
}
|
Javascript
<script>
function sumOfLastDig(n, m)
{
let sum = 0, k;
k = parseInt(n/m);
let arr = new Array(10);
for (let i = 0; i < 10; i++) {
arr[i] = m*(i+1) % 10;
sum += arr[i];
}
let rem = k % 10;
let ans = parseInt(k/10)*sum;
for (let i = 0; i < rem; i++)
{
ans += arr[i];
}
return ans;
}
let n = 100, m = 3;
document.write(sumOfLastDig(n,m));
</script>
|
Time Complexity: O(10)
Auxiliary Space: O(10)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...