28 ms·
Also, the interviewer didn't even know his own question. As I understand it, you are taking the sequence n -> (sum of the squares of the digits of n) First,
by fsk 11y ago
Also, the interviewer didn't even know his own question.
As I understand it, you are taking the sequence
n -> (sum of the squares of the digits of n)
First, the sequence never diverges to infinity. For example, 9999999999 -> 9*81.
Second, not all numbers converge to 1. There is a stable cycle that does not include one.
Consider
4 -> 16 -> 37 -> 56 -> 61 -> 37 -> ...
So, any positive integer either
1. Converges to 1
2. Converges to the stable cycle I just mentioned above.
So the interviewer should be fired for not understanding the question he was asking.
- 6d0debc071 11y agoInterviewer said it would converge either to one or infinitely: 'The next sentence of English is where the problem started. I _thought_ he said: “The sequence will either go to one or to infinity.” but he actually said: “The sequence will either go to one or infinite_ly_.”' [My emphasis] (Firing's also a bit harsh for getting an interview question wrong)
- fsk 11y agoThat also is incorrect - the sequence either converges to one or a stable cycle - not infinitely. That's also a horrible question for illustrating functional programming style. It would be much more efficient to have a simple loop caching the array n->f(n) and then finding the ones that converge to 1.
- deleted 11y ago[deleted]
- int3 11y ago... the interviewee misunderstood the question. The interviewer understood it just fine.
- deleted 11y ago[deleted]