-
Notifications
You must be signed in to change notification settings - Fork 0
/
wondrous.c
56 lines (44 loc) · 1.08 KB
/
wondrous.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
// Edited by Ryan Shivashankar
// Prints the Wondrous Sequence
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define STOP_NUMBER 1
int printWondrous (int start);
void testWondrous (void);
int main (int argc, char *argv[]){
testWondrous ();
/*int number;
scanf ("%d", &number);
printWondrous(number);
*/
return EXIT_SUCCESS;
}
int printWondrous (int start) {
int count =1;
int number = start;
//printf ("%d ", number);
while (number != STOP_NUMBER) {
printf ("%d ", number);
if ((number%2) == 0){
number = number/2;
} else {
number = (number*3 + 1);
}
count++;
}
printf ("%d\n", number);
//printf ("Number of terms: %d\n" ,count);
return count;
}
void testWondrous (void){
assert(printWondrous(1)==1);
assert(printWondrous(2)==2);
assert(printWondrous(4)==3);
assert(printWondrous(8)==4);
assert(printWondrous(16)==5);
assert(printWondrous(5)==6);
assert(printWondrous(10)==7);
assert(printWondrous(3)==8);
//printf("All tests passed.\n");
}