Getting along with Bernoulli's numbers
Description:
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 + 2B = 0 ... gives ... B = - 1/2
1 + 3B + 3B = 0 ... gives ... B = 1/6
1 + 4B + 6B + 4B = 0 ... gives ... B = 0
1 + 5B + 10B + 10B + 5B = 0 ... gives ... B = - 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.
Similar Kata:
Stats:
Created | Nov 8, 2017 |
Published | Nov 8, 2017 |
Warriors Trained | 2490 |
Total Skips | 517 |
Total Code Submissions | 1708 |
Total Times Completed | 210 |
Shell Completions | 5 |
Python Completions | 85 |
JavaScript Completions | 27 |
Julia Completions | 9 |
Java Completions | 13 |
C# Completions | 22 |
C++ Completions | 33 |
Scala Completions | 23 |
Ruby Completions | 17 |
Clojure Completions | 6 |
Racket Completions | 4 |
F# Completions | 7 |
Fortran Completions | 4 |
Haskell Completions | 7 |
Lua Completions | 12 |
Pascal Completions | 5 |
Perl Completions | 5 |
Raku Completions | 4 |
Total Stars | 71 |
% of votes with a positive feedback rating | 83% of 57 |
Total "Very Satisfied" Votes | 43 |
Total "Somewhat Satisfied" Votes | 9 |
Total "Not Satisfied" Votes | 5 |
Total Rank Assessments | 6 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 7 kyu |