Question

Write a java to find the Hailstone Sequence of a given number upto 1 using recursion....

Write a java to find the Hailstone Sequence of a given number upto 1 using recursion. Example: The hailstone sequence starting at 13 is : 13 40 20 10 5 16 8 4 2 1 The length of the sequence is 10.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class HailstoneSequence {

    public static int hailstone(int n) {
        if (n == 1) {
            System.out.println("1");
            return 1;
        } else {
            System.out.print(n + " ");
            if (n % 2 == 0)
                return 1 + hailstone(n / 2);
            else
                return 1 + hailstone(3 * n + 1);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = in.nextInt();
        System.out.print("Hailstone sequence starting with " + n + " is: ");
        int len = hailstone(n);
        System.out.println("Length of the hailstone sequence starting with " + n + " is " + len);
    }
}

Add a comment
Know the answer?
Add Answer to:
Write a java to find the Hailstone Sequence of a given number upto 1 using recursion....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Fix this C++ below so does not use any loops at all. Use recursion instead. here...

    Fix this C++ below so does not use any loops at all. Use recursion instead. here is some guides given. Create a directory to hold assignment 3. Copy files hailstone.cpp, Makefile and dotestassignment 2 to the assignment 3 directory. Edit the comments at the top of hailstone.cppto say that this is assignment 3. You should be able to keep the same contracts, 'next' function and 'main' function from assignment 2, unless they contained errors that needed to be fixed. If...

  • use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can...

    use Java please. The Fibonacci Sequence Given the initial Fibonacci numbers 0 and 1, we can generate the next number by adding the two previous Fibonacci numbers together. For this sequence, you will be asked to take an input, denoting how many Fibonacci numbers you want to generate. Call this input upperFibLimit. The longest Fib sequence you should generate is 40 and the shortest you should generate is 1. So,1<upperFibLimit<40 The rule is simple given f(0) 0, f(1) 1 ....

  • in java Write a program that uses recursion to find the largest number in an array....

    in java Write a program that uses recursion to find the largest number in an array. Declare and initialize an array of 10 different numbers.

  • Using Java 1. The Fibonacci sequence starts 1, 1, 2, 3, 5, 8, ....Each number in...

    Using Java 1. The Fibonacci sequence starts 1, 1, 2, 3, 5, 8, ....Each number in the sequence (after the first two) is the sum of the previous two. Write a program that computes and outputs the nth Fibonacci number, where n is a value entered by the user. The Fibonacci Sequence 1,1,2,3,5,8,13.21,34,55,89,144,233,377... 1+12 13+2134 1+23 21+34-55 2-35 34+55-09 3+58 56+89144 5+8=13 89+144233 8+13-21 144+233 377 5

  • In Java: The Fibonacci sequence is a series of numbers beginning with 0 and 1, in...

    In Java: The Fibonacci sequence is a series of numbers beginning with 0 and 1, in which each succeeding number is the sum of the previous two.     0, 1, 1, 2, 3, 5, 8, 13, 21, …. Practice your knowledge of recursion by producing a program that prints the nth Fibonacci number. That is, the program should accept an integer (n) as input and output the number that is the nth number in the Fibonacci sequence. For example, if n...

  • 4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array...

    4. [Tests basic knowledge of recursion] Write a recursive static Java method that accepts an array arr of integers argument returns a list of all permutations of these integers. (A permutation of a sequence of integers is a re-arrangement of the integers. For example, one permutation of 1, 3, 4, 8, 2 is 3, 1, 2, 8, 4.) For this problem, you may assume that the input array contains no duplicate entries. Your method should return an ArrayList of int...

  • Write a JAVA program: Consider the method displayRowOfCharacters that displays any given character the specified number...

    Write a JAVA program: Consider the method displayRowOfCharacters that displays any given character the specified number of times on one line. For example, the call displayRowOfCharacters(‘*’, 5) produces the line ***** Implement this method by using recursion.

  • C# Programming 1-Write a program that generates the following sequence using while loop 2, 3,6,11, 18,...

    C# Programming 1-Write a program that generates the following sequence using while loop 2, 3,6,11, 18, 27,... 102 2-A. Repeat 1 using for loop. B. Modify your program in A to skip 27 from that sequence. C. Modify your program in A by using break statement to stop generating the sequence if the generated number is greater than 30 3. Using loops, find the value of f(7) given: fO)-4, f(n) 2f(n-1)+4, where n is Natural number Hint: f(1)-2f(0)+4-2 4+4-12 f12)-2f(1)+4-...

  • Suppose we select some initial number ? and then build the sequence of values by the...

    Suppose we select some initial number ? and then build the sequence of values by the following 0 Hint: each time you find a multiple of each picked number in the initial list [1:n], make it zero. Elements not equal to zero are primes. This works a lot better than trying to ‘erase’ the element. Because each time you erase an element the remaining indexes roll over, i.e., they change. 1. Find the possible ranges of a and b for...

  • Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First...

    Write an application with two classes USING JAVA METHODS HAVE TO BE CREATED USING RECURSION. First class named Recursion has the following three recursive methods: // PRECONDITION n is positive integer. Method sumTerms returns sum of terms that are // reciprocal values of first n integers, with  alternating signs.      // For example,  sumTerms(7) returns  the following:  1/1 – 1/2  + 1/3 – 1/4  + 1/5 -1/6 + 1/7. // Terms with an odd denominator have positive sign, and terms with even denominator have // negative sign.  ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT