n-th number whose sum of digits is ten

Given an integer value n, find out the n-th positive integer whose sum is 10.

Examples:

Input : n = 2
Output : 28
The first number with sum of digits as
10 is 19. Second number is 28.

Input : 15
Output : 154



Method 1 (Simple):
We traverse through all numbers. For every number, we find the sum of digits. We stop when we find the n-th number with sum of digits as 10.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
using namespace std;
  
int findNth(int n)
{
    int count = 0;
  
    for (int curr = 1;; curr++) {
  
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
  
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
  
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
  
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find n-th number
// with sum of digits as 10.
  
import java.util.*;
import java.lang.*;
  
public class GFG {
    public static int findNth(int n)
    {
        int count = 0;
        for (int curr = 1;; curr++) {
  
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
  
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
  
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
  
    public static void main(String[] args)
    {
        System.out.print(findNth(5));
    }
}
  
// Contributed by _omg

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find n-th number
# with sum of digits as 10.
import itertools
  
# function to find required number
def findNth(n):
  
    count = 0
  
    for curr in itertools.count():
        # Find sum of digits in current no.
        sum = 0
        x = curr
        while(x):
            sum = sum + x % 10
            x = x // 10
  
        # If sum is 10, we increment count
        if (sum == 10):
            count = count + 1
  
        # If count becomes n, we return current
        # number.
        if (count == n):
            return curr
  
    return -1
  
# Driver program
if __name__=='__main__':
    print(findNth(5))
  
# This code is contributed by
# Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find n-th number
// with sum of digits as 10.
using System;
  
class GFG {
    public static int findNth(int n)
    {
        int count = 0;
        for (int curr = 1;; curr++) {
  
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
  
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
  
            // If count becomes n, we
            // return current number.
            if (count == n)
                return curr;
        }
    }
  
    // Driver Code
    static public void Main()
    {
        Console.WriteLine(findNth(5));
    }
}
  
// This code is contributed
// by Sach_Code

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Simple PHP program to find n-th 
// number with sum of digits as 10. 
function findNth($n
    $count = 0; 
  
    for ($curr = 1; ; $curr++)
    
  
        // Find sum of digits in 
        // current no. 
        $sum = 0; 
        for ($x = $curr;
             $x > 0; $x = $x / 10) 
            $sum = $sum + $x % 10; 
  
        // If sum is 10, we increment 
        // count 
        if ($sum == 10) 
            $count++; 
  
        // If count becomes n, we return 
        // current number. 
        if ($count == $n
            return $curr
    
    return -1; 
  
// Driver Code
echo findNth(5); 
  
// This code is contributed by Sach .
?>

chevron_right


Output:

55

Method 2 (Efficient):
If we take a closer look, we can notice that all multiples of 9 are present in arithmetic progression 19, 28, 37, 46, 55, 64, 73, 82, 91, 100, 109, ….
However, there are numbers in above series whose sum of digits is not 10, for example, 100. So instead of checking one by one, we start with 19 and increment by 9.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
using namespace std;
  
int findNth(int n)
{
    int count = 0;
  
    for (int curr = 19;; curr += 9) {
  
        // Find sum of digits in current no.
        int sum = 0;
        for (int x = curr; x > 0; x = x / 10)
            sum = sum + x % 10;
  
        // If sum is 10, we increment count
        if (sum == 10)
            count++;
  
        // If count becomes n, we return current
        // number.
        if (count == n)
            return curr;
    }
    return -1;
}
  
int main()
{
    printf("%d\n", findNth(5));
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find n-th number
// with sum of digits as 10.
  
import java.util.*;
import java.lang.*;
  
public class GFG {
    public static int findNth(int n)
    {
        int count = 0;
  
        for (int curr = 19;; curr += 9) {
  
            // Find sum of digits in current no.
            int sum = 0;
            for (int x = curr; x > 0; x = x / 10)
                sum = sum + x % 10;
  
            // If sum is 10, we increment count
            if (sum == 10)
                count++;
  
            // If count becomes n, we return current
            // number.
            if (count == n)
                return curr;
        }
    }
  
    public static void main(String[] args)
    {
        System.out.print(findNth(5));
    }
}
  
// Contributed by _omg

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find n-th 
# number with sum of digits as 10. 
def findNth(n): 
    count = 0;
      
    curr = 19;
  
    while (True): 
  
        # Find sum of digits in
        # current no. 
        sum = 0;
        x = curr;
        while (x > 0):
            sum = sum + x % 10;
            x = int(x / 10);
  
        # If sum is 10, we increment
        # count 
        if (sum == 10): 
            count+= 1
  
        # If count becomes n, we return 
        # current number. 
        if (count == n): 
            return curr;
          
        curr += 9;
  
    return -1
  
# Driver Code
print(findNth(5)); 
  
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find n-th number
// with sum of digits as 10.
using System;
  
class GFG {
    public static int findNth(int n)
    {
        int count = 0;
  
        for (int curr = 19;; curr += 9) {
  
            // Find sum of digits in
            // current no.
            int sum = 0;
            for (int x = curr;
                 x > 0; x = x / 10)
                sum = sum + x % 10;
  
            // If sum is 10, we increment
            // count
            if (sum == 10)
                count++;
  
            // If count becomes n, we return
            // current number.
            if (count == n)
                return curr;
        }
    }
  
    // Driver Code
    static public void Main()
    {
        Console.WriteLine(findNth(5));
    }
}
  
// This code is contributed
// by Sach_Code

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// Simple PHP program to find n-th  
// number with sum of digits as 10. 
function findNth($n
    $count = 0; 
  
    for ($curr = 19; ;$curr += 9) 
    
  
        // Find sum of digits in
        // current no. 
        $sum = 0; 
        for ($x = $curr; $x > 0;
             $x = (int)$x / 10) 
            $sum = $sum + $x % 10; 
  
        // If sum is 10, we increment
        // count 
        if ($sum == 10) 
            $count++; 
  
        // If count becomes n, we return 
        // current number. 
        if ($count == $n
            return $curr
    
    return -1; 
  
// Driver Code
echo findNth(5); 
  
// This code is contributed 
// by Sach_Code
?>

chevron_right


Output:

55

Method 3 (Constant Space and Constant Time):

If we see the List closely, we will find out that it is an AP where a = 19 and d = 9. But it has got some outliers too which are in multiples of 10 and starts from 100. So the sequence of outliers is something like – {100, 1000, 10000, …}

Now if we want to find the nth perfect number, it should be (nth number from AP described above + 9 * number of outliers). Now let’s have a look at how to find the number of outliers. Let’s have a close look at the values returned by the Log10 function for a value n and number of outliers till n –

n Log10 Number of Outliers
10 1 0
100 2 1
1000 3 2

So at any point, if we subtract 1 from the log10 of the nth element and cast the result into an integer, we will get the number of outliers.

Following code implements the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find n-th number
// with sum of digits as 10.
#include <bits/stdc++.h>
  
using namespace std;
  
int findNth(int n)
{
    int nthElement = 19 + (n - 1) * 9;
    int outliersCount = (int)log10(nthElement) - 1;
  
    // find the nth perfect number
    nthElement += 9 * outliersCount;
    return nthElement;
}
  
int main()
{
    cout << findNth(5) << endl;
    return 0;
}
  
// This code is contributed by Rituraj Jain

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find n-th number
// with sum of digits as 10.
import java.lang.Math;
class GFG {
  
    public static int findNth(int n)
    {
        int nthElement = 19 + (n - 1) * 9;
        int outliersCount = (int)Math.log10(nthElement) - 1;
  
        // find the nth perfect number
        nthElement += 9 * outliersCount;
        return nthElement;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(findNth(5));
    }
}
  
// This code is contributed by
// Code_Mech

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the n-th number 
# with the sum of digits as 10. 
import math
  
def findNth(n): 
  
    nthElement = 19 + (n - 1) * 9
    outliersCount = int(math.log10(nthElement)) - 1
  
    # find the nth perfect number 
    nthElement += 9 * outliersCount 
    return nthElement 
  
# Driver Code 
if __name__ == "__main__"
  
    print(findNth(5)) 
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find n-th number
// with sum of digits as 10.
using System;
  
class GFG {
  
    public static int findNth(int n)
    {
        int nthElement = 19 + (n - 1) * 9;
        int outliersCount = (int)Math.Log10(nthElement) - 1;
  
        // find the nth perfect number
        nthElement += 9 * outliersCount;
        return nthElement;
    }
  
    // Driver Code
    static public void Main()
    {
        Console.WriteLine(findNth(5));
    }
}
  
// This code is contributed by
// Ankit Bhardwaj

chevron_right


Output:

55


My Personal Notes arrow_drop_up

good platform for learning

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.