Open In App

Find M-th number whose repeated sum of digits of a number is N

Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers N and M, The task is to find the M-th number whose sum of digits of a number until the sum becomes a single digit is N.

Examples: 

Input: N = 1, M = 3
Output: 19 
The first two numbers are 1 and 10.

Input: N = 2, M = 5
Output:  38 
The first four numbers are 2, 11, 20, and 29.

A naive approach is to iterate for all numbers and keep a count of numbers whose sum returns N. 

  • Create a function singleDigit that takes an integer as input, and returns the single-digit number obtained by summing up the digits of the input number and repeating until the sum is a single-digit number.
  • Define a function findMthNum which takes two inputs, N and M
  • Initialize a variable count to 0
  • Initialize a variable i to 1
  • Use while loop, while true
    • calculate the single digit number of i using function singleDigit
    • check if the single-digit number is equal to N, if yes, increment the count by 1
    • check if the count is equal to M, if yes, return i
    • increment i

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
// function to calculate the sum of digits of the number
int singleDigit(long long n)
{
    int result;
 
    while (true) {
        string s = to_string(n);
        int sum = 0;
        for (auto i : s)
            sum += (i - '0');
 
        if (sum > 9) {
            n = sum;
        }
        else {
            result = sum;
            break;
        }
    }
    return result;
}
 
int findMthNum(int N, int M)
{
    int count = 0;
    int i = 1;
    while (true) {
        int sum = singleDigit(i);
        if (sum == N) {
            count++;
        }
 
        if (count == M) {
            return i;
        }
        i++;
    }
}
 
int main()
{
    int N = 2;
    int M = 5;
    cout << "Mth number whose sum of digits is " << N
         << " is : " << findMthNum(N, M);
    return 0;
}


Java




import java.io.*;
import java.util.*;
 
public class Gfg {
    // function to calculate the sum of digits of the number
    public static int singleDigit(long n)
    {
        int result;
 
        while (true) {
            String s = String.valueOf(n);
            int sum = 0;
            for (int i = 0; i < s.length(); i++) {
                sum += (s.charAt(i) - '0');
            }
 
            if (sum > 9) {
                n = sum;
            }
            else {
                result = sum;
                break;
            }
        }
        return result;
    }
 
    public static int findMthNum(int N, int M)
    {
        int count = 0;
        int i = 1;
        while (true) {
            int sum = singleDigit(i);
            if (sum == N) {
                count++;
            }
 
            if (count == M) {
                return i;
            }
            i++;
        }
    }
 
    public static void main(String[] args)
    {
        int N = 2;
        int M = 5;
        System.out.println(
            "Mth number whose sum of digits is " + N
            + " is : " + findMthNum(N, M));
    }
}


Python3




# function to calculate the sum of digits of the number
def singleDigit(n):
     
    result = 0
 
    while (1):
        s = str(n)
        sum = 0
        for i in range(0, len(s)):
            sum = sum + int(s[i])
 
        if (sum > 9):
            n = sum
        else:
            result = sum
            break
             
    return result
 
def findMthNum(N, M):
     
    count = 0
    i = 1
    while (1):
        sum = singleDigit(i)
        if (sum == N):
            count = count + 1
 
        if (count == M):
            return i
        i = i + 1
 
N = 2
M = 5
print("Mth number whose sum of digits is ", N, " is : ", findMthNum(N, M));
 
# The code is contributed by Gautam goel.


C#




using System;
 
 
class HelloWorld {
     
    // function to calculate the sum of digits of the number
    public static int singleDigit(int n)
    {
        int result = 0;
 
        while (true) {
            string s = n.ToString();
            int sum = 0;
            for(int i = 0; i < s.Length; i++){
                sum += (s[i] - '0');
            }
 
            if (sum > 9) {
                n = sum;
            }
            else {
                result = sum;
                break;
            }
        }
        return result;
    }
     
    public static int findMthNum(int N, int M)
    {
        int count = 0;
        int i = 1;
        while (true) {
            int sum = singleDigit(i);
            if (sum == N) {
                count = count + 1;
            }
 
            if (count == M) {
                return i;
            }
            i = i + 1;
        }
    }
     
    static void Main() {
        int N = 2;
        int M = 5;
         
        Console.WriteLine("Mth number whose sum of digits is " + N + " is : " + findMthNum(N, M));
    }
}
 
// The code is contributed by Gautam goel.


Javascript




// function to calculate the sum of digits of the number
function singleDigit(n)
{
    let result;
 
    while (true) {
        let s = n.toString();
        let sum = 0;
        for (let i of s)
            sum += parseInt(i);
 
        if (sum > 9) {
            n = sum;
        }
        else {
            result = sum;
            break;
        }
    }
    return result;
}
 
function findMthNum( N, M)
{
    let count = 0;
    let i = 1;
    while (true) {
        let sum = singleDigit(i);
        if (sum == N) {
            count++;
        }
 
        if (count == M) {
            return i;
        }
        i++;
    }
}
 
let N = 2;
let M = 5;
console.log("Mth number whose sum of digits is " + N
     + " is : " + findMthNum(N, M));


Output

Mth number whose sum of digits is 2 is : 38

Time Complexity: O(N*M), where N is the number whose sum of digits is being calculated and M is the number of digits in the input number.
Auxiliary Space: O(1), as it uses only a constant amount of memory.

An efficient approach is to find the summation of digits till it becomes single digits in O(1) which has been discussed here. Hence the formula to find the M-th number will be: 

Mth number: (M-1)*9 + N

Below is the implementation of the above approach: 

C++




// C++ program to Find m-th number whose
// sum of digits of a number until
// sum becomes single digit is N
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the M-th
// number whosesum till one digit is N
int findNumber(int n, int m)
{
    int num = (m - 1) * 9 + n;
    return num;
}
 
// Driver Code
int main()
{
 
    int n = 2, m = 5;
    cout << findNumber(n, m);
    return 0;
}


Java




// Java program to Find m-th number whose
// sum of digits of a number until
// sum becomes single digit is N
import java.io.*;
public class GFG
{
 
// Function to find the M-th
// number whosesum till one digit is N
static int findNumber(int n, int m)
{
    int num = (m - 1) * 9 + n;
    return num;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 2, m = 5;
    System.out.print(findNumber(n, m));
}
}
 
// This code is contributed
// by Akanksha Rai


Python3




# Python3 program to Find m-th number
# whose sum of digits of a number
# until sum becomes single digit is N
 
# Function to find the M-th
# number whosesum till one digit is N
def findNumber(n, m) :
     
    num = (m - 1) * 9 + n;
    return num;
 
# Driver Code
if __name__ == "__main__" :
 
    n = 2 ;
    m = 5 ;
    print(findNumber(n, m))
     
# This code is contributed by Ryuga


C#




// C# program to Find m-th number whose
// sum of digits of a number until
// sum becomes single digit is N
using System;
 
class GFG
{
 
// Function to find the M-th
// number whosesum till one digit is N
static int findNumber(int n, int m)
{
    int num = (m - 1) * 9 + n;
    return num;
}
 
// Driver Code
public static void Main()
{
    int n = 2, m = 5;
    Console.Write(findNumber(n, m));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP




<?php
// PHP program to Find m-th number whose
// sum of digits of a number until
// sum becomes single digit is N
 
// number whosesum till one digit is N
function findNumber($n, $m)
{
    $num = ($m - 1) * 9 + $n;
    return $num;
}
 
// Driver Code
$n = 2; $m = 5;
echo findNumber($n, $m);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript




<script>
 
// JavaScript program to Find m-th number whose
// sum of digits of a number until
// sum becomes single digit is N   
 
    // Function to find the M-th
    // number whosesum till one digit is N
    function findNumber(n , m) {
        var num = (m - 1) * 9 + n;
        return num;
    }
 
    // Driver Code
     
        var n = 2, m = 5;
        document.write(findNumber(n, m));
 
// This code contributed by Rajput-Ji
 
</script>


Output

38

Time Complexity: O(1), as we are doing constant time operations without using any loops or recursion.
Auxiliary Space: O(1), as we are not using any extra space.



Last Updated : 02 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads