Faulhaber's triangle: Difference between revisions

m
syntax highlighting fixup automation
(Added a Scheme implementation.)
m (syntax highlighting fixup automation)
Line 39:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Line 196:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 211:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace FaulhabersTriangle {
Line 365:
}
}
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 381:
{{trans|C#}}
Uses C++ 17
<langsyntaxhighlight lang="cpp">#include <exception>
#include <iomanip>
#include <iostream>
Line 503:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 518:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.algorithm : fold;
import std.conv : to;
import std.exception : enforce;
Line 651:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 666:
=={{header|F_Sharp|F#}}==
===The Function===
<langsyntaxhighlight lang="fsharp">
// Generate Faulhaber's Triangle. Nigel Galloway: May 8th., 2018
let Faulhaber=let fN n = (1N - List.sum n)::n
Line 673:
yield! Faul t (b+1N)}
Faul [] 0N
</syntaxhighlight>
</lang>
===The Task===
<langsyntaxhighlight lang="fsharp">
Faulhaber |> Seq.take 10 |> Seq.iter (printfn "%A")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 693:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: kernel math math.combinatorics math.extras math.functions
math.ranges prettyprint sequences ;
 
Line 700:
[ [ nCk ] [ -1 swap ^ ] [ bernoulli ] tri * * * ] 2with map ;
 
10 [ faulhaber . ] each-integer</langsyntaxhighlight>
{{out}}
<pre>
Line 717:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' version 12-08-2017
' compile with: fbc -s console
' uses GMP
Line 804:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>The first 10 rows
Line 831:
{{trans|Kotlin}}
Except that there is no need to roll our own Frac type when we can use the big.Rat type from the Go standard library.
<langsyntaxhighlight lang="go">package main
 
import (
Line 912:
}
fmt.Println(sum.RatString())
}</langsyntaxhighlight>
 
{{out}}
Line 932:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">import java.math.MathContext
import java.util.stream.LongStream
 
Line 1,071:
println(sum.toBigInteger())
}
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 1,086:
=={{header|Haskell}}==
{{works with|GHC|8.6.4}}
<langsyntaxhighlight lang="haskell">import Data.Ratio (Ratio, denominator, numerator, (%))
 
------------------------ FAULHABER -----------------------
Line 1,158:
let widest f xs = maximum $ fmap (length . show . f) xs
in ((,) . widest numerator <*> widest denominator) $
concat xss</langsyntaxhighlight>
{{Out}}
<pre> 1
Line 1,219:
{{trans|Kotlin}}
{{works with|Java|8}}
<langsyntaxhighlight Javalang="java">import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Arrays;
Line 1,360:
System.out.println(sum.toBigInteger());
}
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 1,381:
 
(Further progress would entail implementing some hand-crafted representation of arbitrary precision integers – perhaps a bit beyond the intended scope of this task, and good enough motivation to use a different language)
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
 
// Order of Faulhaber's triangle -> rows of Faulhaber's triangle
Line 1,659:
]
);
})();</langsyntaxhighlight>
{{Out}}
<pre> 1
Line 1,694:
(*) The C implementation of jq does not have sufficient numeric precision for the "extra credit" task.
<langsyntaxhighlight lang="jq">include "Rational";
 
# Preliminaries
Line 1,761:
(faulhabersum(1000; 17) | rpp) ;
testfaulhaber</langsyntaxhighlight>
{{out}}
<pre>
Line 1,781:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function bernoulli(n)
A = Vector{Rational{BigInt}}(undef, n + 1)
for i in 0:n
Line 1,818:
 
testfaulhaber()
</langsyntaxhighlight>{{out}}
<pre>
1
Line 1,836:
=={{header|Kotlin}}==
Uses appropriately modified code from the Faulhaber's Formula task:
<langsyntaxhighlight lang="scala">// version 1.1.2
 
import java.math.BigDecimal
Line 1,956:
}
println(sum.toBigInteger())
}</langsyntaxhighlight>
 
{{out}}
Line 1,976:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang="lua">function binomial(n,k)
if n<0 or k<0 or n<k then return -1 end
if n==0 or k==0 then return 1 end
Line 2,098:
for i=0,9 do
faulhaber(i)
end</langsyntaxhighlight>
{{out}}
<pre> 1
Line 2,113:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Faulhaber]
bernoulliB[1] := 1/2
bernoulliB[n_] := BernoulliB[n]
Faulhaber[n_, p_] := 1/(p + 1) Sum[Binomial[p + 1, j] bernoulliB[j] n^(p + 1 - j), {j, 0, p}]
Table[Rest@CoefficientList[Faulhaber[n, t], n], {t, 0, 9}] // Grid
Faulhaber[1000, 17]</langsyntaxhighlight>
{{out}}
<pre>1
Line 2,135:
{{libheader|bignum}}
For the task, we could use the standard module “rationals” but for the extra task we need big numbers (and big rationals). We use third party module “bignum” for this purpose.
<langsyntaxhighlight Nimlang="nim">import algorithm, math, strutils
import bignum
 
Line 2,191:
echo ""
let fs18 = faulhaber(17) # 18th row.
echo fs18.evaluate(1000)</langsyntaxhighlight>
 
{{out}}
Line 2,212:
 
Row numbering below is 0-based, so row r has r+1 elements. Rather than use a rational number type, the program scales up row r by (r+1)!, which means that all the entries are integers.
<langsyntaxhighlight lang="pascal">
program FaulhaberTriangle;
uses uIntX, uEnums, // units in the library IntXLib4Pascal
Line 2,277:
WriteLn( 'by direct calc. = ' + sum.ToString);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,298:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use 5.010;
use List::Util qw(sum);
use Math::BigRat try => 'GMP';
Line 2,321:
my $n = Math::BigInt->new(1000);
my @r = faulhaber_triangle($p+1);
say "\n", sum(map { $r[$_] * $n**($_ + 1) } 0 .. $#r);</langsyntaxhighlight>
{{out}}
<pre>
Line 2,340:
=={{header|Phix}}==
{{trans|C#}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">pfrac</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- (0.8.0+)</span>
Line 2,399:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s \n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">frac_sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,418:
 
=={{header|Prolog}}==
<syntaxhighlight lang="prolog">
<lang Prolog>
ft_rows(Lz) :-
lazy_list(ft_row, [], Lz).
Line 2,452:
drop(N, Lz1, Lz2) :-
append(Pfx, Lz2, Lz1), length(Pfx, N), !.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 2,475:
{{trans|Haskell}}
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Faulhaber's triangle'''
 
from itertools import accumulate, chain, count, islice
Line 2,622:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Faulhaber's triangle:
Line 2,641:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory)
 
Line 2,664:
(require rackunit)
(check-equal? (sum-k^p:formulaic 17 1000)
(for/sum ((k (in-range 1 (add1 1000)))) (expt k 17))))</langsyntaxhighlight>
{{out}}
<pre>'((1) (1/2 1/2) (1/6 1/2 1/3) (0 1/4 1/2 1/4) (-1/30 0 1/3 1/2 1/5) (0 -1/12 0 5/12 1/2 1/6) (1/42 0 -1/6 0 1/2 1/2 1/7) (0 1/12 0 -7/24 0 7/12 1/2 1/8) (-1/30 0 2/9 0 -7/15 0 2/3 1/2 1/9) (0 -3/20 0 1/2 0 -7/10 0 3/4 1/2 1/10))
Line 2,674:
{{trans|Sidef}}
 
<syntaxhighlight lang="raku" perl6line># Helper subs
 
sub infix:<reduce> (\prev, \this) { this.key => this.key * (this.value - prev.value) }
Line 2,699:
my $p = 17;
my $n = 1000;
say sum faulhaber_triangle($p).kv.map: { $^value * $n**($^key + 1) }</langsyntaxhighlight>
{{out}}
<pre> 1
Line 2,715:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">Numeric Digits 100
Do r=0 To 20
ra=r-1
Line 2,813:
Parse Arg a,b
if b = 0 then return abs(a)
return gcd(b,a//b)</langsyntaxhighlight>
{{out}}
<pre> 1
Line 2,831:
=={{header|Ruby}}==
{{trans|D}}
<langsyntaxhighlight lang="ruby">class Frac
attr_accessor:num
attr_accessor:denom
Line 2,957:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre> 1
Line 2,972:
=={{header|Scala}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.math.MathContext
 
import scala.collection.mutable
Line 3,123:
println()
}
}</langsyntaxhighlight>
{{out}}
<pre> 1
Line 3,137:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<langsyntaxhighlight lang="scheme">; Return the first row-count rows of Faulhaber's Triangle as a vector of vectors.
(define faulhabers-triangle
(lambda (row-count)
Line 3,191:
(term-expt sum-to (* term-expt sum-to))
(sum 0 (+ sum (* (vector-ref coefs inx) term-expt))))
((>= inx (vector-length coefs)) sum)))))</langsyntaxhighlight>
{{out}}
<pre>
Line 3,211:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func faulhaber_triangle(p) {
{ binomial(p, _) * bernoulli(_) / p }.map(p ^.. 0)
}
Line 3,223:
 
say ''
say faulhaber_triangle(p+1).map_kv {|k,v| v * n**(k+1) }.sum</langsyntaxhighlight>
{{out}}
<pre>
Line 3,241:
 
Alternative solution:
<langsyntaxhighlight lang="ruby">func find_poly_degree(a) {
var c = 0
loop {
Line 3,262:
}
 
10.times { say faulhaber_triangle(_) }</langsyntaxhighlight>
{{out}}
<pre>
Line 3,279:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Class Frac
Line 3,428:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 1
Line 3,443:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import math.fractions
import math.big
 
Line 3,506:
}
println('')
}</langsyntaxhighlight>
 
{{out}}
Line 3,527:
{{libheader|Wren-math}}
{{libheader|Wren-big}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/math" for Int
import "/big" for BigRat
Line 3,587:
sum = sum + np*c
}
System.print(sum)</langsyntaxhighlight>
 
{{out}}
Line 3,608:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
Uses the code from [[Faulhaber's formula#zkl]]
<langsyntaxhighlight lang="zkl">foreach p in (10){
faulhaberFormula(p).apply("%7s".fmt).concat().println();
}
Line 3,616:
.walk() // -->(0, -3617/60 * 1000^2, 0, 595/3 * 1000^4 ...)
.reduce('+) // rat + rat + ...
.println();</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits