#!/usr/bin/env python3

import sys
from math import *

class integral:
    def __init__(self, n):
        self.begin = n

    def function(self, x):
        total = (sin(x)) / x
        for k in range(1, self.begin + 1):
            total = total * (sin(x / (2 * k + 1))) / (x / (2 * k + 1))
        return total

    def rectangle(self):
        res = 1
        for i in range(1, 10000):
            res += self.function(i * 0.5)
        ret = res - 1
        res *= 0.5
        print("Rectangles:\nI%d = %.10f" % (self.begin,res))
        if format(res - pi / 2, ".10f") == "-0.0000000000":
            print("diff = %.10f\n" % -(res - pi / 2))
        else:
            print("diff = %.10f\n" % (res - pi / 2))
        return ret

    def trapeze(self):
        res = (self.rectangle() * 2) + 1 + self.function(5000)
        ret = res
        res *= 0.25
        print("Trapezoids:\nI%d = %.10f" % (self.begin,res))
        if format(res - pi / 2, ".10f") == "-0.0000000000":
            print("diff = %.10f\n" % -(res - pi / 2))
        else:
            print("diff = %.10f\n" % (res - pi / 2))
        return ret

    def simpson(self):
        res = 0
        for i in range(10000):
            res += self.function((i * 0.5) + 0.25)
        res *= 4
        res += self.trapeze()
        res *= 5000 / 60000
        print("Simpson:\nI%d = %.10f" % (self.begin,res))
        if format(res - pi / 2, ".10f") == "-0.0000000000":
            print("diff = %.10f" % -(res - pi / 2))
        else:
            print("diff = %.10f" % (res - pi / 2))

if __name__ == '__main__':
    if "-h" in sys.argv or "--help" in sys.argv:
        print("UASGE\n"
              "\t./110borwein n\n"
              "\nDESCRIPTION\n"
              "\tn\tconstant defining the integral to be computed")
        sys.exit(0)
    elif len(sys.argv) != 2:
        print("Usage: ./110borwein n, where n is a positive integer",
              file=sys.stderr)
        sys.exit(84)


    if sys.argv[1].isnumeric() == False:
        print("Usage: ./110borwein n, where n is a positive integer",
              file=sys.stderr)
        sys.exit(84)
    
    integral = integral(int(sys.argv[1]))
    integral.simpson()
