6 kyu

Collatz

1,523 of 5,274trent.armstrong

Description:

Preface

A collatz sequence, starting with a positive integern, is found by repeatedly applying the following function to n until n == 1 :

f(n)={n/2, if n is even3n+1, if n is oddf(n) = \begin{cases} n/2, \text{ if $n$ is even} \\ 3n+1, \text{ if $n$ is odd} \end{cases}


A more detailed description of the collatz conjecture may be found on Wikipedia.

The Problem

Create a function collatz that returns a collatz sequence string starting with the positive integer argument passed into the function, in the following form:

"X0->X1->...->XN"

Where Xi is each iteration of the sequence and N is the length of the sequence.

Sample Input

Input: 4
Output: "4->2->1"

Input: 3
Output: "3->10->5->16->8->4->2->1"

Don't worry about invalid input. Arguments passed into the function are guaranteed to be valid integers >= 1.

Number Theory
Algorithms

Stats:

CreatedNov 15, 2013
PublishedNov 16, 2013
Warriors Trained7039
Total Skips317
Total Code Submissions13691
Total Times Completed5274
JavaScript Completions1523
Haskell Completions294
Ruby Completions319
Python Completions1845
Elixir Completions198
C# Completions388
Java Completions585
C Completions175
Factor Completions11
Rust Completions107
Julia Completions21
CoffeeScript Completions12
Crystal Completions10
Go Completions90
Scala Completions19
Total Stars103
% of votes with a positive feedback rating94% of 739
Total "Very Satisfied" Votes654
Total "Somewhat Satisfied" Votes80
Total "Not Satisfied" Votes5
Total Rank Assessments6
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • trent.armstrong Avatar
  • jhoffner Avatar
  • ZozoFouchtra Avatar
  • bkaes Avatar
  • GiacomoSorbi Avatar
  • SpaceBison Avatar
  • kazk Avatar
  • user5036852 Avatar
  • klippx Avatar
  • Voile Avatar
  • Souzooka Avatar
  • rowcased Avatar
  • FArekkusu Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • LegendaryFartMaster Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
  • Kacarott Avatar
  • it1shka Avatar
  • Just4FunCoder Avatar
  • Sigmanificient Avatar
Ad