Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/Compile-time_calculation

View file

@ -0,0 +1,11 @@
Some programming languages allow calculation of values at compile time.
;Task:
Calculate &nbsp; <big> 10! </big> &nbsp; (ten factorial) &nbsp; at compile time.
Print the result when the program is run.
Discuss what limitations apply to compile-time calculations in your language.
<br><br>

View file

@ -0,0 +1,7 @@
COMPCALA CSECT
L R1,=A(FACT10) r1=10!
XDECO R1,PG
XPRNT PG,L'PG print buffer
BR R14 exit
FACT10 EQU 10*9*8*7*6*5*4*3*2*1 factorial computation
PG DS CL12

View file

@ -0,0 +1,22 @@
MACRO
&LAB FACT &REG,&N parameters
&F SETA 1 f=1
&I SETA 1 i=1
.EA AIF (&I GT &N).EB ea: if i>n then goto eb
&F SETA &F*&I f=f*i
&I SETA &I+1 i=i+1
AGO .EA goto ea
.EB ANOP eb:
MNOTE 0,'Load &REG with &N! = &F' macro note
&LAB L &REG,=A(&F) load reg with factorial
MEND macro end
COMPCALB CSECT
USING COMPCALB,R12 base register
LR R12,R15 set base register
FACT R1,10 macro call
XDECO R1,PG
XPRNT PG,L'PG print buffer
BR R14 exit
PG DS CL12
YREGS
END COMPCALB

View file

@ -0,0 +1,33 @@
; Display the value of 10!, which is precomputed at assembly time
; on any Commodore 8-bit.
.ifndef __CBM__
.error "Target must be a Commodore system."
.endif
; zero-page work pointer
temp = $fb
; ROM routines used
chrout = $ffd2
.code
lda #<tenfactorial
sta temp
lda #>tenfactorial
sta temp+1
ldy #0
loop:
lda (temp),y
beq done
jsr chrout
iny
bne loop
done:
rts
.data
; the actual value to print
tenfactorial: .byte 13,"10! = ",.string(10*9*8*7*6*5*4*3*2*1),13,0

View file

@ -0,0 +1,5 @@
tenfactorial equ 10*9*8*7*6*5*4*3*2
MOVE.L #tenfactorial,D1 ;load the constant integer 10! into D1
LEA tenfactorial,A1 ;load into A1 the memory address 10! = 0x375F00
ADD.L #tenfactorial,D2 ;add 10! to whatever is stored in D2 and store the result in D2

View file

@ -0,0 +1,2 @@
mov ax,8*7*6*5*4*3*2 ;8! equals 40,320 or 0x9D80
call Monitor ;unimplemented routine, displays the register contents to screen

View file

@ -0,0 +1,7 @@
with Ada.Text_Io;
procedure CompileTimeCalculation is
Factorial : constant Integer := 10*9*8*7*6*5*4*3*2*1;
begin
Ada.Text_Io.Put(Integer'Image(Factorial));
end CompileTimeCalculation;

View file

@ -0,0 +1,16 @@
with Ada.Text_Io;
procedure CompileTimeCalculation is
function Factorial (Int : in Integer) return Integer is
begin
if Int > 1 then
return Int * Factorial(Int-1);
else
return 1;
end if;
end;
Fact10 : Integer := Factorial(10);
begin
Ada.Text_Io.Put(Integer'Image(Fact10));
end CompileTimeCalculation;

View file

@ -0,0 +1,12 @@
with Ada.Text_IO;
procedure Unbounded_Compile_Time_Calculation is
F_10 : constant Integer := 10*9*8*7*6*5*4*3*2*1;
A_11_15 : constant Integer := 15*14*13*12*11;
A_16_20 : constant Integer := 20*19*18*17*16;
begin
Ada.Text_IO.Put_Line -- prints out
("20 choose 10 =" & Integer'Image((A_11_15 * A_16_20 * F_10) / (F_10 * F_10)));
-- Ada.Text_IO.Put_Line -- would not compile
-- ("Factorial(20) =" & Integer'Image(A_11_15 * A_16_20 * F_10));
end Unbounded_Compile_Time_Calculation;

View file

@ -0,0 +1,2 @@
Ada.Text_IO.Put_Line -- would not compile
("Factorial(20) =" & Integer'Image(A_11_15 * A_16_20 * F_10));

View file

@ -0,0 +1,18 @@
-- This handler must be declared somewhere above the relevant property declaration
-- so that the compiler knows about it when compiling the property.
on factorial(n)
set f to 1
repeat with i from 2 to n
set f to f * i
end repeat
return f
end factorial
property compiledValue : factorial(10)
-- Or of course simply:
-- property compiledValue : 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
on run
return compiledValue
end run

View file

@ -0,0 +1,19 @@
f10: 1*2*3*4*5*6*7*8*9*10 ; this is evaluated at compile time
; the generate bytecode is:
; [ :bytecode
; ================================
; DATA
; ================================
; 0: 3628800 :integer
; 1: f10 :label
; ================================
; CODE
; ================================
; push0
; store1
; end
; ]
print f10

View file

@ -0,0 +1 @@
CONST factorial10 = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10

View file

@ -0,0 +1,2 @@
DIM factorial10 AS LONG
factorial10 = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10

View file

@ -0,0 +1,2 @@
factorial = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
print "10! = "; factorial # 3628800

View file

@ -0,0 +1,18 @@
#include <iostream>
template<int i> struct Fac
{
static const int result = i * Fac<i-1>::result;
};
template<> struct Fac<1>
{
static const int result = 1;
};
int main()
{
std::cout << "10! = " << Fac<10>::result << "\n";
return 0;
}

View file

@ -0,0 +1,12 @@
#include <stdio.h>
constexpr int factorial(int n) {
return n ? (n * factorial(n - 1)) : 1;
}
constexpr int f10 = factorial(10);
int main() {
printf("%d\n", f10);
return 0;
}

View file

@ -0,0 +1,12 @@
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call ___main
movl $3628800, 4(%esp)
movl $LC0, (%esp)
call _printf
movl $0, %eax
leave
ret

View file

@ -0,0 +1,10 @@
using System;
public static class Program
{
public const int FACTORIAL_10 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
static void Main()
{
Console.WriteLine(FACTORIAL_10);
}
}

View file

@ -0,0 +1,21 @@
.class public auto ansi abstract sealed beforefieldinit Program
extends [System.Runtime]System.Object
{
// Fields
.field public static literal int32 FACTORIAL_10 = int32(3628800)
// Methods
.method private hidebysig static
void Main () cil managed
{
// Method begins at RVA 0x2050
// Code size 11 (0xb)
.maxstack 8
.entrypoint
IL_0000: ldc.i4 3628800
IL_0005: call void [System.Console]System.Console::WriteLine(int32)
IL_000a: ret
} // end of method Program::Main
} // end of class Program

View file

@ -0,0 +1,9 @@
using System;
static class Program
{
static void Main()
{
Console.WriteLine(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1);
}
}

View file

@ -0,0 +1,11 @@
using System;
static class Program
{
static void Main()
{
int factorial;
factorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1;
Console.WriteLine(factorial);
}
}

View file

@ -0,0 +1,18 @@
.class private auto ansi abstract sealed beforefieldinit Program
extends [System.Runtime]System.Object
{
// Methods
.method private hidebysig static
void Main () cil managed
{
// Method begins at RVA 0x2050
// Code size 11 (0xb)
.maxstack 8
.entrypoint
IL_0000: ldc.i4 3628800
IL_0005: call void [System.Console]System.Console::WriteLine(int32)
IL_000a: ret
} // end of method Program::Main
} // end of class Program

View file

@ -0,0 +1,10 @@
#include <stdio.h>
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fac ORDER_PP_FN( \
8fn(8X, 8seq_fold(8times, 1, 8seq_iota(1, 8inc(8X)))) )
int main(void) {
printf("10! = %d\n", ORDER_PP( 8to_lit( 8fac(10) ) ) );
return 0;
}

View file

@ -0,0 +1,6 @@
#include <stdio.h>
const int val = 2*3*4*5*6*7*8*9*10;
int main(void) {
printf("10! = %d\n", val );
return 0;
}

View file

@ -0,0 +1,16 @@
macro int factorial($n)
{
$if ($n == 0):
return 1;
$else:
return $n * factorial($n - 1);
$endif;
}
extern fn void printf(char *fmt, ...);
fn void main()
{
int x = factorial(10);
printf("10! = %d\n", x);
}

View file

@ -0,0 +1,2 @@
(defn fac [n] (apply * (range 1 (inc n))))
(defmacro ct-factorial [n] (fac n))

View file

@ -0,0 +1,2 @@
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun factorial ...))

View file

@ -0,0 +1,6 @@
(defmacro ct-factorial (n)
(factorial n))
...
(print (ct-factorial 10))

View file

@ -0,0 +1,6 @@
(defmacro ct-factorial (n)
`(quote ,(factorial n)))
; or, equivalently,
(defmacro ct-factorial (n)
`',(factorial n))

View file

@ -0,0 +1 @@
(print (load-time-value (factorial 10)))

View file

@ -0,0 +1 @@
(print (#. (factorial 10)))

View file

@ -0,0 +1,4 @@
(define-compiler-macro factorial (&whole form arg)
(if (constantp arg)
(factorial arg)
form))

View file

@ -0,0 +1,15 @@
long fact(in long x) pure nothrow @nogc {
long result = 1;
foreach (immutable i; 2 .. x + 1)
result *= i;
return result;
}
void main() {
// enum means "compile-time constant", it forces CTFE.
enum fact10 = fact(10);
import core.stdc.stdio;
printf("%ld\n", fact10);
}

View file

@ -0,0 +1,11 @@
__Dmain
push EAX
mov EAX,offset FLAT:_DATA
push 0
push 0375F00h
push EAX
call near ptr _printf
add ESP,0Ch
xor EAX,EAX
pop ECX
ret

View file

@ -0,0 +1 @@
const fact10 = Factorial(10);

View file

@ -0,0 +1,66 @@
[Demo of calculating a constant in an interlude at load time.
EDSAC program, Initial Orders 2.]
[Arrange the storage]
T46K P56F [N parameter: library subroutine P7 to print integer]
T47K P100F [M parameter: main routine]
E25K TM GK [M parameter, main routine]
T#Z PF [clear 35-bit value at relative locations
0 & 1, including the middle ("sandwich") bit]
T2#Z PF [same for 2 & 3]
T4#Z PF [same for 4 & 5]
TZ [resume normal loading at relative location 0]
[Storage for interlude, must be at even address]
[0] PD PF [35-bit factorial, initially integer 1]
[2] PD PF [35-bit factor 1..10, initially integer 1]
[4] PF K4096F [to save multiplier register (MR), initially floating point -1]
[6] PD [17-bit integer 1]
[7] P5F [17-bit integer 10 (or number whose factorial is required)]
[8] PF [dump for clearing acc]
[Executable code for interlude; here with acc = 0]
[9] N4#@ [acc := MR, by subtracting (-1 * MR)]
T4#@ [save MR over interlude]
[11] T8@ [start of loop: clear acc]
A2@ [acc := factor]
A6@ [add 1]
T2@ [update factor, clear acc]
H2#@ [MR := factor, extended to 35 bits]
V#@ [times 35-bit product, result in acc]
L1024F L1024F L256F [integer scaling: shift 34 left]
T#@ [update product]
A2@ [acc := factor just used]
S7@ [is it 10 yet?]
G11@ [if not, loop back]
H4#@ [restore MR before exit from interlude]
E25F [pass control back to initial orders]
[At this point the interlude has been loaded but not executed.
The next control combination starts execution.]
E9Z [pass control to relative location 9 above]
PF [value in accumulator when control is passed: here = 0]
[After the interlude, loading resumes here.]
T2Z [resume normal loading at relative location 2,
overwriting the above interlude except the factorial]
[Teleprinter characters]
[2] #F [set figures mode]
[3] @F [carriage return]
[4] &F [line feed]
[Enter here with acc = 0]
[5] O2@ [set teleprinter to figures]
A#@ [acc := factorial, as calculated in the interlude]
TD [pass to print subroutine]
[8] A8@ GN [call print subroutine]
O3@ O4@ [print CR, LF]
O2@ [dummy character to flush teleprinter buffer]
ZF [stop]
E25K TN [N parameter]
[Library subroutine P7, prints 35-bit strictly positive integer in 0D.]
[10 characters, right justified, padded left with spaces.]
[Even address; 35 storage locations; working position 4D.]
GKA3FT26@H28#@NDYFLDT4DS27@TFH8@S8@T1FV4DAFG31@SFLDUFOFFFSF
L4FT4DA1FA27@G11@XFT28#ZPFT27ZP1024FP610D@524D!FO30@SFL8FE22@
E25K TM GK [M parameter again]
E5Z [define entry point]
PF [acc = 0 on entry]

View file

@ -0,0 +1,5 @@
(define-constant DIX! (factorial 10))
(define-constant DIX!+1 (1+ DIX!))
(writeln DIX!+1)
3628801

View file

@ -0,0 +1,3 @@
: factorial ( n -- n! ) [1,b] product ;
CONSTANT: 10-factorial $[ 10 factorial ]

View file

@ -0,0 +1,7 @@
: fac ( n -- n! ) 1 swap 1+ 2 max 2 ?do i * loop ;
: main ." 10! = " [ 10 fac ] literal . ;
see main
: main
.\" 10! = " 3628800 . ; ok

View file

@ -0,0 +1,9 @@
: more ( "digits" -- ) \ store "1234" as 1 c, 2 c, 3 c, 4 c,
parse-word bounds ?do
i c@ [char] 0 - c,
loop ;
create bignum
more 73167176531330624919225119674426574742355349194934
more 96983520312774506326239578318016984801869478851843
...

View file

@ -0,0 +1,8 @@
program test
implicit none
integer,parameter :: t = 10*9*8*7*6*5*4*3*2 !computed at compile time
write(*,*) t !write the value the console.
end program test

View file

@ -0,0 +1,8 @@
' FB 1.05.0 Win64
' Calculations can be done in a Const declaration at compile time
' provided only literals or other constant expressions are used
Const factorial As Integer = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
Print factorial ' 3628800
Sleep

View file

@ -0,0 +1,7 @@
package main
import "fmt"
func main() {
fmt.Println(2*3*4*5*6*7*8*9*10)
}

View file

@ -0,0 +1,7 @@
module Factorial where
import Language.Haskell.TH.Syntax
fact n = product [1..n]
factQ :: Integer -> Q Exp
factQ = lift . fact

View file

@ -0,0 +1,4 @@
{-# LANGUAGE TemplateHaskell #-}
import Factorial
main = print $(factQ 10)

View file

@ -0,0 +1 @@
pf10=: smoutput bind (!10)

View file

@ -0,0 +1,26 @@
9!:3]1 2 4 5 6
pf10
┌───────────────────────────────────────┐
│┌─┬───────────────────────────────────┐│
││@│┌────────┬────────────────────────┐││
││ ││smoutput│┌─┬────────────────────┐│││
││ ││ ││"│┌────────────┬─────┐││││
││ ││ ││ ││┌─┬────────┐│┌─┬─┐│││││
││ ││ ││ │││0│3.6288e6│││0│_││││││
││ ││ ││ ││└─┴────────┘│└─┴─┘│││││
││ ││ ││ │└────────────┴─────┘││││
││ ││ │└─┴────────────────────┘│││
││ │└────────┴────────────────────────┘││
│└─┴───────────────────────────────────┘│
└───────────────────────────────────────┘
┌────────┬─┬──────────────┐
│smoutput│@│┌────────┬─┬─┐│
│ │ ││3.6288e6│"│_││
│ │ │└────────┴─┴─┘│
└────────┴─┴──────────────┘
┌─ smoutput
── @ ─┤ ┌─ 3628800
└─ " ──────┴─ _
smoutput@(3628800"_)
smoutput@(3628800"_)

View file

@ -0,0 +1,2 @@
pf10 ''
3628800

View file

@ -0,0 +1,3 @@
macro fact(n)
factorial(n)
end

View file

@ -0,0 +1 @@
foo() = @fact 10

View file

@ -0,0 +1,6 @@
// version 1.0.6
const val TEN_FACTORIAL = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
fun main(args: Array<String>) {
println("10! = $TEN_FACTORIAL")
}

View file

@ -0,0 +1,8 @@
-- create new (movie) script at runtime
m = new(#script)
-- the following line triggers compilation to bytecode
m.scriptText = "on fac10"&RETURN&"return "&(10*9*8*7*6*5*4*3*2)&RETURN&"end"
put fac10()
-- 3628800

View file

@ -0,0 +1,2 @@
local factorial = 10*9*8*7*6*5*4*3*2*1
print(factorial)

View file

@ -0,0 +1,6 @@
define(`factorial',
`ifelse($1, 0, 1, `eval($1 * factorial(eval($1 - 1)))')')dnl
dnl
BEGIN {
print "10! is factorial(10)"
}

View file

@ -0,0 +1,3 @@
BEGIN {
print "10! is 3628800"
}

View file

@ -0,0 +1,3 @@
li t0,10*9*8*7*6*5*4*3*2 ;= 10! = 0x375F00
jal monitor ;display all registers to the screen
nop

View file

@ -0,0 +1 @@
f = Compile[{}, 10!]

View file

@ -0,0 +1,7 @@
proc fact(x: int): int =
result = 1
for i in 2..x:
result = result * i
const fact10 = fact(10)
echo(fact10)

View file

@ -0,0 +1,3 @@
...
STRING_LITERAL(TMP122, "3628800", 7);
...

View file

@ -0,0 +1,4 @@
let days_to_seconds n =
let conv = 24 * 60 * 60 in
(n * conv)
;;

View file

@ -0,0 +1,5 @@
let conv = 24 * 60 * 60
let days_to_seconds n =
(n * conv)
;;

View file

@ -0,0 +1,9 @@
let fact10 =
let rec factorial n =
if n = 1 then n else n * factorial (n-1)
in
(factorial[@unrolled 10]) 10
(* The unrolled annotation is what allows flambda to keep reducing the call
* Beware that the number of unrollings must be greater than or equal to the
* number of iterations (recursive calls) for this to compile down to a constant. *)

View file

@ -0,0 +1,8 @@
MODULE CompileTime;
IMPORT
Out;
CONST
tenfac = 10*9*8*7*6*5*4*3*2;
BEGIN
Out.String("10! =");Out.LongInt(tenfac,0);Out.Ln
END CompileTime.

View file

@ -0,0 +1,7 @@
bundle Default {
class CompileTime {
function : Main(args : String[]) ~ Nil {
(10*9*8*7*6*5*4*3*2*1)->PrintLine();
}
}
}

View file

@ -0,0 +1,2 @@
10 seq reduce(#*) Constant new: FACT10
: newFunction FACT10 . ;

View file

@ -0,0 +1,4 @@
20 seq map(#[ seq reduce(#*) ]) Constant new: ALLFACTS
: fact(n) n ifZero: [ 1 ] else: [ ALLFACTS at(n) ] ;
ALLFACTS println

View file

@ -0,0 +1,52 @@
'LIBRARY CALLS
'=============
extern lib "../../oxygen.dll"
declare o2_basic (string src)
declare o2_exec (optional sys p) as sys
declare o2_errno () as sys
declare o2_error () as string
extern lib "kernel32.dll"
declare QueryPerformanceFrequency(quad*freq)
declare QueryPerformanceCounter(quad*count)
end extern
'EMBEDDED SOURCE CODE
'====================
src=quote
===Source===
def Pling10 2*3*4*5*6*7*8*9*10
byte a[pling10] 'Pling10 is resolved to a number here at compile time
print pling10
===Source===
'TIMER
'=====
quad ts,tc,freq
QueryPerformanceFrequency freq
QueryPerformanceCounter ts
'COMPILE/EXECUTE
'===============
o2_basic src
if o2_errno then
print o2_error
else
QueryPerformanceCounter tc
print "Compile time: " str((tc-ts)*1000/freq, 1) " MilliSeconds"
o2_exec 'Run the program
end if

View file

@ -0,0 +1,12 @@
functor
import
System Application
prepare
fun {Fac N}
{FoldL {List.number 1 N 1} Number.'*' 1}
end
Fac10 = {Fac 10}
define
{System.showInfo "10! = "#Fac10}
{Application.exit 0}
end

View file

@ -0,0 +1,26 @@
/* Factorials using the pre-processor. */
test: procedure options (main);
%factorial: procedure (N) returns (fixed);
declare N fixed;
declare (i, k) fixed;
k = 1;
do i = 2 to N;
k = k*i;
end;
return (k);
%end factorial;
%activate factorial;
declare (x, y) fixed decimal;
x = factorial (4);
put ('factorial 4 is ', x);
y = factorial (6);
put skip list ('factorial 6 is ', y);
end test;

View file

@ -0,0 +1,8 @@
/* Factorials using the pre-processor. */
test: procedure options (main);
declare (x, y) fixed decimal;
x = 24;
put ('factorial 4 is ', x);
y = 720;
put skip list ('factorial 6 is ', y);
end test;

View file

@ -0,0 +1,2 @@
factorial 4 is 24
factorial 6 is 720

View file

@ -0,0 +1,11 @@
program in out;
const
X = 10*9*8*7*6*5*4*3*2*1 ;
begin
writeln(x);
end;

View file

@ -0,0 +1,6 @@
my $tenfactorial;
print "$tenfactorial\n";
BEGIN
{$tenfactorial = 1;
$tenfactorial *= $_ foreach 1 .. 10;}

View file

@ -0,0 +1 @@
my $tenfactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2;

View file

@ -0,0 +1,6 @@
(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span>
<span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">*</span><span style="color: #000000;">9</span><span style="color: #0000FF;">*</span><span style="color: #000000;">8</span><span style="color: #0000FF;">*</span><span style="color: #000000;">7</span><span style="color: #0000FF;">*</span><span style="color: #000000;">6</span><span style="color: #0000FF;">*</span><span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1</span>
<span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?{</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">}</span>
<!--

View file

@ -0,0 +1,5 @@
(de fact (N)
(apply * (range 1 N)) )
(de foo ()
(prinl "The value of fact(10) is " `(fact 10)) )

View file

@ -0,0 +1,14 @@
function fact([BigInt]$n){
if($n -ge ([BigInt]::Zero)) {
$fact = [BigInt]::One
([BigInt]::One)..$n | foreach{
$fact = [BigInt]::Multiply($fact, $_)
}
$fact
} else {
Write-Error "$n is lower than 0"
}
}
"$((Measure-Command {$fact = fact 10}).TotalSeconds) Seconds"
$fact

View file

@ -0,0 +1,9 @@
% Taken from RosettaCode Factorial page for Prolog
fact(X, 1) :- X<2.
fact(X, F) :- Y is X-1, fact(Y,Z), F is Z*X.
goal_expansion((X = factorial_of(N)), (X = F)) :- fact(N,F).
test :-
F = factorial_of(10),
format('!10 = ~p~n', F).

View file

@ -0,0 +1 @@
a=1*2*3*4*5*6*7*8*9*10

View file

@ -0,0 +1 @@
[ 1 10 times [ i 1+ * ] echo ] now!

View file

@ -0,0 +1 @@
[ 1 10 times [ i 1+ * ] ] constant echo

View file

@ -0,0 +1,6 @@
/*REXX program computes 10! (ten factorial) during REXX's equivalent of "compile─time". */
say '10! =' !(10)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; !=1; do j=2 to arg(1); !=!*j; end /*j*/; return !

View file

@ -0,0 +1,21 @@
#lang racket
;; Import the math library for compile-time
;; Note: included in Racket v5.3.2
(require (for-syntax math))
;; In versions older than v5.3.2, just define the function
;; for compile-time
;;
;; (begin-for-syntax
;; (define (factorial n)
;; (if (zero? n)
;; 1
;; (factorial (- n 1)))))
;; define a macro that calls factorial at compile-time
(define-syntax (fact10 stx)
#`#,(factorial 10))
;; use the macro defined above
(fact10)

View file

@ -0,0 +1,2 @@
constant $tenfact = [*] 2..10;
say $tenfact;

View file

@ -0,0 +1 @@
say(BEGIN [*] 2..10);

View file

@ -0,0 +1,6 @@
a = 10*9*8*7*6*5*4*3*2*1
b = factorial(10)
see a + nl
see b + nl
func factorial nr if nr = 1 return 1 else return nr * factorial(nr-1) ok

View file

@ -0,0 +1,2 @@
factorial = 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10
print "10! = "; factorial ' 3628800

View file

@ -0,0 +1,11 @@
fn factorial(n: i64) -> i64 {
let mut total = 1;
for i in 1..n+1 {
total *= i;
}
return total;
}
fn main() {
println!("Factorial of 10 is {}.", factorial(10));
}

View file

@ -0,0 +1,7 @@
object Main extends {
val tenFactorial = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
def tenFac = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2
println(s"10! = $tenFactorial", tenFac)
}

View file

@ -0,0 +1,8 @@
$ include "seed7_05.s7i";
const proc: main is func
local
const integer: factorial is !10;
begin
writeln(factorial);
end func;

View file

@ -0,0 +1,2 @@
define n = (10!);
say n;

View file

@ -0,0 +1,2 @@
define n = (func(n){ n > 0 ? __FUNC__(n-1)*n : 1 }(10));
say n;

View file

@ -0,0 +1,3 @@
(defun buildinfo ()
(macro-time
`Built by @{(uname).nodename} on @(time-string-local (time) "%c")`))

View file

@ -0,0 +1,8 @@
proc makeFacExpr n {
set exp 1
for {set i 2} {$i <= $n} {incr i} {
append exp " * $i"
}
return "expr \{$exp\}"
}
eval [makeFacExpr 10]

View file

@ -0,0 +1,9 @@
% tcl::unsupported::disassemble script [makeFacExpr 10]
ByteCode 0x0x4de10, refCt 1, epoch 3, interp 0x0x31c10 (epoch 3)
Source "expr {1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10}"
Cmds 1, src 45, inst 3, litObjs 1, aux 0, stkDepth 1, code/src 0.00
Commands 1:
1: pc 0-1, src 0-44
Command 1: "expr {1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10}"
(0) push1 0 # "3628800"
(2) done

View file

@ -0,0 +1,7 @@
#import nat
x = factorial 10
#executable&
comcal = ! (%nP x)--<''>

Some files were not shown because too many files have changed in this diff Show more