Ryan
Ryan

Reputation: 416

Method that determines if a number is 'insipid'

So I have been tasked with writing a program that determines if a number is insipid or not. Meaning, if you take a number: 57 for example, and you add the square of each digit, 5*5 + 7*7, that is now the new number:74. And you keep doing that until you either get 58 which means the number is NOT insipid, or you get 1, meaning the number is. 58 will just repeat a sequence that always ends back on 58.

So I wanted to attempt it with some basic recursion but perhaps I misunderstand the use of recursion here.

Here are the two relevant methods I wrote:

public static boolean insipid(int num){

    int dig1 = 0, dig2 = 0, dig3 = 0; // num = 159 for example, dig1 would be 1. Default is 0 in case of a 2 digit number, dig1*dig1 = 0
    if(num == 58){ //The number is not insipid
        return false;
    }
    if(num == 1){ // the number is insipid
        return true;
    }

    if (num < 10){
        insipid(num * num);
    }
    if(num>99){
        dig1 = (int)(num / 100);
        dig2 = (int)((num - 100)/10);
        dig3 = num - (((int)(num / 10))*10);
        insipid(squaresum(dig1,dig2,dig3));
    }
    else{
        dig2 = (int)(num/10); //the 10s place
        dig3 = num - (((int)(num/10)) * 10); // the 1's place
        insipid(squaresum(dig1, dig2,dig3)); //dig1 = 0 so I just pass it along with it.
    }
}

public static int squaresum(int dig1, int dig2, int dig3){
    //Returns the sum of three digits squared.
    return (dig1 * dig1) + (dig2 * dig2) + (dig3 + dig3);
}

It gives me an error saying that Insipid() must return a boolean whenever I supply it a number. But I know for any number given it will always either eventually resolve to 58 or 1. So shouldn't true or false always eventually be returned so a boolean is returned and the error is invalid? Obviously this is not the case, but how I see it. Is it the use of the recursion here that is invalid?

Also, if you have any suggestions on how I might clean this up I wouldn't mind harsh criticism, my java is not very good.

Upvotes: 0

Views: 754

Answers (3)

j_random_hacker
j_random_hacker

Reputation: 51246

In addition to Yogendra Singh's answer, you're calculating digits incorrectly: e.g. with

dig2 = (int)((num -100)/10);

you're assuming that the hundreds digit is 1. Try

dig2 = (int)((num - dig1 * 100)/10);

and similarly for dig3.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245449

If you went with your solution, the only thing missing is the return statements from the recursive calls to incipid.

Upvotes: 2

Yogendra Singh
Yogendra Singh

Reputation: 34367

Update you insipid as below. A method declared with return must return values in all cases. Your method doesn't return any value if it reach second else i.e. if number is anything other than 1 & 58.

public static boolean insipid(int num){
  boolean returnValue = false;
  if(num == 58){ //The number is not insipid
   //do nothing
  } if(num == 1){ // the number is insipid
    returnValue = true;
  }else{
    int dig1 = 0, dig2 = 0, dig3 = 0; // num = 159 for example, dig1 would be 1. Default is 0 in case of a 2   digit number, dig1*dig1 = 0
    if (num < 10){
        returnValue = insipid(num * num);
    }
    else if(num>99){
        dig1 = (int)(num / 100);
        dig2 = (int)((num - 100)/10);
        dig3 = num - (((int)(num / 10))*10);
        returnValue = insipid(squaresum(dig1,dig2,dig3));
    }
    else{
        dig2 = (int)(num/10); //The 10s place
        dig3 = num - (((int)(num/10)) * 10); //the 1s place
        returnValue = insipid(squaresum(dig1, dig2,dig3)); //dig1 = 0 so I just pass it along with it.
    }
  }
  return returnValue ;
}

Please Note: I just tried to answer your issue regarding return issue ONLY.

Upvotes: 1

Related Questions