-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolitary_number.c
332 lines (274 loc) · 7.17 KB
/
solitary_number.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// https://mathworld.wolfram.com/SolitaryNumber.html
// Friendly/Solitary/Multi-perfect numbers
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <stdnoreturn.h>
#include <stdbool.h>
#include <assert.h>
#include <math.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include <CUnit/Console.h>
//#define NDEBUG // run or test
#define LIMIT 100000//100000// 5000
noreturn void simple_err(const char *s)
{
if( (s==NULL) || ( *s=='\0') )
{
fprintf(stderr,"Please provide a proper message for simple_err function\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"%s\n",s);
exit(EXIT_FAILURE);
}
bool is_prime(int num)
{
int i, flag = 1;
// Iterate from 2 to sqrt(n)
for (i = 2; i <= sqrt(num); i++)
{
// If n is divisible by any number between
// 2 and n/2, it is not prime
if (num % i == 0)
{
flag = 0;
break;
}
}
if (num <= 1)
flag = 0;
if (flag == 1)
{
//printf("%d is a prime number", n);
return true;
}
else
{
// printf("%d is not a prime number", n);
return false;
}
//return 0;
}
/*int divisor_function(int num)
{
int i;
int sigma=0;
for(i=1; i<=sqrt(num); i++)
{
if(num %i == 0)
{
int ndi=num/i;
if(ndi == i)
sigma += i;
else
{
sigma += i;
sigma += (ndi);
}
}
}
return sigma;
}*/
// https://rosettacode.org/wiki/Sum_of_divisors#C
// See https://en.wikipedia.org/wiki/Divisor_function
unsigned int divisor_function(unsigned int n) {
unsigned int total = 1, power = 2;
unsigned int p;
// Deal with powers of 2 first
for (; (n & 1) == 0; power <<= 1, n >>= 1) {
total += power;
}
// Odd prime factors up to the square root
for (p = 3; p * p <= n; p += 2) {
unsigned int sum = 1;
for (power = p; n % p == 0; power *= p, n /= p) {
sum += power;
}
total *= sum;
}
// If n > 1 then it's prime
if (n > 1) {
total *= n + 1;
}
return total;
}
int gcd(int a, int b)
{
int r;
while (b != 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
struct ratio
{ // simplified sigma(n)/n
int sigma_n;
int n;
int gcd;
};
struct ratio ten_ratio(void) // 18/10 or simplified 9/5
{
struct ratio const rs={.sigma_n=9 , .n=5};
return rs;
}
struct ratio make_ratio(int sigma_n, int n)
{
struct ratio rs;
rs.sigma_n=sigma_n;
rs.n=n;
return rs;
}
bool ratio_equal(struct ratio rs1,struct ratio rs2)
{
assert(gcd(rs1.sigma_n,rs1.n)==1);
assert(gcd(rs2.sigma_n,rs2.n)==1);
if(rs1.sigma_n==rs2.sigma_n && rs1.n==rs2.n)
return true;
else
return false;
}
struct ratio compute_ratio(int num)
{
struct ratio rs;
rs.sigma_n=divisor_function(num);
rs.n=num;
int gcd_num=gcd(rs.sigma_n,rs.n);
rs.sigma_n /= gcd_num;
rs.n /= gcd_num;
rs.gcd=gcd_num;
assert(gcd(rs.sigma_n,rs.n)==1);
return rs;
}
bool p_multi_p(int num)
{
bool pmp=false ;
double s1=sqrt(num);
int s2=round(s1);
if(is_prime(s2) && s2*s2==num)
pmp=true;
return pmp;
}
void test_gcd(void);
void test_divisor_function(void);
void test_is_prime(void);
void test_ratio_equal(void);
void test_compute_ratio(void);
void test_p_multi_p(void);
int main(void)
{
#ifndef NDEBUG
int in_reg;
in_reg = CU_initialize_registry();
if (in_reg != CUE_SUCCESS)
simple_err("Initialization failed.");
CU_pSuite suite1 = CU_add_suite("suite1", NULL, NULL);
if (suite1 == NULL)
simple_err("suite1 creation failed.");
CU_pTest test1 = CU_add_test(suite1, "test_gcd", test_gcd);
if (test1 == NULL)
simple_err("test1 creation failed.");
CU_pTest test2 = CU_add_test(suite1, "test_is_prime", test_is_prime);
if (test2 == NULL)
simple_err("test2 creation failed.");
CU_pTest test3 = CU_add_test(suite1, "test_divisor_function", test_divisor_function);
if (test3 == NULL)
simple_err("test3 creation failed.");
CU_pTest test4 = CU_add_test(suite1, "test_ratio_equal", test_ratio_equal);
if (test4 == NULL)
simple_err("test4 creation failed.");
CU_pTest test5 = CU_add_test(suite1, "test_compute_ratio", test_compute_ratio);
if (test5 == NULL)
simple_err("test5 creation failed.");
CU_pTest test6 = CU_add_test(suite1, "test_p_multi_p", test_p_multi_p);
if (test6 == NULL)
simple_err("test6 creation failed.");
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
#else // release
int i;
struct ratio rs;
for(i=1; i<=LIMIT; i++)
{
rs=compute_ratio(i);
if(ratio_equal(rs,ten_ratio() ) && i !=10)
{
printf("Friend of 10 found, it is %d\n",i);
return 0;
}
else
{
bool p2=p_multi_p(rs.sigma_n);
if( p2 && is_prime(rs.n) )// rs.sigma_n/10==0 &&
//if(is_prime(rs.sigma_n) && is_prime(rs.n) )
printf("Num:%6d gcd:%6d ratio: %d/%d \n",i,rs.gcd,rs.sigma_n,rs.n);
}
}
#endif // NDEBUG
return EXIT_SUCCESS;
}
void test_gcd(void)
{
CU_ASSERT_EQUAL(gcd(1,2),1);
CU_ASSERT_EQUAL(gcd(2,2),2);
CU_ASSERT_EQUAL(gcd(6,4),2);
CU_ASSERT_EQUAL(gcd(8,20),4);
CU_ASSERT_EQUAL(gcd(18,27),9);
CU_ASSERT_EQUAL(gcd(1000,101),1);
}
void test_divisor_function(void)
{
CU_ASSERT_EQUAL(divisor_function(1),1);
CU_ASSERT_EQUAL(divisor_function(2),3);
CU_ASSERT_EQUAL(divisor_function(3),4);
CU_ASSERT_EQUAL(divisor_function(4),7);
CU_ASSERT_EQUAL(divisor_function(10),18);
CU_ASSERT_EQUAL(divisor_function(15),24);
}
void test_is_prime(void)
{
CU_ASSERT_EQUAL(is_prime(1),false);
CU_ASSERT_EQUAL(is_prime(2),true);
CU_ASSERT_EQUAL(is_prime(5),true);
CU_ASSERT_EQUAL(is_prime(10),false);
CU_ASSERT_EQUAL(is_prime(19),true);
CU_ASSERT_EQUAL(is_prime(25),false);
CU_ASSERT_EQUAL(is_prime(1000),false);
}
void test_ratio_equal(void)
{
struct ratio rs1={1,1,1};
struct ratio rs2={1,1,1};
CU_ASSERT_EQUAL(ratio_equal(rs1,rs2),true);
rs1.sigma_n=2;
CU_ASSERT_EQUAL(ratio_equal(rs1,rs2),false);
}
void test_compute_ratio(void)
{
struct ratio rs;
rs=make_ratio(1,1);
CU_ASSERT(ratio_equal(compute_ratio(1),rs));
rs=make_ratio(3,2);
CU_ASSERT(ratio_equal(compute_ratio(2),rs));
rs=make_ratio(4,3);
CU_ASSERT(ratio_equal(compute_ratio(3),rs));
rs=make_ratio(9,5);
CU_ASSERT(ratio_equal(compute_ratio(10),rs));
rs=make_ratio(8,5);//24,15
CU_ASSERT(ratio_equal(compute_ratio(15),rs));
CU_ASSERT(ratio_equal(compute_ratio(24),compute_ratio(91963648)) );
}
void test_p_multi_p(void)
{
CU_ASSERT(p_multi_p(4));
CU_ASSERT(p_multi_p(9));
CU_ASSERT(p_multi_p(25));
CU_ASSERT_FALSE(p_multi_p(2));
CU_ASSERT_FALSE(p_multi_p(3));
CU_ASSERT_FALSE(p_multi_p(6));
CU_ASSERT_FALSE(p_multi_p(10));
}