sin(n) = -1 where n is an integer in radians

I saw a fun math problem on reddit the other day.

find a number n, so that sin(n)=-1, where n is an integer in radians; so sin(270 degrees) doesn’t count. Obviously it will never be exactly -1 but close enough for the difference to be lost in rounding.

$$ \sin(\frac{3\pi}{2} + 2\pi \cdot n) = -1 $$

I need the argument of sin function to be as close to an integer as possible. Call this integer m.

$$ \frac{3\pi}{2}+2\pi \cdot n \approx m $$

Solving for \( \pi \) leads to:

$$ \pi \approx \frac{p}{q} \approx \frac{2m}{4n+3} $$

If I have a rational approximation to \( \pi \) with an even numerator I can divide it by two get my m. I also have to make sure that the denominator is in the form of 4n+3. It’s possible to use continued fractions to approximate real numbers. Here’s a continued fraction sequence for pi.

The first rational approximation I learned in elementary school is 227 which is perfect.

> (sin 11) 

For the others I’ll have to evaluate the continued fraction to get my approximation of a simple fraction.

> (eval-terms (list 3 7 15 1 292 1))

> (sin (/ 104348 2))
> (eval-terms (list 3 7 15 1 292 1 1 1 2 1 3 1 14 2 1))
> (sin (/ 245850922 2))

Looks like a good candidate was found.

This is the code to evaluate a continued fraction coefficients. It’s very convenient that scheme has a native rational data type.

(define (eval-terms ts)
  (cond ((= (length ts) 1) (car ts))
          (+ (car ts) (/ 1 (eval-terms (cdr ts)))))))

(define (eval-terms-iter ts)
  (let ((rev-ts (reverse ts)))
    (define (ev-terms ts frac)
      (if (null? ts)
        (ev-terms (cdr ts) (+ (car ts) (/ 1 frac)))))
    (ev-terms rev-ts (car rev-ts))))