Jump to content

Lah numbers: Difference between revisions

m
syntax highlighting fixup automation
(Added Dyalect)
m (syntax highlighting fixup automation)
Line 40:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F lah(BigInt n, BigInt k)
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)</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang="algol68">BEGIN # calculate Lah numbers upto L( 12, 12 ) #
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</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="algolw">begin % calculate Lah numbers upto L( 12, 12 ) %
% 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.</langsyntaxhighlight>
{{out}}
<pre>
Line 238:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">factorial: function [n]-> product 1..n
 
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
]</langsyntaxhighlight>
 
{{out}}
Line 272:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<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>
</lang>
{{out}}
<pre>
Line 330:
 
The last line builds a table where the rows represent <code>n</code> and columns represent <code>k</code>.
<langsyntaxhighlight lang="bqn">F ← (≥⟜0)◶⟨0,×´1+↕⟩
Lah ← {
𝕨 𝕊 0: 0;
Line 339:
}
 
•Show Lah⌜˜↕12</langsyntaxhighlight><syntaxhighlight lang="text">┌─
╵ 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
┘</langsyntaxhighlight>
[https://mlochbaum.github.io/BQN/try.html#code=RiDihpAgKOKJpeKfnDAp4pe24p+oMCzDl8K0MSvihpXin6kKTGFoIOKGkCB7CiAg8J2VqCDwnZWKIDA6IDA7CiAgMCDwnZWKIPCdlak6IDA7CiAg8J2VqCDwnZWKIDE6IEYg8J2VqDsKICBuIPCdlYogazoKICAobj1rKeKKkeKfqCgobiDDl+KXi0Ygbi0xKSDDtyBrIMOX4peLRiBrLTEpICgwPeKKoinil7bDt+KAvzAgRiBuLWssIDHin6kKfQoK4oCiU2hvdyBMYWjijJzLnOKGlTEy Try It!]
 
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include <stdint.h>
#include <stdio.h>
 
Line 394:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 414:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Numerics;
Line 457:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0 1
Line 478:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">// Reference: https://en.wikipedia.org/wiki/Lah_number#Identities_and_relations
 
#include <algorithm>
Line 531:
std::cout << max << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 555:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.algorithm : map;
import std.bigint;
import std.range;
Line 609:
auto lambda = (int a) => lah(BigInt(100), BigInt(a));
writeln(iota(0, 100).map!lambda.max);
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 632:
=={{header|Dyalect}}==
 
<langsyntaxhighlight lang="dyalect">func fact(n) =>
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("")
})</langsyntaxhighlight>
 
{{out}}
Line 672:
=={{header|Factor}}==
{{works with|Factor|0.99 development version 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: combinators combinators.short-circuit formatting infix io
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 .</langsyntaxhighlight>
{{out}}
<pre>
Line 727:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight FreeBASIClang="freebasic">function factorial( n as uinteger ) as ulongint
if n = 0 then return 1
return n*factorial(n-1)
Line 761:
next k
print outstr
next n</langsyntaxhighlight>
{{out}}
<pre>
Line 792:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 853:
fmt.Println(max)
fmt.Printf("which has %d digits.\n", len(max.String()))
}</langsyntaxhighlight>
 
{{out}}
Line 880:
=={{header|Haskell}}==
{{trans|Python}}
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
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]</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 931:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<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>
</lang>
<pre>
lah&>~table~>:i.12
Line 984:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.util.HashMap;
Line 1,042:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,070:
but gojq is required to produce the precise maximum value.
 
<langsyntaxhighlight lang="jq">## Generic functions
 
def factorial: reduce range(2;.+1) as $i (1; . * $i);
Line 1,098:
 
def lah($n; $k): lah($n;$k;false);
</syntaxhighlight>
</lang>
'''The task'''
<langsyntaxhighlight lang="jq">def printlahtable($kmax):
def pad: lpad(12);
reduce range(0;$kmax+1) as $k ("n/k"|lpad(4); . + ($k|pad)),
Line 1,114:
 
task
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,137:
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
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))
</langsyntaxhighlight>{{out}}
<pre>
0 1 2 3 4 5 6 7 8 9 10 11 12
Line 1,191:
=={{header|Kotlin}}==
{{trans|Perl}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
 
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())
}</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,252:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[Lah]
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]]]</langsyntaxhighlight>
{{out}}
<pre>1 0 0 0 0 0 0 0 0 0 0 0 0
Line 1,281:
{{trans|Julia}}
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import math, strutils
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</langsyntaxhighlight>
 
{{out}}
Line 1,332:
{{libheader|ntheory}}
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,360:
 
say "\nMaximum value from the L(100, *) row:";
say max map { Lah(100,$_) } 0..100;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,384:
{{libheader|Phix/mpfr}}
{{trans|Go}}
<!--<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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,451:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de fact (N)
(if (=0 N)
1
Line 1,475:
(prinl "Maximum value from the L(100, *) row:")
(maxi '((N) (lah 100 N)) (range 0 100))
(prinl @@)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,498:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">% Reference: https://en.wikipedia.org/wiki/Lah_number#Identities_and_relations
 
:- 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).</langsyntaxhighlight>
 
{{out}}
Line 1,563:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def factorial(n):
if n == 0:
return 1
Line 1,599:
print maxVal
 
main()</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,622:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ table ] is ! ( n --> n )
1
101 times
Line 1,667:
[ 100 i^ lah
2dup < iff nip else drop ]
echo</langsyntaxhighlight>
 
{{out}}
Line 1,691:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">Lah_numbers <- function(n, k, type = "unsigned") {
if (n == k)
Line 1,722:
 
Table
</langsyntaxhighlight>
{{out}}
Line 1,745:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>constant @factorial = 1, |[\*] 1..*;
 
sub Lah (Int \n, Int \k) {
Line 1,768:
 
say "\nMaximum value from the L(100, *) row:";
say (^100).map( { Lah 100, $_ } ).max;</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,793:
 
Also, code was added to use memoization of the factorial calculations.
<langsyntaxhighlight lang="rexx">/*REXX pgm computes & display (unsigned) Stirling numbers of the 3rd kind (Lah numbers).*/
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</langsyntaxhighlight>
{{out|output|text=&nbsp; 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).
<langsyntaxhighlight lang="ruby">def fact(n) = n.zero? ? 1 : 1.upto(n).inject(&:*)
 
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>
</lang>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 1,904:
=={{header|Scheme}}==
{{works with|Chez Scheme}}
<langsyntaxhighlight lang="scheme">; Compute the Unsigned Lah number L(n, k).
(define lah
(lambda (n k)
Line 1,938:
(let ((val (lah 100 k)))
(when (> val max) (set! max val))))
(printf "~d~%" max))</langsyntaxhighlight>
{{out}}
<pre>The Unsigned Lah numbers L(n, k) up to L(12, 12):
Line 1,959:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func lah(n, k) {
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
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,007:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">import BigInt
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)")</langsyntaxhighlight>
 
{{out}}
Line 2,084:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc prod {from to} {
set r 1
if {$from <= $to} {
Line 2,162:
puts "([string length $maxv] digits, k=$maxk)"
}
main </langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">Unsigned Lah numbers L(n,k):
Line 2,185:
=={{header|VBScript}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="vb">' Lah numbers - VBScript - 04/02/2021
 
Function F(i,n)
Line 2,237:
End Sub 'Main
 
Main() </langsyntaxhighlight>
{{out}}
<pre>
Line 2,258:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Module1
Line 2,314:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Unsigned Lah numbers: L(n, k):
Line 2,337:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import math.big
 
fn main() {
Line 2,394:
println("which has ${max.str().len} digits.")
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,401:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for 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()
}</langsyntaxhighlight>
 
{{out}}
Line 2,449:
 
=={{header|Vala}}==
<langsyntaxhighlight Valalang="vala">uint64 factorial(uint8 n) {
uint64 res = 1;
if (n == 0) return res;
Line 2,481:
print("\n");
}
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">Unsigned Lah numbers: L(n, k):
Line 2,501:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn lah(n,k,fact=fcn(n){ [1..n].reduce('*,1) }){
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)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">// calculate entire table (quick), find max, find num digits in max
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));
}</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
Line 2,534:
</pre>
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
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);</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.