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

 

davereed@creighton.edu > hailstone
prev  |  next  |  chance

hailstone() - An interesting unsolved problem in mathematics concerns what is called the hailstone sequence of integers. This sequence is defined as follows: start with any integer. If that number is odd, then multiply it by 3 and add 1. If it is even, then divide it by 2. Now, repeat. For example, if we start with the number 5, we get the following sequence: 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, . . . Here, the subsequence 4, 2, 1 is reached which produces a loop. It was conjectured (by mathematician Lothar Collatz) that no matter what number you start with, you will always end up stuck in the 4, 2, 1 loop. It has, in fact, been shown to hold for all starting values less than 5 x 2^60 ≈ 5.764 x 10^18. However, it still has not been proven for all numbers. You are given a starting number of a hailstone sequence, and should return the number of values in that sequence up to (and including) the first 1.


hailstone(1) → 1
hailstone(5) → 6
hailstone(10) → 7

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

public int hailstone(int 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

Java Help

Misc Code Practice

Post-solution available

Copyright Nick Parlante 2017 - privacy