Lah numbers: Difference between revisions
m
syntax highlighting fixup automation
(Added Dyalect) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 40:
{{trans|Python}}
<
I k == 1
R factorial(n)
Line 64:
print("\nMaximum value from the L(100, *) row:")
V maxVal = max((0.<100).map(a -> lah(100, a)))
print(maxVal)</
{{out}}
Line 92:
{{Trans|ALGOL W}}
Uses Algol 68G's LONG LONG INT which has programmer-specifiable precision, so can find Lah numbers with n = 100 and need not use the scaling "trick" used by the Algol W sample.
<
PR precision 400 PR # set the precision for LONG LONG INT #
# returns Lah Number L( n, k ), f must be a table of factorials to at least n #
Line 138:
OD;
print( ( "maximum Lah number for n = 100: ", whole( max 100, 0 ), newline ) )
END</
{{out}}
<pre>
Line 162:
<br>
As L(12,2), L(12,3) and L(12,4) are too large for signed 32 bit integers, this sample scales the result by an appropriate power of 10 to enable the table to be printed up to L(12,12). Luckily, the problematic L(12,k) values all have at least 2 trailing zeros.
<
% sets lahNumber to L( n, k ), lahScale is returned as the power of 10 %
% lahNumber should be multiplied by %
Line 216:
end for_k
end for_n
end.</
{{out}}
<pre>
Line 238:
=={{header|Arturo}}==
<
lah: function [n,k][
Line 252:
loop 1..12 'x [
print @[pad to :string x 3] ++ map to [:string] map 1..12 'y -> lah x y 's -> pad s 10
]</
{{out}}
Line 272:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LAH_NUMBERS.AWK
# converted from C
Line 304:
return (factorial(n) * factorial(n-1)) / (factorial(k) * factorial(k-1)) / factorial(n-k)
}
</syntaxhighlight>
{{out}}
<pre>
Line 330:
The last line builds a table where the rows represent <code>n</code> and columns represent <code>k</code>.
<
Lah ← {
𝕨 𝕊 0: 0;
Line 339:
}
•Show Lah⌜˜↕12</
╵ 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0 0
Line 352:
0 3628800 16329600 21772800 12700800 3810240 635040 60480 3240 90 1 0
0 39916800 199584000 299376000 199584000 69854400 13970880 1663200 118800 4950 110 1
┘</
[https://mlochbaum.github.io/BQN/try.html#code=RiDihpAgKOKJpeKfnDAp4pe24p+oMCzDl8K0MSvihpXin6kKTGFoIOKGkCB7CiAg8J2VqCDwnZWKIDA6IDA7CiAgMCDwnZWKIPCdlak6IDA7CiAg8J2VqCDwnZWKIDE6IEYg8J2VqDsKICBuIPCdlYogazoKICAobj1rKeKKkeKfqCgobiDDl+KXi0Ygbi0xKSDDtyBrIMOX4peLRiBrLTEpICgwPeKKoinil7bDt+KAvzAgRiBuLWssIDHin6kKfQoK4oCiU2hvdyBMYWjijJzLnOKGlTEy Try It!]
=={{header|C}}==
{{trans|D}}
<
#include <stdio.h>
Line 394:
return 0;
}</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 414:
=={{header|C sharp|C#}}==
{{trans|D}}
<
using System.Linq;
using System.Numerics;
Line 457:
}
}
}</
{{out}}
<pre>0 1
Line 478:
=={{header|C++}}==
{{libheader|GMP}}
<
#include <algorithm>
Line 531:
std::cout << max << '\n';
return 0;
}</
{{out}}
Line 555:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.bigint;
import std.range;
Line 609:
auto lambda = (int a) => lah(BigInt(100), BigInt(a));
writeln(iota(0, 100).map!lambda.max);
}</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 632:
=={{header|Dyalect}}==
<
n is 0 ? 1 : (n^-1..<0).Fold(1, (acc, val) => acc * val)
Line 650:
(0..row).ForEach(i => lah(row, i) >> "{0,11}".Format >> print(terminator: ""))
print("")
})</
{{out}}
Line 672:
=={{header|Factor}}==
{{works with|Factor|0.99 development version 2019-07-10}}
<
kernel locals math math.factorials math.ranges prettyprint
sequences ;
Line 703:
"Maximum value from the 100 _ lah row:" print
100 [0,b] [ 100 swap lah ] map supremum .</
{{out}}
<pre>
Line 727:
=={{header|FreeBASIC}}==
<
if n = 0 then return 1
return n*factorial(n-1)
Line 761:
next k
print outstr
next n</
{{out}}
<pre>
Line 792:
=={{header|Go}}==
<
import (
Line 853:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</
{{out}}
Line 880:
=={{header|Haskell}}==
{{trans|Python}}
<
import Control.Monad (when)
Line 909:
printf "\nMaximum value from the L(100, *) row:\n%d\n"
(maximum $ lah 100 <$> ([0..100] :: [Integer]))
where zeroToTwelve = [0..12]</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 931:
=={{header|J}}==
<syntaxhighlight lang="j">
NB. use: k lah n
lah=: ! :(!&<: * %&!~)&x: NB. `%~' is shorter than `*inv'
Line 949:
extend_precision =: x: Monad
wordy_lah =: ((combinations but_1st decrement) times (into but_1st factorial))but_1st extend_precision Dyad
</syntaxhighlight>
<pre>
lah&>~table~>:i.12
Line 984:
=={{header|Java}}==
<
import java.math.BigInteger;
import java.util.HashMap;
Line 1,042:
}
</syntaxhighlight>
{{out}}
Line 1,070:
but gojq is required to produce the precise maximum value.
<
def factorial: reduce range(2;.+1) as $i (1; . * $i);
Line 1,098:
def lah($n; $k): lah($n;$k;false);
</syntaxhighlight>
'''The task'''
<
def pad: lpad(12);
reduce range(0;$kmax+1) as $k ("n/k"|lpad(4); . + ($k|pad)),
Line 1,114:
task
</syntaxhighlight>
{{out}}
<pre>
Line 1,137:
=={{header|Julia}}==
<
function lah(n::Integer, k::Integer, signed=false)
Line 1,169:
println("\nThe maxiumum of lah(100, _) is: ", maximum(k -> lah(BigInt(100), BigInt(k)), 1:100))
</
<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12
Line 1,191:
=={{header|Kotlin}}==
{{trans|Perl}}
<
fun factorial(n: BigInteger): BigInteger {
Line 1,230:
println("\nMaximum value from the L(100, *) row:")
println((0..100).map { lah(BigInteger.valueOf(100.toLong()), BigInteger.valueOf(it.toLong())) }.max())
}</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,252:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
Lah[n_?Positive, 0] := 0
Lah[0, k_?Positive] := 0
Line 1,259:
Lah[n_, k_] := (-1)^n (n! ((n - 1)!))/((k! ((k - 1)!)) ((n - k)!))
Table[Lah[i, j], {i, 0, 12}, {j, 0, 12}] // Grid
Max[Lah[100, Range[0, 100]]]</
{{out}}
<pre>1 0 0 0 0 0 0 0 0 0 0 0 0
Line 1,281:
{{trans|Julia}}
{{libheader|bignum}}
<
import bignum
Line 1,309:
let val = lah(n, k)
if val > maxval: maxval = val
echo "\nThe maximum value of lah(100, k) is ", maxval</
{{out}}
Line 1,332:
{{libheader|ntheory}}
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 1,360:
say "\nMaximum value from the L(100, *) row:";
say max map { Lah(100,$_) } 0..100;</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,384:
{{libheader|Phix/mpfr}}
{{trans|Go}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,427:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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;">"\nThe maximum l(100,k): %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m100</span><span style="color: #0000FF;">)))</span>
<!--</
{{out}}
<pre>
Line 1,451:
=={{header|PicoLisp}}==
<
(if (=0 N)
1
Line 1,475:
(prinl "Maximum value from the L(100, *) row:")
(maxi '((N) (lah 100 N)) (range 0 100))
(prinl @@)</
{{out}}
<pre>
Line 1,498:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
:- dynamic unsigned_lah_number_cache/3.
Line 1,541:
writeln('Maximum value of L(n,k) where n = 100:'),
max_unsigned_lah_number(100, M),
writeln(M).</
{{out}}
Line 1,563:
=={{header|Python}}==
<
if n == 0:
return 1
Line 1,599:
print maxVal
main()</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,622:
=={{header|Quackery}}==
<
1
101 times
Line 1,667:
[ 100 i^ lah
2dup < iff nip else drop ]
echo</
{{out}}
Line 1,691:
=={{header|R}}==
<
if (n == k)
Line 1,722:
Table
</
{{out}}
Line 1,745:
{{works with|Rakudo|2019.07.1}}
<syntaxhighlight lang="raku"
sub Lah (Int \n, Int \k) {
Line 1,768:
say "\nMaximum value from the L(100, *) row:";
say (^100).map( { Lah 100, $_ } ).max;</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,793:
Also, code was added to use memoization of the factorial calculations.
<
parse arg lim . /*obtain optional argument from the CL.*/
if lim=='' | lim=="," then lim= 12 /*Not specified? Then use the default.*/
Line 1,833:
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: parse arg z; if !.z\==. then return !.z; !=1; do f=2 to z; !=!*f; end; !.z=!; return !
maxer: max#.k= max(max#.k, @.n.k); max#.b= max(max#.b, @.n.k); return</
{{out|output|text= when using the default input:}}
<pre>
Line 1,859:
=={{header|Ruby}}==
Works with Ruby 3.0 (end-less method; end-less and begin-less range).
<
def lah(n, k)
Line 1,881:
puts "\nMaximum value from the L(100, *) row:";
puts (1..100).map{|a| lah(100,a)}.max
</syntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,904:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<
(define lah
(lambda (n k)
Line 1,938:
(let ((val (lah 100 k)))
(when (> val max) (set! max val))))
(printf "~d~%" max))</
{{out}}
<pre>The Unsigned Lah numbers L(n, k) up to L(12, 12):
Line 1,959:
=={{header|Sidef}}==
<
stirling3(n, k)
#binomial(n-1, k-1) * n!/k! # alternative formula
Line 1,980:
say "\nMaximum value from the L(#{n}, *) row:"
say { lah(n, _) }.map(^n).max
}</
{{out}}
<pre>
Line 2,007:
{{trans|Kotlin}}
<
import Foundation
Line 2,061:
let maxLah = (0...100).map({ lah(n: BigInt(100), k: BigInt($0)) }).max()!
print("Maximum value from the L(100, *) row: \(maxLah)")</
{{out}}
Line 2,084:
=={{header|Tcl}}==
<
set r 1
if {$from <= $to} {
Line 2,162:
puts "([string length $maxv] digits, k=$maxk)"
}
main </
{{out}}
<pre style="font-size:83%">Unsigned Lah numbers L(n,k):
Line 2,185:
=={{header|VBScript}}==
{{trans|Visual Basic .NET}}
<
Function F(i,n)
Line 2,237:
End Sub 'Main
Main() </
{{out}}
<pre>
Line 2,258:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 2,314:
End Sub
End Module</
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 2,337:
=={{header|Vlang}}==
{{trans|go}}
<
fn main() {
Line 2,394:
println("which has ${max.str().len} digits.")
}
</syntaxhighlight>
{{out}}
Line 2,401:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var fact = Fn.new { |n|
Line 2,426:
for (k in 0..n) System.write("%(Fmt.d(10, lah.call(n, k))) ")
System.print()
}</
{{out}}
Line 2,449:
=={{header|Vala}}==
<
uint64 res = 1;
if (n == 0) return res;
Line 2,481:
print("\n");
}
}</
{{out}}
<pre style="font-size:83%">Unsigned Lah numbers: L(n, k):
Line 2,501:
=={{header|zkl}}==
<
if(n==k) return(1);
if(k==1) return(fact(n));
if(n<1 or k<1) return(0);
(fact(n)*fact(n - 1)) /(fact(k)*fact(k - 1)) /fact(n - k)
}</
<
N,mx := 12, [1..N].apply(fcn(n){ [1..n].apply(lah.fp(n)) }).flatten() : (0).max(_);
fmt:="%%%dd".fmt("%d".fmt(mx.numDigits + 1)).fmt; // "%9d".fmt
Line 2,514:
foreach row in ([0..N]){
println("%3d".fmt(row), [0..row].pump(String, lah.fp(row), fmt));
}</
{{out}}
<pre style="font-size:83%">
Line 2,534:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<
N=100;
L100:=[1..N].apply(lah.fpM("101",BI(N),fcn(n){ BI(n).factorial() }))
.reduce(fcn(m,n){ m.max(n) });
println("Maximum value from the L(%d, *) row (%d digits):".fmt(N,L100.numDigits));
println(L100);</
{{out}}
<pre style="font-size:83%">
|