id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

peter@norvig.com numbers > longest_collatz
prev  |  next  |  chance

Given an integer n, find the longest Collatz sequence starting with any integer from 1 up to (but not including) n. (As discussed in a previous exercise, A Collatz sequence is a sequence of numbers starting with n where the next element is n/2 if n is even and 3*n + 1 if n is odd; we end the sequence when it reaches 1.)


longest_collatz(2) → [1]
longest_collatz(4) → [3, 10, 5, 16, 8, 4, 2, 1]
longest_collatz(10) → [9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]

...Save, Compile, Run (ctrl-enter)

def longest_collatz(n):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 241 Post-solution available

Copyright Nick Parlante 2017 - privacy