-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
129 lines (107 loc) · 2.82 KB
/
main.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
/**
* @author Andrej Bartulin
* PROJECT: 3x
* LICENSE: Public Domain
* DESCRIPTION: Main file for C version
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "gmp.h"
int main(int argc, const char *argv[])
{
int mode;
if (argc != 3)
{
printf("Usage: %s <mode> <number>\n", argv[0]);
return 0;
}
else
{
mode = atoi(argv[1]);
}
/*
mpz_t is the type defined for GMP integers.
It is a pointer to the internals of the GMP integer data structure
*/
mpz_t n, previous, max;
int flag;
/* Initialize the number */
mpz_init(n);
mpz_set_ui(n,0);
/* Parse the input string as a base 10 number */
flag = mpz_set_str(n, argv[2], 10);
assert (flag == 0); /* If flag is not 0 then the operation failed */
flag = mpz_set_str(previous, "0", 10);
assert (flag == 0); /* If flag is not 0 then the operation failed */
flag = mpz_set_str(max, argv[2], 10);
assert (flag == 0); /* If flag is not 0 then the operation failed */
if (mode == 1)
{
while (1)
{
if (mpz_cmp_d(n, (double)1) == 0)
{
break;
}
if (mpz_divisible_ui_p(n, (unsigned long)2) != 0)
{
mpz_div_ui(n, n, 2);
mpz_out_str(stdout, 10, n);
printf("\n");
}
else
{
mpz_mul_ui(n, n, 3);
mpz_add_ui(n, n, 1);
mpz_out_str(stdout, 10, n);
printf("\n");
}
}
printf("We came to the loop! Number n at start was: %s, now is: ", argv[1]);
mpz_out_str(stdout, 10, n);
printf(".\n");
}
else if (mode == 2)
{
while (1)
{
if (mpz_cmp_d(n, (double)1) == 0)
{
break;
}
if (mpz_divisible_ui_p(n, (unsigned long)2) != 0)
{
mpz_div_ui(n, n, 2);
mpz_out_str(stdout, 10, n);
printf("\n");
}
else
{
mpz_mul_ui(n, n, 3);
mpz_add_ui(n, n, 1);
mpz_out_str(stdout, 10, n);
printf("\n");
}
if (mpz_cmp(n, max) > 0)
{
mpz_set(max, n);
}
mpz_set(previous, n);
}
printf("We came to the loop! Number n at start was: %s, now is: ", argv[1]);
mpz_out_str(stdout, 10, n);
printf(". Max is: ");
mpz_out_str(stdout, 10, max);
printf(".\n");
}
else
{
printf("Invalid mode\n");
return 0;
}
/* Clean up the mpz_t handles or else we will leak memory */
mpz_clear(n);
return 0;
}