Faulhaber's triangle: Difference between revisions
m
syntax highlighting fixup automation
(Added a Scheme implementation.) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 39:
=={{header|C}}==
{{trans|C++}}
<
#include <stdio.h>
#include <stdlib.h>
Line 196:
return 0;
}</
{{out}}
<pre> 1
Line 211:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace FaulhabersTriangle {
Line 365:
}
}
}</
{{out}}
<pre> 1
Line 381:
{{trans|C#}}
Uses C++ 17
<
#include <iomanip>
#include <iostream>
Line 503:
return 0;
}</
{{out}}
<pre> 1
Line 518:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.conv : to;
import std.exception : enforce;
Line 651:
}
writeln;
}</
{{out}}
<pre> 1
Line 666:
=={{header|F_Sharp|F#}}==
===The Function===
<
// 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>
===The Task===
<
Faulhaber |> Seq.take 10 |> Seq.iter (printfn "%A")
</syntaxhighlight>
{{out}}
<pre>
Line 693:
=={{header|Factor}}==
<
math.ranges prettyprint sequences ;
Line 700:
[ [ nCk ] [ -1 swap ^ ] [ bernoulli ] tri * * * ] 2with map ;
10 [ faulhaber . ] each-integer</
{{out}}
<pre>
Line 717:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<
' compile with: fbc -s console
' uses GMP
Line 804:
Print : Print "hit any key to end program"
Sleep
End</
{{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.
<
import (
Line 912:
}
fmt.Println(sum.RatString())
}</
{{out}}
Line 932:
=={{header|Groovy}}==
{{trans|Java}}
<
import java.util.stream.LongStream
Line 1,071:
println(sum.toBigInteger())
}
}</
{{out}}
<pre> 1
Line 1,086:
=={{header|Haskell}}==
{{works with|GHC|8.6.4}}
<
------------------------ FAULHABER -----------------------
Line 1,158:
let widest f xs = maximum $ fmap (length . show . f) xs
in ((,) . widest numerator <*> widest denominator) $
concat xss</
{{Out}}
<pre> 1
Line 1,219:
{{trans|Kotlin}}
{{works with|Java|8}}
<
import java.math.MathContext;
import java.util.Arrays;
Line 1,360:
System.out.println(sum.toBigInteger());
}
}</
{{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)
<
// Order of Faulhaber's triangle -> rows of Faulhaber's triangle
Line 1,659:
]
);
})();</
{{Out}}
<pre> 1
Line 1,694:
(*) The C implementation of jq does not have sufficient numeric precision for the "extra credit" task.
<
# Preliminaries
Line 1,761:
(faulhabersum(1000; 17) | rpp) ;
testfaulhaber</
{{out}}
<pre>
Line 1,781:
=={{header|Julia}}==
<
A = Vector{Rational{BigInt}}(undef, n + 1)
for i in 0:n
Line 1,818:
testfaulhaber()
</
<pre>
1
Line 1,836:
=={{header|Kotlin}}==
Uses appropriately modified code from the Faulhaber's Formula task:
<
import java.math.BigDecimal
Line 1,956:
}
println(sum.toBigInteger())
}</
{{out}}
Line 1,976:
=={{header|Lua}}==
{{trans|C}}
<
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</
{{out}}
<pre> 1
Line 2,113:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
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]</
{{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.
<
import bignum
Line 2,191:
echo ""
let fs18 = faulhaber(17) # 18th row.
echo fs18.evaluate(1000)</
{{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.
<
program FaulhaberTriangle;
uses uIntX, uEnums, // units in the library IntXLib4Pascal
Line 2,277:
WriteLn( 'by direct calc. = ' + sum.ToString);
end.
</syntaxhighlight>
{{out}}
<pre>
Line 2,298:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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);</
{{out}}
<pre>
Line 2,340:
=={{header|Phix}}==
{{trans|C#}}
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,418:
=={{header|Prolog}}==
<syntaxhighlight 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>
{{Out}}
<pre>
Line 2,475:
{{trans|Haskell}}
{{Works with|Python|3.7}}
<
from itertools import accumulate, chain, count, islice
Line 2,622:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Faulhaber's triangle:
Line 2,641:
=={{header|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))))</
{{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"
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) }</
{{out}}
<pre> 1
Line 2,715:
=={{header|REXX}}==
<
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)</
{{out}}
<pre> 1
Line 2,831:
=={{header|Ruby}}==
{{trans|D}}
<
attr_accessor:num
attr_accessor:denom
Line 2,957:
end
main()</
{{out}}
<pre> 1
Line 2,972:
=={{header|Scala}}==
{{trans|Java}}
<
import scala.collection.mutable
Line 3,123:
println()
}
}</
{{out}}
<pre> 1
Line 3,137:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<
(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)))))</
{{out}}
<pre>
Line 3,211:
=={{header|Sidef}}==
<
{ 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</
{{out}}
<pre>
Line 3,241:
Alternative solution:
<
var c = 0
loop {
Line 3,262:
}
10.times { say faulhaber_triangle(_) }</
{{out}}
<pre>
Line 3,279:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Class Frac
Line 3,428:
End Sub
End Module</
{{out}}
<pre> 1
Line 3,443:
=={{header|Vlang}}==
{{trans|Go}}
<
import math.big
Line 3,506:
}
println('')
}</
{{out}}
Line 3,527:
{{libheader|Wren-math}}
{{libheader|Wren-big}}
<
import "/math" for Int
import "/big" for BigRat
Line 3,587:
sum = sum + np*c
}
System.print(sum)</
{{out}}
Line 3,608:
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
Uses the code from [[Faulhaber's formula#zkl]]
<
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();</
{{out}}
<pre>
|