Sum of integers upto N with given unit digit
Last Updated :
11 Sep, 2023
Given two integer N and D, 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
Naive approach:
- Traverse from 1 to N.
- If the unit digit of the number is D add the number to the sum.
- Finally print the value of sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll getSum( int n, int d)
{
ll sum = 0;
for ( int i = 1; i <= n; i++) {
if (i % 10 == d)
sum += i;
}
return sum;
}
int main()
{
int n = 30, d = 3;
cout << getSum(n, d);
return 0;
}
|
Java
import java.util.*;
class solution
{
static long getSum( int n, int d)
{
long sum = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (i % 10 == d)
sum += i;
}
return sum;
}
public static void main(String args[])
{
int n = 30 , d = 3 ;
System.out.println(getSum(n, d));
}
}
|
Python3
def getSum(n, d) :
sum = 0 ;
for i in range (n + 1 ) :
if (i % 10 = = d) :
sum + = i
return sum
if __name__ = = "__main__" :
n , d = 30 , 3
print (getSum(n, d))
|
C#
using System;
class gfg
{
public static int getSum( int n, int d)
{
int sum = 0;
for ( int i = 1; i <= n; i++) {
if (i % 10 == d)
sum += i;
}
return sum;
}
public static int Main()
{
int n = 30, d = 3;
Console.WriteLine( getSum(n, d));
return 0;
}
}
|
PHP
<?php
function getSum( $n , $d )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $i % 10 == $d )
$sum += $i ;
}
return $sum ;
}
$n = 30;
$d = 3;
echo getSum( $n , $d );
?>
|
Javascript
<script>
function getSum(n, d)
{
let sum = 0;
for (let i = 1; i <= n; i++)
{
if (i % 10 == d)
sum += i;
}
return sum;
}
let n = 30;
let d = 3;
document.write( getSum(n, d));
</script>
|
Time Complexity: O(n) since one traversal of the loop till the number is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) since no extra array is used, the space taken by the algorithm is constant
Efficient approach: While D < N update sum = sum + D and D = D + 10. Print the sum in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll getSum( int n, int d)
{
ll sum = 0;
while (d <= n)
{
sum += d;
d += 10;
}
return sum;
}
int main()
{
int n = 30, d = 3;
cout << getSum(n, d);
return 0;
}
|
Java
class Solution
{
static long getSum( int n, int d)
{
long sum = 0 ;
while (d <= n) {
sum += d;
d += 10 ;
}
return sum;
}
public static void main(String args[])
{
int n = 30 , d = 3 ;
System.out.print(getSum(n, d));
}
}
|
Python3
def getSum(n, d):
sum = 0
while (d < = n):
sum + = d
d + = 10
return sum
n = 30
d = 3
print (getSum(n, d))
|
C#
using System;
class GFG
{
static long getSum( int n, int d)
{
long sum = 0;
while (d <= n)
{
sum += d;
d += 10;
}
return sum;
}
public static void Main()
{
int n = 30, d = 3;
Console.Write(getSum(n, d));
}
}
|
PHP
<?php
function getSum( $n , $d )
{
$sum = 0;
while ( $d <= $n )
{
$sum += $d ;
$d += 10;
}
return $sum ;
}
$n = 30; $d = 3;
echo (getSum( $n , $d ));
?>
|
Javascript
<script>
function getSum(n, d)
{
let sum = 0;
while (d <= n)
{
sum += d;
d += 10;
}
return sum;
}
let n = 30;
let d = 3;
document.write(getSum(n, d));
</script>
|
Time Complexity: O(n) since one traversal of the loop till the number is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...