Question

public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...

public static int[] collatz(int start, int numIterations)

Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34]

TESTING:

collatz(7,3) should return {7, 22, 11, 34}

collatz(6,0) should return {6}

collatz(6, 5) should return {6, 3, 10, 5, 16, 8}

collatz(1,1) should return {1, 4}

collatz(0, 5) should return {0, 0, 0, 0, 0, 0}

Parameters:

start - starting integer

numIterations - how long to compute the Collatz sequence for

Returns:

an array containing the Collatz sequence beginning with start up to numIterations.

CODE I HAVE THAT IS WRONG:

public class Collatz {

public void main(String[] args) {

int arr[]=collatz(7, 3);

for(int x:arr)

System.out.print(x+" ");

}

public int[] collatz(int start, int numIterations) {

int arr[] = new int[numIterations + 1];

arr[0]=start;

for (int i = 1; i < 4; i++) {

if(start%2==1)

start=start*3+1;

else

start=start/2;

arr[i]=start;

}

return arr;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class Collatz {

public static void main(String[] args) {

int[] arr=collatz(6, 5);

for(int x:arr)

System.out.print(x+" ");

}

public static int[] collatz(int start, int numIterations) {

int[] arr = new int[numIterations + 1];

arr[0]=start;

for (int i = 1; i <= numIterations; i++) {

if(start%2==1)

start=start*3+1;

else

start=start/2;

arr[i]=start;

}

return arr;

}

}

Add a comment
Know the answer?
Add Answer to:
public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...
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
  • public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array...

    public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the Collatz...

  • collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an...

    collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an array containing the Collatz sequence beginning with start up to numIterations. The Collatz function is defined by: 3n + 1 if n is odd n/2 if n is even Given start = 7 and numIterations = 3, this method returns [7, 22, 11, 34] Parameters: start - starting integer numIterations - how long to compute the Collatz sequence for Returns: an array containing the...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int...

    Consider the following method. public static ArrayList<Integer mystery(int n) ArrayList<Integer seg - new ArrayList<IntegerO; for (int k = n; k > 0; k--) seq.add(new Integer(k+3)); return seq What is the final output of the following Java statement? System.out.println(mystery(3)); a. [1,2,4,5) b. [2,3,4,5) c. [6,5,4,3] d. [7, 7, 8, 8] e. [7,8,9, 8) O Consider the following method: public static int mystery(int[] arr, int k) ifk-0) return 0; }else{ return arr[k - 1] + mystery(arr, k-1):) The code segment below is...

  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • (JAVA) Given an array of unique positive integers, write a function findSums that takes the array...

    (JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...

  • static public int[] Question() // retrieve an integer 'n' from the console // the first input...

    static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...

  • 2. Use hashing (solve_with_Hash(int[] array, int k)) Initialize a counter variable to O: Insert a...

    2. Use hashing (solve_with_Hash(int[] array, int k)) Initialize a counter variable to O: Insert all elements of array in a hashtable For every element in array: counter0 a. b. c. .Look for array[i] . Look for array [幻 + k in the hash map, if found then increment counter. -k in the hash map, if found then increment counter. Remove arrayli] from hash table. d. return counter For example: Input : array[] {1, 5, 3, 4, 2), k 3 Output:...

  • Java Array The method rotateLeft() takes in a reference a to an integer array and a...

    Java Array The method rotateLeft() takes in a reference a to an integer array and a positive integer n and it returns a new array whose contents is the contents of the input array rotated to the left n places. So each element a[i] of the input array should be placed at location b[i-n] of the returned array. If the index i-n is negative, then that index should "wrap" around to the end of the output array. For example, if...

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