5 kyu

Getting along with Bernoulli's numbers

85 of 210g964

Description:

Let us define a function fsuch 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.
Fundamentals
Mathematics

More By Author:

Check out these other kata created by g964

Stats:

CreatedNov 8, 2017
PublishedNov 8, 2017
Warriors Trained2490
Total Skips517
Total Code Submissions1708
Total Times Completed210
Shell Completions5
Python Completions85
JavaScript Completions27
Julia Completions9
Java Completions13
C# Completions22
C++ Completions33
Scala Completions23
Ruby Completions17
Clojure Completions6
Racket Completions4
F# Completions7
Fortran Completions4
Haskell Completions7
Lua Completions12
Pascal Completions5
Perl Completions5
Raku Completions4
Total Stars71
% of votes with a positive feedback rating83% of 57
Total "Very Satisfied" Votes43
Total "Somewhat Satisfied" Votes9
Total "Not Satisfied" Votes5
Total Rank Assessments6
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • g964 Avatar
  • JohanWiltink Avatar
  • Voile Avatar
  • ZED.CWT Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad