-
Notifications
You must be signed in to change notification settings - Fork 0
/
syracuse.c
179 lines (145 loc) · 5.44 KB
/
syracuse.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* syracuse.c */
# include <math.h>
# include <stdlib.h>
# include <stdio.h>
# include <ctype.h>
# include <limits.h>
////////////////////////////////////////////////
//function that takes s0 and n as args, and returns nth element in series
int element ( unsigned int s, unsigned int n ) {
//declare counter for element in the series
int i;
//while loop, to test if s0 and subsequent s != 1. Start at 2, as first
//element is the parameter s before modification
for (i=2; i<=n; i++) {
//if s is even, divide it by two
if (s % 2 == 0 ) {
s /= 2;
}
//if s is odd, multiply it by 3 then add 1
else {
s = 3 * s + 1;
}
}
return s;
}
////////////////////////////////////////////////
//function that takes s0 and returns number of iterations before reaching 1
unsigned int iterationsto1 ( unsigned int s ) {
//must initialise counter to zero, or else could start from any value
unsigned int iterations = 0;
//iterations = 222;
//while loop, to test if s0 and subsequent s != 1
while (s != 1) {
//if s is even, divide it by two
if (s % 2 == 0 ) {
s /= 2;
}
//if s is odd, multiply it by 3 then add 1
else {
s = 3 * s + 1;
}
//increment iterations counter in either case
iterations++;
}
return iterations;
}
////////////////////////////////////////////////
//function that takes s0 and returns the number of iterations before reaching
//less than s0
unsigned int iterationstos0 ( unsigned int s ) {
//must initialise counter to zero, or else could start from any value
unsigned int iterations = 0;
//save value of s0
unsigned int s0 = s;
//while loop, to test if s0 and subsequent s >= s0
while (s >= s0) {
//if s is even, divide it by two
if (s % 2 == 0 ) {
s /= 2;
}
//if s is odd, multiply it by 3 then add 1
else {
s = 3 * s + 1;
}
//increment iterations counter in either case
iterations++;
}
return iterations;
}
////////////////////////////////////////////////
//function that takes s0 and continues until reaching 1, returning the highest value taken by sN along the way
int highestvalue ( unsigned int s ) {
//declare highest value counter as 1
int highest = 1;
//while loop, to test if s0 and subsequent s != 1
while (s != 1) {
//if s is even, divide it by two
if (s % 2 == 0 ) {
s /= 2;
}
//if s is odd, multiply it by 3 then add 1
else {
s = 3 * s + 1;
}
//if s > highest, set highest to s, otherwise do nothing
if (s > highest) {
highest = s;
}
}
return highest;
}
////////////////////////////////////////////////
//main function
int main ( int argc, char * argv []) {
//declare vars
int mode, s0, n;
char term;
//display 4 options to user, parse input and call switch
printf("Please choose one of the following 4 modes:\n");
printf("[1] Display nth element for a given s0\n");
printf("[2] Display the number of iterations before reaching 1\n");
printf("[3] Display the number of iterations before reaching < s0\n");
printf("[4] Display the highest value taken by sN in the series\n");
printf("Enter the mode number followed by <enter>:\n");
if(scanf("%d%c", &mode, &term) != 2 || term != '\n' || mode > 4 || mode < 1){
printf("Unrecognised character - enter a valid integer [1-4].\n");
exit(0);
}
//ask user for s0, exit if invalid
printf("Please enter the value of the first element of the syracuse series (positive integer):\n");
if(scanf("%d%c", &s0, &term) != 2 || term != '\n' || s0 < 1){
printf("Unrecognised character - enter a positive integer.\n");
exit(0);
}
//call one of four functions based on input
switch (mode) {
//option 1 - nth element
case 1:
//ask user for nth element to fetch
printf("Please enter a value for n. The nth element of the series will then be displayed:\n");
if(scanf("%d%c", &n, &term) != 2 || term != '\n' || n < 1){
printf("Unrecognised character - enter a positive integer.\n");
exit(0);
}
//call element() with s0 as param and display result
printf("Element number %d of the syracuse series starting at %d is: %d\n", n, s0, element(s0, n));
break;
case 2:
//call iterationsto1() with s0 as param and display result
printf("The number of iterations required to reach 1 in the syracuse series starting at %d is: %d\n", s0, iterationsto1(s0));
break;
case 3:
//call iterationstos0) with s0 as param and display result
printf("The number of iterations required to drop below s0 in the syracuse series starting at %d is: %d\n", s0, iterationstos0(s0));
break;
case 4:
//highestvalue()highestvaluehighestvalue
//call highestvalue() with s0 as param and display result
printf("The highest value taken by sN in the syracuse series starting at %d is: %d\n", s0, highestvalue(s0));
break;
default:
printf("Error occured.\n");
exit(0);
}
}// end of main function