-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollatzConjectureSequence.c
113 lines (106 loc) · 1.9 KB
/
CollatzConjectureSequence.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
/* Simple Application to print collatz conjecture
* sequences by creating 2 child processes
*
* @author Neelima Parakala
* UNAME - neelima1
* UID - U03623424
*/
int main(int argc, char *argv[])
{
int n;
if( argc == 2 )
{
n = atoi(argv[1]);// String to number conversion
if( n > 0 && n < 40 )// Wrong input checking
{
collatz( n, n+4 );// Function calling
}
else
{
printf( "please enter valid input... the input must be greater than 0 and less than 40.\n" );
}
}
else if( argc > 2 )
{
printf( "Too many arguments supplied.\n" );
}
else
{
printf( "One argument expected.\n" );
}
printf( "\n" );
return 0;
}
/* This method creates 2 child processes
* to print collatz conjecture sequence
* @param {int} i
* @param {int} j
* @return {int}
*/
int collatz(int i, int j)
{
pid_t child_pid1, child_pid2;
// First child process creation
child_pid1 = fork();
if ( child_pid1 < 0 )
{
printf( "fork() of child_pid1 failed\n" );
}
else if ( child_pid1 > 0 )
{
// Second child process creation
child_pid2 = fork();
if( child_pid2 < 0 )
{
printf( "fork() of child_pid2 failed\n" );
}
else if( child_pid2 > 0 )
{
wait();
printf( "One done! \n" );
wait();
printf( "Children Complete \n" );
exit(1);
}
else
{
collatzSequence( j, 2 );
}
}
else
{
collatzSequence( i, 1 );
}
return 0;
}
/**
* This method prints collatz conjecture
* sequence of the given input
* @param {int} j
* @param {int} child
*/
int collatzSequence( int num, int child )
{
printf( "From child %d init n=%d", child, num );
while( num != 1 )
{
printf( ", " );
// If n is even
if( num%2 == 0 )
{
num /= 2;
}
// if n is odd
else
{
num = 3*num + 1;
}
printf( "From Child %d n=%d", child, num );
}
printf( "\n\n" );
exit(1);
return 0;
}