How to resolve the algorithm Faulhaber's formula step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Faulhaber's formula step by step in the Factor programming language

Table of Contents

Problem Statement

In mathematics,   Faulhaber's formula,   named after Johann Faulhaber,   expresses the sum of the p-th powers of the first n positive integers as a (p + 1)th-degree polynomial function of n,   the coefficients involving Bernoulli numbers.

Generate the first 10 closed-form expressions, starting with p = 0.

Let's start with the solution:

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

Source code in the factor programming language

USING: formatting kernel math math.combinatorics math.extras
math.functions regexp sequences ;

: faulhaber ( p -- seq )
    1 + dup recip swap dup <iota>
    [ [ nCk ] [ -1 swap ^ ] [ bernoulli ] tri * * * ] 2with map ;

: (poly>str) ( seq -- str )
    reverse [ 1 + "%un^%d" sprintf ] map-index reverse " + " join ;

: clean-up ( str -- str' )
    R/ n\^1\z/ "n" re-replace            ! Change n^1 to n.
    R/ 1n/ "n" re-replace                ! Change 1n to n.
    R/ \+ -/ "- " re-replace             ! Change + - to - .
    R/ [+-] 0n(\^\d+ )?/ "" re-replace ; ! Remove terms of zero.

: poly>str ( seq -- str ) (poly>str) clean-up ;

10 [ dup faulhaber poly>str "%d: %s\n" printf ] each-integer


  

You may also check:How to resolve the algorithm Percolation/Site percolation step by step in the Python programming language
You may also check:How to resolve the algorithm Modular inverse step by step in the PL/I programming language
You may also check:How to resolve the algorithm Memory allocation step by step in the Raku programming language
You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Draw a cuboid step by step in the Maxima programming language