-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtotient.C
98 lines (78 loc) · 1.67 KB
/
totient.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
/*
* totient.C
*
* FUNCTION:
* display euler totient
*
* HISTORY:
* quick hack -- Linas Vepstas October 1989
* modernize -- Linas Vepstas March 1996
* more stuff -- January 2000
* more stuff -- October 2004
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "brat.h"
#include "totient.h"
static int max_terms;
static void totient_series_c (double re_q, double im_q, double *prep, double *pimp)
{
double tmp;
int i;
*prep = 0.0;
*pimp = 0.0;
double rep = 0.0;
double imp = 0.0;
double qpr = 1.0;
double qpi = 0.0;
double qpmod = re_q*re_q+im_q*im_q;
if (1.0 <= qpmod) return;
for (i=0; i<max_terms; i++)
{
double t = totient_phi (i+1);
t *= (i+1);
t *= (i+1);
t *= (i+1);
rep += qpr *t;
imp += qpi *t;
/* compute q^k */
tmp = qpr*re_q - qpi * im_q;
qpi = qpr*im_q + qpi * re_q;
qpr = tmp;
qpmod = qpr*qpr + qpi*qpi;
if (qpmod < 1.0e-30) break;
}
if (max_terms-1 < i)
{
// printf ("not converged re=%g im=%g modulus=%g\n", re_q, im_q, qpmod);
}
#if NO_NOT_THIS
/* multiply by (1-q)^2 */
qpr = 1.0 - re_q;
qpi = - im_q;
tmp = qpr*qpr - qpi * qpi;
qpi = 2.0*qpr*qpi;
qpr = tmp;
tmp = qpr*rep - qpi * imp;
imp = qpr*imp + qpi * rep;
rep = tmp;
#endif
/* multiply by (1-|q|)^2 */
tmp = 1.0 - sqrt (re_q*re_q + im_q*im_q);
tmp *= tmp;
rep *= tmp;
imp *= tmp;
*prep = rep;
*pimp = imp;
}
static double totient_series (double re_q, double im_q, int itermax, double param)
{
max_terms = itermax;
double rep, imp;
totient_series_c (re_q, im_q, &rep, &imp);
// return sqrt (rep*rep+imp*imp);
return rep;
}
DECL_MAKE_HEIGHT(totient_series);
/* --------------------------- END OF LIFE ------------------------- */