public static int luckySevens( long number ) { if(number == 7) return 1; if(number == 0) return 0; return number + luckySevens(number - 1); } } Does this code make sense?
Jack_Prism
Jack_Prism
@Jack_Prism
Well this is a very open ended question, it is merely asking whether it makes sense or not to you
public class Lucky7s { public static void main(String [] args) { System.out.println(luckySevens(7)); } /* luckySevens will return a count of the 7s in the number * 1087171 would return 2 as there are 2 7s * 1077171 would return 15 as 2 7s side by side count as 14 + 1 = 15 * 77077 would return 28 ( 14 + 14 ) * 97171771707797 would return 32 ( 1 + 1 + 14 + 1 + 14 + 1 ) * 1089651234 would return 0 * the solution to this problem must use recursion */ public static int luckySevens( long number ) { if(number == 7) return 1; if(number == 0) return 0; return number + luckySevens(number - 1); } }
I'm supposed to do this
@Jack_Prism
Im looking, but this makes absolutely no sense to me
hmm ok
its a java program
I apologize for my lack of knowledge in this category
its ok
One possible solution I have in mind, with the provided assumptions that -recursion simply defined is a method that calls itself -numbers with groups of 7's greater than or equal to 3 wont be tested, and simply put, one 7 = 1, two 7 = 14 (call this assumption a1) -(a possible variation of the above assumption, could be to assume the mathematical rule is `numSevens*(7^(numSevens-1)))` where numSevens == number of 7's in a given group) (call this assumption a2) -that the method luckySevens is not restricted to only accepting one argument/parameter is to turn the number into a String, then start a loop through all of the characters, and stop the loop when the first instance of `'7'` is found. If it's never found, return 0. If we go with assumption a1, then determine whether the 7 is alone or in a group of 2 (while making sure not to throw an IndexOutOfBoundException), then return the value of the 7 group + a recursive call `luckySevens(number, startPositionWhereILeftOff)`, where the 2nd argument indicates what position in the number that the loop should start. The start position should initially be 0, and when called from inside the method, it should make sure to pick up after the '7' or group of '77'. In essence, this recursive method would be a single loop that calculates the value of a 7 group, stops, then recursively calls itself to look for the next 7 group etc. Going with the a2 assumption wouldn't change much, other than performing an inner-loop once you find the first instance of a '7', to figure out the group size, before calculating the value of the 7 group and making another recursive call. I'm a noob in terms of recursive programming, and is a rather messy and entertaining solution imo, I'm curious to see what other recursive solutions are. The problem for me over all would be much simpler without recursion, however not to say that it's a bad thing, since challenges are good opportunities. Though still, my listed approach is valid, provided the listed assumptions. (you might want to clarify them, lest the assumptions result in point deductions). I have the approach running as a proof of concept in the a1 and a2 variations, to give you more confidence. I think the approach I listed gives you a significant foundation from which to approach the problem, to wrestle, internalize and understand the concepts, in an endeavor that may prompt you to learn new aspects of programming and problem solving.
Recursion isn't all that complicated. We've all been doing it since second grade (or first). It's usually taught, in my opinion, wrong. A recursive function solves a part of the problem, then calls itself with the remaining data and solves another part of the problem and so on until the whole problem is solved. What we all learned in first or second grade is: addition with pen and paper. 784 + 256 = ? First we put the numbers in columns. Then we add the rightmost column. If it's over 10 we carry the 1 along. Repeat until we've added all the columns and handled any leftover carries. Voila, recursion!
Interesting perspective, I like the way you put it. With my original approach to the problem, I feel like I was using recursion to iterate through a string's characters. Thinking of another approach, I could say ``` function luckySevens(num) { if(consistsOnlyOfSevens(num)) { var valueOfThisSevenGroup = calculateValueOfSevenGroup(num); return valueOfThisSevenGroup; } else { var sumOfAll7Groups = 0; List listOfDifferent7Groups = splitNumberIntoList7Groups(num); //input of 177871117 will become a list of [77, 7, 7] for(var i = 0; i < listOfDifferent7Groups.length; i++) { sumOfAll7Groups = sumOfAll7Groups + luckySevens( listOfDifferent7Groups.getElementAt(i)); } return sumOfAll7Groups; } } ``` which basically splits the inputted number into a list of connected 7s. Then it recursively calls itself to calculate the value of each element (7 group) in the list, adding each result to the sumOfAll7Groups variable before returning the grand sum of the 7 groups. Though that example feels more like it's just calling on a different method to calculate the value of each 7 group under the guise of recursion. And never goes deeper than a method call inside of a method call, if that matters. I'm not positive that my approaches qualify / are mathematically pure enough, though maybe they are.
Join our real-time social learning platform and learn together with your friends!