-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfactor_test.c
79 lines (71 loc) · 1.67 KB
/
factor_test.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
#include "fundamental.h"
#define MAX_N_FACTORS 128
typedef unsigned long long uintmax_t;
static int factor (uintmax_t n0, int max_n_factors, uintmax_t *factors)
{
register uintmax_t n = n0, d, q;
int n_factors = 0;
if (n < 1)
return n_factors;
while (n % 2 == 0)
{
assert (n_factors < max_n_factors);
factors[n_factors++] = 2;
n /= 2;
}
/* The exit condition in the following loop is correct because
any time it is tested one of these 3 conditions holds:
(1) d divides n
(2) n is prime
(3) n is composite but has no factors less than d.
If (1) or (2) obviously the right thing happens.
If (3), then since n is composite it is >= d^2. */
d = 3;
do
{
q = n / d;
while (n == q * d)
{
assert (n_factors < max_n_factors);
factors[n_factors++] = d;
n = q;
q = n / d;
}
d += 2;
}
while (d <= q);
if (n != 1 || n0 == 1)
{
assert (n_factors < max_n_factors);
factors[n_factors++] = n;
}
return n_factors;
}
#if 1
#if NUM_SEQUENCE_DIMENSIONS!=1 || !defined(MULTIPLE_RESULTS)
#error "Bad factorisation configuration"
#endif
int sequence_func(result_t *retnums,dimension_t *array_indices)
{
uintmax_t factors[MAX_N_FACTORS];
int i,n_factors;
n_factors = factor ((uintmax_t)array_indices[0], MAX_N_FACTORS, factors);
#ifdef SPARSE_ARRAY_INDICES
#if NUM_ANSWERS!=2
#error "NUM_ANSWERS defined wrong"
#endif
if(n_factors==2)
*retnums=alloc_result();
else
{
*retnums=NULL;
return 0;
}
#else
*retnums=alloc_result(n_factors);
#endif
for(i=0;i<n_factors;i++)
(*retnums)->answer[i]=(number_t)factors[i];
return 0;
}
#endif