/* Author: Pate Williams (c) 1997 "Exercise 2.1. Computing li x. Write a function li(x) for li x, utilizing the continued fraction expansion li(exp(z)) = exp(z) / z - 1 / 1 - 1 / z - ... Compute the continued fraction backwards, starting with the term 10 / z. Test values can be found in Table 3 (compute li x as pi(x) + (li x - pi(x))." -Hans Riesel- See "Primes Numbers and Computer Methods for Factorization" by Hans Riesel second edition page 43. */ #include #include #define SIZE 256 double li(long x) { double A0, A1, Am1, B0, B1, Bm1, z = log(x); double a[SIZE], b[SIZE]; long i, i2; a[1] = x; for (i = 1; i <= 10; i++) { i2 = 2 * i; a[i2] = - i; a[i2 + 1] = - i; } for (i = 1; i <= 21; i += 2) b[i] = z; for (i = 2; i <= 22; i += 2) b[i] = 1; Am1 = 1; A0 = 0; B0 = 1; Bm1 = 0; for (i = 1; i <= 20; i++) { A1 = b[i] * A0 + a[i] * Am1; B1 = b[i] * B0 + a[i] * Bm1; Am1 = A0; Bm1 = B0; A0 = A1; B0 = B1; } return A1 / B1; } int main(void) { long x; printf("x = "); scanf("%ld", &x); printf("li %ld = %lf\n", x, li(x)); return 0; }