Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod that determines if a number is 'insipid'
    text
    copied!<p>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. </p> <p>So I wanted to attempt it with some basic recursion but perhaps I misunderstand the use of recursion here.</p> <p>Here are the two relevant methods I wrote:</p> <pre><code>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 &lt; 10){ insipid(num * num); } if(num&gt;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); } </code></pre> <p>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 <em>always</em> 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?</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload