-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_system.c
More file actions
199 lines (152 loc) · 3.38 KB
/
Copy pathnumber_system.c
File metadata and controls
199 lines (152 loc) · 3.38 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
/*
* Converts a decimal number to binary by repeatedly dividing
* the number by 2 and pushing the remainders into a stack.
*/
void decimal_to_binary_by_division(int decimal)
{
int binary[32] = {0,};
int top = -1;
printf("%d = ", decimal);
while (decimal > 0)
// push into the stack
binary[++top] = decimal % 2, decimal /= 2;
while (top > -1)
// pop from the stack
printf("%d", binary[top--]);
printf("\n");
}
/*
* Converts a decimal number to its binary (base-2) representation
* using the bitwise right shift operator.
*/
void decimal_to_binary_by_shift(int decimal)
{
int binary[32] = {0,};
int top = -1;
printf("%d = ", decimal);
while (decimal > 0) {
binary[++top] = decimal & 1;
decimal >>= 1;
}
while (top > -1)
printf("%d", binary[top--]);
printf("\n");
}
void binary_to_decimal(char *binary)
{
int decimal = 0;
int len = strlen(binary);
int i;
for (i = 0 ; i < len ; i++)
decimal = decimal * 2 + (binary[i] - '0');
printf("%s = %d\n", binary, decimal);
}
void octal_to_decimal(char *octal)
{
int decimal = 0;
int len = strlen(octal);
for (int i = 0 ; i < len ; i++)
decimal = decimal * 8 + (octal[i] - '0');
printf("%s = %d\n", octal, decimal);
}
void binary_to_hexadecimal(char *binary)
{
int decimal = 0;
int len = strlen(binary);
for (int i = 0 ; i < len ; i++)
decimal = decimal * 2 + (binary[i] - '0');
printf("%s = %#x\n", binary, decimal);
}
// TODO: convert from any base to decimal and vice versa
int any_base_to_decimal(char *num, int base);
char* decimal_to_any_base(int decimal, int base);
#define MAX_N 1000
void prime_numbers_with_saved_array(int max_n)
{
int n, i;
int prime[MAX_N] = {0,};
int idx = 0;
printf("%s: <= %d\n", __func__, max_n);
if (max_n <= 1)
return;
prime[0] = 2;
idx = 1;
printf("%d ", prime[0]);
for (n = 3 ; n <= max_n ; n += 2) {
for (i = 0 ; i < idx ; i++)
if (n % prime[i] == 0)
break;
if (i == idx) {
prime[idx++] = n;
printf("%d ", n);
}
}
printf("\n");
}
void prime_numbers_without_array(int max_n)
{
int n, i;
printf("%s: <= %d\n", __func__, max_n);
if (max_n <= 1)
return;
printf("2 ");
for (n = 3 ; n <= max_n ; n += 2) {
for (i = 3 ; i*i < n ; i += 2)
if (n % i == 0)
break;
if (i*i > n)
printf("%d ", n);
}
printf("\n");
}
void prime_numbers_using_Sieve_of_Eratosthenes(int max_n)
{
bool is_prime[MAX_N + 1] = {0,};
int n, i;
printf("%s: <= %d\n", __func__, max_n);
memset(is_prime, true, sizeof(is_prime));
for (n = 2 ; n*n <= max_n ; n++)
if (is_prime[n])
// for (i = n*n ; i <= max_n ; i += n)
// is_prime[i] = false;
for (i = n ; i <= max_n/n ; i++) // alternative
is_prime[n*i] = false;
for (n = 2 ; n <= max_n ; n++)
if (is_prime[n])
printf("%d ", n);
}
int gcd_brute_force(int a, int b)
{
int min = a < b ? a : b;
int gcd = min;
printf("%s(%d, %d)", __func__, a, b);
while (gcd > 0) {
if (a % gcd == 0 && b % gcd == 0)
break;
gcd--;
}
printf(" = %d\n", gcd);
return gcd;
}
int gcd_Euclidean_recur(int a, int b)
{
printf("%s(%d, %d)\n", __func__, a, b);
if (b == 0) // b is smaller than a
return a;
return gcd_Euclidean_recur(b, a % b);
}
int gcd_Euclidean_iter(int a, int b)
{
int r;
while (b != 0) { // b is smaller than a
printf("%s(%d, %d)\n", __func__, a, b);
r = a % b;
a = b;
b = r;
}
printf("= %d\n", a);
return a;
}