How to resolve the algorithm Faulhaber's triangle step by step in the Sidef programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Faulhaber's triangle step by step in the Sidef programming language

Table of Contents

Problem Statement

Named after Johann Faulhaber, the rows of Faulhaber's triangle are the coefficients of polynomials that represent sums of integer powers, which are extracted from Faulhaber's formula:

where

B

n

{\displaystyle B_{n}}

is the nth-Bernoulli number.

The first 5 rows of Faulhaber's triangle, are:

Using the third row of the triangle, we have:

k

1

n

k

2

=

1 6

n +

1 2

n

2

1 3

n

3

{\displaystyle \sum _{k=1}^{n}k^{2}={1 \over 6}n+{1 \over 2}n^{2}+{1 \over 3}n^{3}}

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Faulhaber's triangle step by step in the Sidef programming language

Source code in the sidef programming language

func faulhaber_triangle(p) {
    { binomial(p, _) * bernoulli(_) / p }.map(p ^.. 0)
}

{ |p|
    say faulhaber_triangle(p).map{ '%6s' % .as_rat }.join
} << 1..10

const p = 17
const n = 1000

say ''
say faulhaber_triangle(p+1).map_kv {|k,v| v * n**(k+1) }.sum


func find_poly_degree(a) {
    var c = 0
    loop {
        ++c
        a = a.map_cons(2, {|n,k| n-k })
        return 0 if a.is_empty
        return c if a.all { .is_zero }
    }
}

func faulhaber_triangle(n) {
    var a = (0..(n+2) -> accumulate { _**n })
    var c = find_poly_degree(a)

    var A = c.of {|n|
        c.of {|k| n**k }
    }

    A.msolve(a).slice(1)
}

10.times { say faulhaber_triangle(_) }


  

You may also check:How to resolve the algorithm Recaman's sequence step by step in the REXX programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the C programming language
You may also check:How to resolve the algorithm File input/output step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Ruth-Aaron numbers step by step in the Arturo programming language