-2

So I need to develop a method to find the smallest digit in an integer.

CLearner
  • 63
  • 1
  • 2
  • 8
  • 1
    I think I can do mod 10 and then compare? – CLearner Oct 20 '14 at 17:45
  • @CLearner yes, you can, that's the correct approach. Now what's your base case and your recursion step? – Jon Kiparsky Oct 20 '14 at 17:59
  • As an opinion, I think you shouldn't accept an answer, until you get one which gets right and explains two things: tail recursion (even though Java still has [no guaranteed tail call optimization](http://stackoverflow.com/questions/3616483/why-does-the-jvm-still-not-support-tail-call-optimization), but just on principle and to learn this important recursion concept) and logarithmic recursion depth. Doing this thing with recursion in Java is totally pointless, except to learn these two things. – hyde Oct 20 '14 at 18:11

5 Answers5

2

Here is my implementation

public int find(int n){
    if(n < 10) return n;
    return Math.min(n%10, find(n/10));
}

You can change the int by long ...

1

If you're interested in learning how to figure this out yourself (and you should be), I would try following these steps.

  1. Do the process in your head, slowly - or even better, write the steps on paper! Take notice of each step you take.

    Step one may be: look at the first digit

  2. Consider the steps you've created. Are there parts which seem to be repeating themselves? These parts will likely be your recursive function.

  3. Rewrite the steps as a recursive function (in plain English)

  4. Translate the steps into your programming language; Java, in this case.

If you want, you can even leave the plain english steps in your code behind each line as comments, so everyone can easily follow your code

Community
  • 1
  • 1
DoubleDouble
  • 1,493
  • 10
  • 25
  • Thank you so much for suggesting me how to go about it, I thought of mod 10 and then I dint know how to compare it and get the smallest number – CLearner Oct 20 '14 at 19:00
0
public static int find(int num) {
    if(num < 10){
        return num;
    }
    int d = num % 10;
    int pmin = find(num / 10);
    return (d <= pmin) ? d : pmin;
}
afzalex
  • 8,598
  • 2
  • 34
  • 61
0

Personally I think a for loop would be quicker and easier than a recursive function. But for recursion or a for loop you need something to iterate on. Easiest way is to convert the number to a string and then iterate through it doing needed comparisons.

In your main:

int i = 578329;
String s = Integer.toString(i);
s = FindSmallest(s);

Call the function:

private String FindSmallest(String s){
    if(s.length() <= 1)
        return s;
    String sFirstChar = s.substring(0,1);
    String sSecondChar = s.substring(1,2);
    int iFirst = Integer.parseInt(sFirstChar);
    int iSecond = Integer.parseInt(sSecondChar);

    if(iFirst < iSecond)
        return FindSmallest( sFirstChar + s.substring(2));
    else
        return FindSmallest(sSecondChar + s.substring(2));
}
Mark
  • 158
  • 1
  • 2
  • 8
0

You can also write:

public static int minDigit(int n, int min){
    if(n!=0)    {
        if(n%10 < min) {
            min = n%10;
        }
        return minDigit(n/10, min);
    }
    return min;
}