Hailstone

Hailstone - int n=readInt(); int i=0; while(n!=1){...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * File: Hailstone.java * Name: * Section Leader: * -------------------- * This file is the starter file for the Hailstone problem. */ import acm.program.*; public class Hailstone extends ConsoleProgram { public void run() { print("Enter a number: ");
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int n=readInt(); int i=0; while(n!=1){ if((n%2)==1){ println(n+" is odd, so i make 3n+1: " + ((3*n)+1)); n=3*n+1; } else{ println(n+" is even, so i take half: "+n/2); n/=2; } i++; } println("The process took "+i+" to reach 1"); } }...
View Full Document

This note was uploaded on 10/25/2010 for the course CS 106A at Stanford.

Ask a homework question - tutors are online