Cody

Problem 21. Return the 3n+1 sequence for n

Solution 2089343

Submitted on 12 Jan 2020 by Ishan Fernando
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
n = 1; c_correct = 1; assert(isequal(collatz(n),c_correct))

array = 1 count = 2 number = 1 c = 1

2   Pass
n = 2; c_correct = [2 1]; assert(isequal(collatz(n),c_correct))

array = 2 count = 2 number = 2 newnumb = 1 count = 3 number = 1 c = 2 1

3   Pass
n = 5; c_correct = [5 16 8 4 2 1]; assert(isequal(collatz(n),c_correct))

array = 5 count = 2 number = 5 newnumb = 16 count = 3 number = 16 newnumb = 8 count = 4 number = 8 newnumb = 4 count = 5 number = 4 newnumb = 2 count = 6 number = 2 newnumb = 1 count = 7 number = 1 c = 5 16 8 4 2 1

4   Pass
n = 22; c_correct = [22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1]; assert(isequal(collatz(n),c_correct))

array = 22 count = 2 number = 22 newnumb = 11 count = 3 number = 11 newnumb = 34 count = 4 number = 34 newnumb = 17 count = 5 number = 17 newnumb = 52 count = 6 number = 52 newnumb = 26 count = 7 number = 26 newnumb = 13 count = 8 number = 13 newnumb = 40 count = 9 number = 40 newnumb = 20 count = 10 number = 20 newnumb = 10 count = 11 number = 10 newnumb = 5 count = 12 number = 5 newnumb = 16 count = 13 number = 16 newnumb = 8 count = 14 number = 8 newnumb = 4 count = 15 number = 4 newnumb = 2 count = 16 number = 2 newnumb = 1 count = 17 number = 1 c = 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Suggested Problems

More from this Author95

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!