DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Getting Started With Jenkins: Learn fundamentals that underpin CI/CD, how to create a pipeline, and when and where to use Jenkins.

Related

  • Unlocking the Power of Postgres Listen/Notify: Building Scalable Solution With Spring Boot Integration
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • The Long Road to Java Virtual Threads
  • Demystifying Virtual Thread Performance: Unveiling the Truth Beyond the Buzz

Trending

  • Building Intelligent Chatbots With Streamlit, OpenAI, and Elasticsearch
  • Unlocking Seamless Customer Relationship Management With React Integration Features
  • How to Write API Documentation: Best Practices and Examples
  • Optimizing API Lifecycles: A Comprehensive Guide for Product Managers
  1. DZone
  2. Coding
  3. Java
  4. How to generate Hailstone Sequence in Java?

How to generate Hailstone Sequence in Java?

Jagadeesh Motamarri user avatar by
Jagadeesh Motamarri
·
Sep. 20, 13 · Interview
Like (12)
Save
Tweet
Share
30.2K Views

Join the DZone community and get the full member experience.

Join For Free

Program Statement

How to generate Hailstone Sequence in Java?

Solution

The Hailstone sequence of numbers can be generated from a starting positive integer, n by:

  • If n is 1 then the sequence ends.
  • If n is even then the next n of the sequence = n/2
  • If n is odd then the next n of the sequence = (3 * n) + 1

Code

?
package com.skilledmonster.examples.operations;
import java.util.Scanner;
/**
* Program to generate Hailstone Sequence.
* This program reads a number from the user then displays the Hailstone sequence
* for that number followed by a line that shows the number of steps taken to reach 1.
*
* @author Jagadeesh Motamarri
* @version 1.0
*/
public class HailstoneSequenceGenerator {
public static void main(String[] args) {
Scanner inputScanner = new Scanner(System.in);
System.out.printf("Enter a Number:  ");
try {
int number = inputScanner.nextInt();
int steps = 0;
while (number != 1) {
if (number % 2 == 0) {
System.out.println(number + " is even, so I take half: " + number / 2);
number /= 2;
} else {
System.out.println(number + " is odd, so I make 3n + 1: " + (number * 3 + 1));
number = number * 3 + 1;
}
steps++;
}
System.out.println("The process took " + steps + (steps < 2 ? " step" : " steps") + " to reach 1");
} catch (Exception e) {
System.out.println("Not a Number!! Run your Program again <img src="http://www.skilledmonster.com/wp-includes/images/smilies/icon_smile.gif" alt=":-)" class="wp-smiley"> ");
}
}
}

Output

hailstone_sequence   

As shown in the console output, for interger 17, it took 12 steps to reach 1 using Hailstone Sequence.

References

http://en.wikipedia.org/wiki/Collatz_conjecture


Java (programming language)

Published at DZone with permission of Jagadeesh Motamarri, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Unlocking the Power of Postgres Listen/Notify: Building Scalable Solution With Spring Boot Integration
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • The Long Road to Java Virtual Threads
  • Demystifying Virtual Thread Performance: Unveiling the Truth Beyond the Buzz

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: