Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563580 views
1
#include "typedef.h"
2
3
/**************************************************************************\
4
@---------------------------------------------------------------------------
5
@---------------------------------------------------------------------------
6
@ FILE: chin_remainder.c
7
@---------------------------------------------------------------------------
8
@---------------------------------------------------------------------------
9
@
10
\**************************************************************************/
11
/**************************************************************************\
12
@-------------------------------------------------------------------------
13
@ chinrest(x1, x2, p1, p2) calculates for positive primes p1,p2
14
@ and integers x1, x2 an integer x with
15
@ -(p1*p2)/2 < x < (p1*p2)/2 and
16
@ x kongruent x1 modulo p1 and x konguent x2 modulo p2
17
@-------------------------------------------------------------------------
18
\**************************************************************************/
19
int chin_remainder(x1, x2, p1, p2)
20
int x1, x2, p1, p2;
21
{
22
23
int a1, a2;
24
double q, q1, q2, res, waste;
25
int y1, y2;
26
27
a1 = p_inv(p2, p1);
28
a2 = p_inv(p1, p2);
29
q = ((double) p1) * ((double) p2);
30
q1 = ((double) p2) * ((double) a1);
31
q2 = ((double) p1) * ((double) a2);
32
33
y1 = x1%p1;
34
if(2*y1 <= -p1)
35
y1 +=p1;
36
if(2*y1 > p1)
37
y1 -=p1;
38
y2 = x2%p2;
39
if(2*y2 <= -p2)
40
y2 +=p2;
41
if(2*y2 > p2)
42
y2 -=p2;
43
if(y1 == y2)
44
return(y1);
45
res = ((double) y1) * q1 + ((double) y2) * q2;
46
modf(res/q, &waste);
47
res = res - waste * q;
48
while( (res/q) > 0.5)
49
res = res -q;
50
while( (res/q) < -0.5)
51
res = res + q;
52
return(((int) res));
53
}
54
55