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 Collatz sequence beginning with start up to numIterations.
public class Collatz {
public static void main(String[] args) {
int arr[]=collatz(7, 3);
for(int x:arr)
System.out.print(x+" ");
int [][]a=
{{1,2},{3,4},{5,6}};
int sum=0;
for(int
i=0;i<a.length;i++)
sum+=a[i][0];
System.out.println(sum);
}
public static int[] collatz(int start, int
numIterations) {
int arr[] = new int[numIterations +
1];
arr[0]=start;//first num will be
starting only
for (int i = 1; i < 4; i++)
{
//checking if it
odd than 3n+1 else n/2
if(start%2==1)
start=start*3+1;
else
start=start/2;
arr[i]=start;
}
return arr;
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me
collatz public static int[] collatz(int start, int numIterations) Given integer start and integer numIterations, return an...
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...
c++ The Collatz Conjecture is a conjecture in mathematics that concerns a sequence sometimes known as hailstone numbers. Given any positive integer n, the following term, n+1 is calculated as follows: If n is even, then n+1 is defined as n/2. If n is odd, then n+1 is defined as 3n + 1 The Collatz Conjecture states that, for any value of n, the sequence will always reach 1. Once the pattern reaches 1, it repeats indefinitely (3 * 1...
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...
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...
Please help me ONLY for the second method : public static int[] runningGroups(String[] inputFileNames) throws IOException. The second method has an "array of files as a parameter". and return int [ ]. Each element of the return array is the number of group that can get from the first method. I got an error Exception in thread "main" java.lang.NullPointerException Here my code: import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class RunningGroups { public static void main(String[] args) throws IOException...
public static int Fibonacci(int n) This method receives an integer as a parameter and returns the index of n in the Fibonacci series. If n does not exist, the method returns -1. C#
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,...
Given an array and a starting position write a function replaceFromN, that takes an integer array 'array', the size of the array size and a starting positions 'n' as parameters and replaces the elements starting from that index onward with the sequence 1,2,3,... The function returns nothing. void replaceFromN(int array[], int size, int n) For example, given array= {15,12,4,9,2,3} n =2 the function should modify array to be {15,12,1,2,3,4}
1. t(n) is the runtime of following function, public static int f4(int [] a, int start, int end){ int ans = 0; if (start >= end) ans = a[start]; else { int mid = (start + end) / 2; int x = f4(a, start, mid); int y = f4(a, mid + 1, end); print(a, start, end); //print each element in a from start to end if (x < y) ans = x; else ans = y; } return ans; }...