Let us define a function `f`such as:

- (1) for k positive odd integer > 2 : 



- (2) for k positive even integer >= 2 : 



- (3) for k positive integer > 1 : 




where `|x|` is `abs(x)` and Bk the kth Bernoulli number.

`f` is not defined for `0, 1, -1`. These values will not be tested.

# Guidelines for Bernoulli numbers:

https://en.wikipedia.org/wiki/Bernoulli_number

http://mathworld.wolfram.com/BernoulliNumber.html

https://www.codewars.com/kata/bernoulli-numbers-1

There is more than one way to calculate them. You can make Pascal triangle and then with the basic formula below generate all Bernoulli numbers.

1 + 2B1 = 0 ... gives ...
  B1 = - 1/2

1 + 3B1 + 3B2 = 0 ... gives ...    B2        = 1/6

1 + 4B1 + 6B2 + 4B3 = 0 ... gives ... B3 = 0

1 + 5B1 + 10B2 + 10B3 + 5B4 = 0 ... gives ... B4 = - 1/30

... and so on


# Task 

Function `series(k, nb)` returns (1), (2) or (3) where `k` is the `k` parameter of `f` and `nb` the upper bound in the summation of (1). `nb` is of no use for (2) and (3) but for the sake of simplicity it will always appear in the tests even for cases (2) and (3) with a value of `0`.

```
Examples
S(2, 0) = 1.644934066848224....
S(3, 100000) = 1.20205690310973....
S(4, 0) = 1.08232323371113.....
S(-5, 0) = -0.003968253968....
S(-11, 0) = 0.02109279609279....
```

# Notes

- For Java, C#, C++: `k` should be such as `-27 <= k <= 20`; otherwise `-30 <= k <= 30` and be careful of 32-bit integer overflow.
- Translators are welcome.

Difficulty: EASY
Tags: Fundamentals

Mathematics

Starter code:
def comb(n,k):
	

Solutions:
from math import factorial, pi
from fractions import Fraction

def comb(n, k):
	return factorial(n) // (factorial(n - k) * factorial(k))

def bernoulli(m):
	b = [1]
	for i in range(1, m + 1):
		n = 0
		for k in range(0, i):
			n += comb(i + 1, k) * b[k]
		b.append(Fraction(-n, i + 1))
	return b
b = bernoulli(31)

def series(k, nb):
	if k < 0:
		k = -k
		return b[k + 1] * (-1) ** k / (k + 1)
	elif k % 2 == 1:
		return sum((1 / n ** k for n in range(1, nb + 1)))
	else:
		return abs(b[k]) * (2 * pi) ** k / (2 * factorial(k))


import math
from scipy.special import comb

def Bornoulli(i):
	if i == 0:
		return 1
	elif i == 1:
		return -0.5
	elif i % 2 == 1:
		return 0
	else:
		s = 0
		for k in range(i):
			s -= comb(i, k) * Bornoulli(k) / (i - k + 1)
		return s

def series(k, nb):
	if k >= 2:
		if k % 2 == 1:
			return sum((1 / i ** k for i in range(1, nb + 1)))
		else:
			return 0.5 * abs(Bornoulli(k)) * (2 * math.pi) ** k / math.factorial(k)
	elif k < -1:
		return (-1) ** k * Bornoulli(-k + 1) / (-k + 1)


import math
from math import pi

def comb(n, k):
	return math.factorial(n) / math.factorial(n - k) / math.factorial(k)

def bernoulli():
	A = [1]
	yield A[0]
	counter = 0
	while True:
		somma = 0
		p = len(A)
		for i in range(len(A)):
			somma += A[i] * comb(p + 1, i) / (p + 1)
		A.append(-somma)
		if len(A) < 4 or len(A) % 2 == 1:
			counter += 1
			yield A[counter]

def series(k, nb):
	if k % 2 == 1 and k > 2:
		return sum(map(lambda x: 1 / x ** k, range(1, nb + 1)))
	bn2 = [ix for ix in zip(range(abs(k) + 2), bernoulli())]
	bn2 = [b for (i, b) in bn2]
	if k % 2 == 0 and k >= 2:
		return 1 / 2 * abs(bn2[k]) * (2 * pi) ** k / math.factorial(k)
	else:
		k = abs(k)
		return (-1) ** k * bn2[k + 1] / (k + 1)


from math import factorial as f
import numpy as np

def combination(n, k):
	return f(n) // (f(k) * f(n - k))

def bern(target_level):
	b0 = 1
	b1 = -(1.0 / 2.0)
	bern_nums = [b0, b1]
	n = 3
	while n <= target_level + 1:
		ser = []
		k = 0
		comb_list = []
		while k < n - 1:
			comb_list.append(combination(n, k))
			k += 1
		result_l = [c * b for (c, b) in zip(comb_list, bern_nums)]
		result_s = 0
		for element in result_l:
			result_s += element
		b_k = -result_s / combination(n, k)
		bern_nums.append(b_k)
		n += 1
	return bern_nums[-1]

def series(k, nb):
	if k > 2 and k % 2 != 0:
		n = 1
		result = 0
		while n <= nb:
			result += 1 / n ** k
			n += 1
		return result
	elif k >= 2 and k % 2 == 0:
		bern_num = bern(k)
		return 1.0 / 2.0 * abs(bern_num) * (2 * np.pi) ** k / f(k)
	else:
		k = abs(k)
		bern_num = bern(k + 1)
		return (-1) ** k * bern_num / (k + 1)
	return 0


from fractions import Fraction
import math

def comb(m, n):
	return math.factorial(m) // (math.factorial(n) * math.factorial(m - n))

def simplify(a, b):
	return [a // math.gcd(a, b), b // math.gcd(a, b)]
arr = [[1, 1], [-1, 2]]

def _b(n):
	product_denominators = 1
	triangles = []
	for i in range(0, n):
		p = comb(n, i)
		triangles.append(p)
		if i < len(arr):
			product_denominators *= arr[i][1]
	sum_numerator = 0
	for i in range(0, n - 1):
		p = product_denominators * arr[i][0] // arr[i][1] * triangles[i]
		sum_numerator += p
	[a, b] = simplify(-sum_numerator, product_denominators * triangles[-1])
	arr.append([a, b])

def b(n):
	if n % 2 and n > 1:
		return 0
	if n >= len(arr):
		for i in range(len(arr) + 1, n + 2):
			_b(i)
	return arr[n]

def series(k, nb):
	if k > 0 and k % 2 == 0:
		temp = b(k)
		bk = abs(temp[0] / temp[1])
		fk = math.factorial(k)
		return 1 / 2 * bk * (2 * math.pi) ** k / fk
	elif k > 0 and k % 2:
		sum = 0
		for i in range(1, nb):
			sum += 1 / i ** k
		return sum
	elif k < 0:
		temp = b(1 - k)
		bk = temp[0] / temp[1] if type(temp) is list else temp
		return (-1) ** (-k) * bk / (1 - k)