Cody Problem 46898 deals with the sum of divisors function, denoted by
, while Cody Problem 656 deals with the totient function, denoted by
. The sum of divisors is straightforward: for example,
. The totient of n counts the numbers less than n that are relatively prime to n. For example
because the greatest common divisor of 12 and four numbers (1, 5, 7, 11) is 1.
What happens if you repeatedly apply the two functions, starting with the sum of divisors and alternating? For example, start with 7. Then
and the pattern 7, 6, 12, 4 will repeat.
Oscillating behavior is plausible because the sum of divisors is always greater than n, and the totient is always smaller than n. Furthermore, because the totient has a minimum value and the sum of divisors has a maximum value, with enough iterations the sequence would have to hit a repeating pattern.
Write a function that takes an initial seed and returns the repeating pattern and the index of the sequence where the pattern begins. With an initial seed of 7, the sequence would be 7, 8, 4, 7, 6, 12, 4, 7, 6, 12,… Therefore, the repeating pattern is [7 6 12 4] and the start index is 3.
Solution Stats
Problem Comments
4 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers17
Suggested Problems
-
Find all elements less than 0 or greater than 10 and replace them with NaN
15777 Solvers
-
Find the alphabetic word product
3456 Solvers
-
Find the stride of the longest skip sequence
177 Solvers
-
(Linear) Recurrence Equations - Generalised Fibonacci-like sequences
409 Solvers
-
206 Solvers
More from this Author321
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Chris,
Would you check test17. I get a repeating pattern starting at index 161 that has 36 members.
Yes, I do too. Not sure what happened or why my reference solution worked. Thanks, David.
An interesting and challenging problem. I feel that it should be worth more than ten points...
The greatest challenge is to produce the most optimized code. Writing code that works is easy.