Question
Problem 1. (100 points) The Collatz conjecture concerns what happens when we take any positive integer and apply the following algorithm N = N /
Problem 1. (100 points)
The
Collatz
conjecture concerns what happens when we take any positive integer
and apply the following algorithm
N = N / 2 (if N is even
) OR
N = 3 * N + 1 (if N is odd)
The conjecture states that when this algorithm is implemented continuously, all
positive integers will reach 1 finally.
For example, if N = 35, the sequence is
35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Write a C program
using the
fork() system call
which generates this
Collatz
Sequence in the Child process
.
At command terminal, the starting number should be given by user.
Ex) if a user give
8
as a parameter, the child process will output
8, 4, 2, 1
.
Because the
parent
and
child
process have their own copies of the data, it will be
necessary for the
child
to output the sequence.
Also, your program should allow the
parent
to invoke the
wait()
call for the child
process to complete before exiting the program
For your information, I attach the basic template as follows.
(Also, you may use your own template or code such as scanf() )
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
pid_t pid;
int n;
if (argc == 1) {
fprintf(stderr,"Usage: ./a.out
return -1;
}
n = atoi(argv[1]);
....
....
}
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