Hofstadter's Q -Sequence ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Hofstadter's Q -Sequence ?

I am getting bizarre results with what seems like an easy program to write. For n>35, I *usually* get “no output,” though sometimes it will correctly give me the answer, eg 19 for n=36. Is this a memory issue—or is there some limit on recursion I’m not grasping? https://code.sololearn.com/czp6KDt3yw7r/?ref=app

2nd Apr 2022, 4:27 AM
JRS
JRS - avatar
20 Answers
+ 4
Yes, it might be a time issue since the program has a limited time frame to execute. Try using some means of caching results (memoization) to cut the number of recursions.
2nd Apr 2022, 4:40 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 3
JRS Actually in Python or C# we can use dictionary based objects which make the task easier
2nd Apr 2022, 11:52 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Bob_Li thank you
3rd Apr 2022, 5:13 PM
Harsha S
Harsha S - avatar
+ 1
It's probably a timeout issue. Sololearn times out pretty quickly. Have you tried running the code somewhere else?
2nd Apr 2022, 4:38 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Not dumb. Awareness for the efficiency of an algorithm is important. Now try to make it work for any n >70 and find the next limit...
2nd Apr 2022, 5:35 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Simon Sauter It can be solved. It just has to be solved without using recursion.
2nd Apr 2022, 9:30 AM
Jan
Jan - avatar
+ 1
JRS Try using an array, and populate it sequentially. Then get the value of the last populated array element.
2nd Apr 2022, 4:51 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 1
JRS If all else fails, Google is your friend. Here is a solution that works and passes the challenge. It basically just precomputes the array, then you just find the answer by it's index. The sequence may be hard to predict, but easy to simulate. Directly computing the answer is computationaly expensive for large values, but the sequence is relatively easy to generate without using recursion. https://code.sololearn.com/c8ZnDtoL15Xp/?ref=app
3rd Apr 2022, 4:26 AM
Bob_Li
Bob_Li - avatar
+ 1
JRS It is not a Sololearn problem, it's just the nature of recursive functions... they blow up. You just have to find another way,
3rd Apr 2022, 7:03 AM
Bob_Li
Bob_Li - avatar
+ 1
can i get the problem statement?
3rd Apr 2022, 12:44 PM
Harsha S
Harsha S - avatar
+ 1
Harsha S It's a code coach challenge. Hofstadter's Q-Sequence is a sequence of numbers where every integer above zero has a corresponding q-sequence value. You can determine the q-sequence value from a formula that tells you how far back in the sequence to go and add two values together. The first two values of the sequence are Q(1) = 1 and Q(2) = 1, and every number above 2 can be expressed according to the following formula (where n is your input value): Q(n) = Q(n - Q(n - 1)) + Q(n - Q(n -2)) Task: Given an integer value input, determine and output the corresponding q-sequence value. Input Format: A positive integer value. Output Format: A positive integer value that represents the value in the q-sequence that the input holds. Sample Input: 5 Sample Output: 3
3rd Apr 2022, 2:24 PM
Bob_Li
Bob_Li - avatar
+ 1
So in the real world, should recursive functions be avoided?
3rd Apr 2022, 4:32 PM
JRS
JRS - avatar
+ 1
JRS The problem with those recursive functions is that they create an overflow on the stack.
3rd Apr 2022, 6:21 PM
Jan
Jan - avatar
0
Hmm, okay, I will try that, thanks!
2nd Apr 2022, 4:45 AM
JRS
JRS - avatar
0
Right, but try 37. Or 50.
2nd Apr 2022, 5:20 AM
JRS
JRS - avatar
0
I just tried using an array with the first 70 q values…works fine for n<=70. So it is a timing issue I guess. Dumb.
2nd Apr 2022, 5:22 AM
JRS
JRS - avatar
0
I meant it’s dumb that sololearn works that way. This is a code challenge for those of us who are beginners—and I did have the code correct, but couldn’t pass the challenge because their program can’t be bothered to wait another half a second or whatever. Seems silly that their code-checking software returns a false negative. That’s all.
2nd Apr 2022, 5:45 AM
JRS
JRS - avatar
0
I understand :)
2nd Apr 2022, 6:08 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
If this is a sololearn code challenge and it can't be solved because of this you can send a bug report to [email protected].
2nd Apr 2022, 6:11 AM
Simon Sauter
Simon Sauter - avatar
0
JRS if it results in problems like this, yes.
3rd Apr 2022, 5:55 PM
Bob_Li
Bob_Li - avatar