In java, please Create and then modify your version of the 3N+1 sequence problem
ID: 3599405 • Letter: I
Question
In java, please
Create and then modify your version of the 3N+1 sequence problem so that it includes the function 'threeNSeqLength' which accepts any positive integer and returns the length of the given positive integer's 3N+1 sequence length. This new version of the program should then loop through the first 100 positive integers, storing each integer's 3N+1 sequence length in a ArrayList. One of the outputs should be a console listing of each of the first 100 positive integers, followed by its 3N+1 sequence length. The next outputExplanation / Answer
import java.util.ArrayList;
class Main{
static int threeNSeqLength(int N){
int s = 1;
//Loop until 1 is reached
while(N>1){
if(N%2==0){
N/=2;
}
else{
N = 3*N+1;
}
s++;
}
return s;
}
public static void main(String[] args) {
ArrayList<Integer> seq = new ArrayList();
for(int i=1;i<=100;i++){
//Calcuate the three sequence length and add to the list
int ans = threeNSeqLength(i);
seq.add(ans);
}
for(int i=1;i<=100;i++){
System.out.println(i+" "+seq.get(i-1));
}
}
}
OUTPUT :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.