First N terms whose sum of digits is a multiple of 10
Last Updated :
09 Aug, 2023
Given an integer N, the task is to print the first N terms whose sum of digits is a multiple of 10. First few terms of the series are 19, 28, 37, 46, 55, …
Examples:
Input: N = 5
Output: 19 28 37 46 55
Input: N = 10
Output: 19 28 37 46 55 64 73 82 91 109
Approach: It can be observed that to get the Nth term of the required series, find the sum of the digits of N. If the sum is already a multiple of 10 then append digit 0 in the end of N else append the minimum possible digit in the end such that the new sum of digits is a multiple of 10.
For example, to get the 19th term, since the sum of digits is already a multiple of 10 then append 0 and 190 is the 19th term of the series.
For N = 5, the minimum digit that can be appended to make the sum of digits as a multiple of 10 is 5 and 55 is the 5th term of the series.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int TEN = 10;
int digitSum( int n)
{
int sum = 0;
while (n > 0) {
sum += n % TEN;
n /= TEN;
}
return sum;
}
int getNthTerm( int n)
{
int sum = digitSum(n);
if (sum % TEN == 0)
return (n * TEN);
int extra = TEN - (sum % TEN);
return ((n * TEN) + extra);
}
void firstNTerms( int n)
{
for ( int i = 1; i <= n; i++)
cout << getNthTerm(i) << " " ;
}
int main()
{
int n = 10;
firstNTerms(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
final static int TEN = 10 ;
static int digitSum( int n)
{
int sum = 0 ;
while (n > 0 )
{
sum += n % TEN;
n /= TEN;
}
return sum;
}
static int getNthTerm( int n)
{
int sum = digitSum(n);
if (sum % TEN == 0 )
return (n * TEN);
int extra = TEN - (sum % TEN);
return ((n * TEN) + extra);
}
static void firstNTerms( int n)
{
for ( int i = 1 ; i <= n; i++)
System.out.print(getNthTerm(i) + " " );
}
public static void main (String[] args)
{
int n = 10 ;
firstNTerms(n);
}
}
|
Python3
TEN = 10
def digitSum(n):
sum = 0
while (n > 0 ):
sum + = n % TEN
n / / = TEN
return sum
def getNthTerm(n):
sum = digitSum(n)
if ( sum % TEN = = 0 ):
return (n * TEN)
extra = TEN - ( sum % TEN)
return ((n * TEN) + extra)
def firstNTerms(n):
for i in range ( 1 , n + 1 ):
print (getNthTerm(i), end = " " )
n = 10
firstNTerms(n)
|
C#
using System;
class GFG
{
readonly static int TEN = 10;
static int digitSum( int n)
{
int sum = 0;
while (n > 0)
{
sum += n % TEN;
n /= TEN;
}
return sum;
}
static int getNthTerm( int n)
{
int sum = digitSum(n);
if (sum % TEN == 0)
return (n * TEN);
int extra = TEN - (sum % TEN);
return ((n * TEN) + extra);
}
static void firstNTerms( int n)
{
for ( int i = 1; i <= n; i++)
Console.Write(getNthTerm(i) + " " );
}
public static void Main (String[] args)
{
int n = 10;
firstNTerms(n);
}
}
|
Javascript
const TEN = 10;
function digitSum(n)
{
let sum = 0;
while (n > 0) {
sum += n % TEN;
n = Math.floor(n / TEN);
}
return sum;
}
function getNthTerm(n)
{
let sum = digitSum(n);
if (sum % TEN == 0)
return (n * TEN);
let extra = TEN - (sum % TEN);
return ((n * TEN) + extra);
}
function firstNTerms(n)
{
for (let i = 1; i <= n; i++)
console.log(getNthTerm(i) + " " );
}
let n = 10;
firstNTerms(n);
|
Output
19 28 37 46 55 64 73 82 91 109
New Approach:- Another approach to solve this problem is to generate the series by iterating over all numbers from 1 to infinity and selecting only those numbers whose sum of digits is a multiple of 10.
Algorithm :-
- Define a function named digitSum that takes an integer n as input and returns the sum of its digits.
- Define a function named firstNTerms that takes an integer n as input and prints the first n terms of the required series.
- Inside the firstNTerms function, initialize a variable count to 0.
- Use a for loop to iterate over all integers from 1 to infinity until n terms whose sum of digits is a multiple of 10 are found.
- Inside the loop, call the digitSum function for the current integer I.
- If the sum of digits of i is a multiple of 10, print i and increment the count variable.
- If count reaches n, exit the loop and return.
- In the main function, call the firstNTerms function with the value of n to print the first n terms of the required series.
Here’s the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int
digitSum ( int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
void
firstNTerms ( int n)
{
int count = 0;
for ( int i = 1; count < n; i++)
{
if (digitSum (i) % 10 == 0)
{
cout << i << " " ;
count++;
}
}
}
int
main ()
{
int n = 10;
firstNTerms (n);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int digitSum( int n)
{
int sum = 0 ;
while (n > 0 ) {
sum += n % 10 ;
n /= 10 ;
}
return sum;
}
public static void firstNTerms( int n)
{
int count = 0 ;
for ( int i = 1 ; count < n; i++) {
if (digitSum(i) % 10 == 0 ) {
System.out.print(i + " " );
count++;
}
}
}
public static void main(String[] args)
{
int n = 10 ;
firstNTerms(n);
}
}
|
Python3
def digitSum(n):
sum = 0
while n > 0 :
sum + = n % 10
n / / = 10
return sum
def firstNTerms(n):
count = 0
i = 1
while count < n:
if digitSum(i) % 10 = = 0 :
print (i, end = " " )
count + = 1
i + = 1
n = 10
firstNTerms(n)
|
C#
using System;
public class GFG
{
public static int DigitSum( int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
public static void FirstNTerms( int n)
{
int count = 0;
for ( int i = 1; count < n; i++)
{
if (DigitSum(i) % 10 == 0)
{
Console.Write(i + " " );
count++;
}
}
}
public static void Main( string [] args)
{
int n = 10;
FirstNTerms(n);
}
}
|
Javascript
function digitSum(n) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
function firstNTerms(n) {
let count = 0;
for (let i = 1; count < n; i++) {
if (digitSum(i) % 10 === 0) {
console.log(i + " " );
count++;
}
}
}
const n = 10;
firstNTerms(n);
|
Output
19 28 37 46 55 64 73 82 91 109
“In this approach, we first define a function digitSum to calculate the sum of digits of a given number. Then, we iterate over all numbers from 1 to infinity and check if the sum of digits of each number is a multiple of 10. If it is, we print that number and increment the count of printed terms until we reach the desired number of terms.”
Time complexity:
The digitSum function runs in O(log10(n)) time complexity as it iterates over the digits of the number and removes one digit in each iteration.
The firstNTerms function iterates over all numbers from 1 to infinity until it finds n terms whose sum of digits is a multiple of 10. In the worst case, it may need to iterate over all integers up to 10n to find n terms, as every tenth integer has a sum of digits that is a multiple of 10. Therefore, the time complexity of this function is O(n log n).
Space complexity:
The digitSum function uses a constant amount of extra memory to store the sum variable. The firstNTerms function uses a constant amount of extra memory to store the count variable. Therefore, the space complexity of both functions is O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...