-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
114 lines (91 loc) · 2.73 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
/**
* @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 <inttypes.h>
#include "gmp.h"
int main(int argc, const char *argv[])
{
uint8_t mode;
/* ------------------------------------------ */
/* Check arguments */
/* ------------------------------------------ */
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, start, max;
int flag;
/* ------------------------------------------ */
/* Initialize the numbers */
/* ------------------------------------------ */
mpz_init(n);
mpz_init(start);
mpz_init(max);
/* ------------------------------------------ */
/* Assign values to the numbers */
/* ------------------------------------------ */
flag = mpz_set_str(n, argv[2], 10);
assert (flag == 0); // If flag is not 0 then the operation failed
flag = mpz_set_str(start, argv[2], 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
/* ------------------------------------------ */
/* Main loop */
/* ------------------------------------------ */
while (1)
{
if (mpz_cmp_si(n, 0) == 0 || mpz_cmp_si(n, 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 (mode == 2)
{
if (mpz_cmp(n, max) > 0)
{
mpz_set(max, n);
}
}
}
/* ------------------------------------------ */
/* Print the results */
/* ------------------------------------------ */
printf("We came to the loop!\n");
gmp_printf("Start: '%Zd', end: '%Zd', max: '%Zd'.\n", start, n, max);
/* ------------------------------------------ */
/* Free the numbers */
/* ------------------------------------------ */
mpz_clear(n);
mpz_clear(start);
mpz_clear(max);
return 0;
}