Question
Re: that programming concept is new < Destiny Sparks: Do you meant it is unanswerable? -Thanks > COMP 1900 - Spring 2021 Lab 4: Loops
Re: "that programming concept is new" < Destiny Sparks: Do you meant it is unanswerable? -Thanks >
COMP 1900 - Spring 2021
Lab 4: Loops
Please explain as best you can, I want to understand not just get the answer. Thank you so much!
1.
Write a program named
Warmup.java
that prints the following sequences of numbers on the
screen. Start by writing a loop that runs for the desired number of iterations, then think about
what each iteration should do.
(a)
(2 points) The first 20 terms of -11, -8, -5, -2, 1, ...
(b)
(2 points) The first 50 terms of 100.0, 102.0, 104.04, 106.1208, 108.243216, ... (Hint:
Each term is 2 percent more than the previous term.)
(c)
(3 points) The first 100 terms of 3840, 3838, 3835, 3831, 3826, ... (Hint: Make a variable
to store how much you should subtract to get to the next term. That variable changes its
value with each loop iteration.)
(d)
(3 points) The first 997 terms of 1000, -997, 994, -991, 988, ... (Hint: One way to do this is
to include conditional statements within your loop.)
2.
(6 points) Consider the sequence of integers defined as follows:
The first term is any positive integer.
For each term
n
in the sequence, the next term is computed like this: If
n
is even, the next
term is
n/
2. If
n
is odd, the next term is 3
n
+ 1.
Stop once the sequence reaches 1.
Here are a few examples of this sequence for different values of the first term:
8, 4, 2, 1
12, 6, 3, 10, 5, 16, 8, 4, 2, 1
19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2
Note that all three of these eventually do reach 1. In fact, it is believed (but not known) that
the sequence will
always
reach 1, regardless of the first term chosen. This is known as the
Collatz conjecture
. Despite its apparent simplicity, it has so far eluded all attempts at a proof.
Mathematician Jeffrey Lagarias at the University of Michigan has claimed that this is an
extraordinarily difficult problem, completely out of reach of present day mathematics.
We might not be able to prove the Collatz conjecture here, but we can experiment
computationally with it! Write a program named
Collatz.java
that allows the user to enter any
positive integer. The program should then compute and list all the terms of the sequence until
it reaches 1. At the end, show how many terms it took to get to 1 (including 1 itself).
Include input validation to ensure that the users input is positive. If its not positive, show an
error message and allow the user to re-enter the input as many times as necessary.
Example program run (underlined parts indicate what the user enters)
Enter starting value (must be a positive integer):
12
12
6
3
10
5
16
8
4
2
1
Number of terms: 10
Example program run (underlined parts indicate what the user enters)
Enter starting value (must be a positive integer):
1
1
Number of terms: 1
Example program run (underlined parts indicate what the user enters)
Enter starting value (must be a positive integer):
-5
Must be a positive integer! Try again:
0
Must be a positive integer! Try again:
5
5
16
8
4
2
1
Number of terms: 6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started