-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.14_collatz-conjecture.c
68 lines (60 loc) · 1.99 KB
/
3.14_collatz-conjecture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Name : Tri Wanda Septian
* Chapter 3.14* Programming
* ++++++++++++
* 3.14*: The Collatz conjecture concerns what happens when we take any positive integer n and apply the following algorithm:
* n= n/2, if n is even
* n= 3*n+1, if n is odd
* The conjecture states that when this algorithm is continually applied, all positive integers will eventually reach 1.
* 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 that generates this sequence in the child process.
* The starting number will be provided from the command line.
* For example, if 8 is passed as a parameter on the command line, the child process will output 8, 4, 2, 1.
* Because the parent and child processes have their own copies of the data, it will be necessary for the child to output the sequence.
* Have the parent invoke the wait() call to wait for the child process to complete before existing the program.
* Perform necessary error checking to ensure that a positive integer is passed on the command line.
*
*/
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
printf("Operating System\n");
printf("Name : Tri Wanda Septian\n");
int n=0;
int k=0;
pid_t pid;
do
{
printf("Please enter a number greater than 0 to run the Collatz Conjecture : ");scanf("%d", &k);
}while (k <= 0);
pid = fork();
if (pid == 0)
{
printf("Child is working...\n");
printf("%d\n",k);
while (k!=1)
{
if (k%2 == 0)
{
k = k/2;
}
else if (k%2 == 1)
{
k = 3*(k)+1;
}
printf("%d\n",k);
}
printf("Child process is done.\n");
}
else
{
printf("Parents is waiting on child process...\n");
wait(NULL);
printf("Parent process is done.\n");
}
return 0;
}