import ptolemy.plot.*;
public class Hailstone
{
public static void main( String[] args )
{
int n = 300;
plot_hailstone( n );
System.out.println( "Plot of Hailstone numbers from 1 to " + n + ".\n" );
}
public static void hailstone( int a, int i, Plot hsPlot )
{
if( a == 1 ) return;
if( a%2 == 1 ) a = 3*a + 1 ;
else a = a/2 ;
hsPlot.addPoint( 0, i, a, false );
hailstone( a, i, hsPlot );
return;
}
public static void plot_hailstone( int n ) {
Plot hsPlot = new Plot();
hsPlot.setTitle( "hailstone(n) vs n" ); hsPlot.setXLabel( "n" ); hsPlot.setYLabel( "hailstone(n)" ); hsPlot.setYRange( 0, 200 ); hsPlot.setYLog( true ); hsPlot.setMarksStyle( "dots", 0 );
for( int i = 2; i < n; i++ )
{
hsPlot.addPoint( 0, i, i, false );
hailstone( i, i, hsPlot );
}
PlotApplication app = new PlotApplication( hsPlot ); }
}