Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
9
Task/Factorial/00-META.yaml
Normal file
9
Task/Factorial/00-META.yaml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
category:
|
||||
- Recursion
|
||||
- Memoization
|
||||
- Classic CS problems and programs
|
||||
- Arithmetic
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Factorial
|
||||
note: Arithmetic operations
|
||||
18
Task/Factorial/00-TASK.txt
Normal file
18
Task/Factorial/00-TASK.txt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
;Definitions:
|
||||
:* The factorial of '''0''' (zero) is [[wp:Factorial#Definition|defined]] as being 1 (unity).
|
||||
:* The '''Factorial Function''' of a positive integer, <big> ''n'', </big> is defined as the product of the sequence:
|
||||
<big><big> ''n'', ''n''-1, ''n''-2, ... 1 </big></big>
|
||||
|
||||
|
||||
;Task:
|
||||
Write a function to return the factorial of a number.
|
||||
|
||||
Solutions can be iterative or recursive.
|
||||
|
||||
Support for trapping negative <big> ''n'' </big> errors is optional.
|
||||
|
||||
|
||||
;Related task:
|
||||
* [[Primorial numbers]]
|
||||
<br><br>
|
||||
|
||||
11
Task/Factorial/0815/factorial.0815
Normal file
11
Task/Factorial/0815/factorial.0815
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
}:r: Start reader loop.
|
||||
|~ Read n,
|
||||
#:end: if n is 0 terminates
|
||||
>= enqueue it as the initial product, reposition.
|
||||
}:f: Start factorial loop.
|
||||
x<:1:x- Decrement n.
|
||||
{=*> Dequeue product, position n, multiply, update product.
|
||||
^:f:
|
||||
{+% Dequeue incidental 0, add to get Y into Z, output fac(n).
|
||||
<:a:~$ Output a newline.
|
||||
^:r:
|
||||
8
Task/Factorial/11l/factorial.11l
Normal file
8
Task/Factorial/11l/factorial.11l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
F factorial(n)
|
||||
V result = 1
|
||||
L(i) 2..n
|
||||
result *= i
|
||||
R result
|
||||
|
||||
L(n) 0..5
|
||||
print(n‘ ’factorial(n))
|
||||
64
Task/Factorial/360-Assembly/factorial.360
Normal file
64
Task/Factorial/360-Assembly/factorial.360
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
FACTO CSECT
|
||||
USING FACTO,R13
|
||||
SAVEAREA B STM-SAVEAREA(R15)
|
||||
DC 17F'0'
|
||||
DC CL8'FACTO'
|
||||
STM STM R14,R12,12(R13)
|
||||
ST R13,4(R15)
|
||||
ST R15,8(R13)
|
||||
LR R13,R15 base register and savearea pointer
|
||||
ZAP N,=P'1' n=1
|
||||
LOOPN CP N,NN if n>nn
|
||||
BH ENDLOOPN then goto endloop
|
||||
LA R1,PARMLIST
|
||||
L R15,=A(FACT)
|
||||
BALR R14,R15 call fact(n)
|
||||
ZAP F,0(L'R,R1) f=fact(n)
|
||||
DUMP EQU *
|
||||
MVC S,MASK
|
||||
ED S,N
|
||||
MVC WTOBUF+5(2),S+30
|
||||
MVC S,MASK
|
||||
ED S,F
|
||||
MVC WTOBUF+9(32),S
|
||||
WTO MF=(E,WTOMSG)
|
||||
AP N,=P'1' n=n+1
|
||||
B LOOPN
|
||||
ENDLOOPN EQU *
|
||||
RETURN EQU *
|
||||
L R13,4(0,R13)
|
||||
LM R14,R12,12(R13)
|
||||
XR R15,R15
|
||||
BR R14
|
||||
FACT EQU * function FACT(l)
|
||||
L R2,0(R1)
|
||||
L R3,12(R2)
|
||||
ZAP L,0(L'N,R2) l=n
|
||||
ZAP R,=P'1' r=1
|
||||
ZAP I,=P'2' i=2
|
||||
LOOP CP I,L if i>l
|
||||
BH ENDLOOP then goto endloop
|
||||
MP R,I r=r*i
|
||||
AP I,=P'1' i=i+1
|
||||
B LOOP
|
||||
ENDLOOP EQU *
|
||||
LA R1,R return r
|
||||
BR R14 end function FACT
|
||||
DS 0D
|
||||
NN DC PL16'29'
|
||||
N DS PL16
|
||||
F DS PL16
|
||||
C DS CL16
|
||||
II DS PL16
|
||||
PARMLIST DC A(N)
|
||||
S DS CL33
|
||||
MASK DC X'40',29X'20',X'212060' CL33
|
||||
WTOMSG DS 0F
|
||||
DC H'80',XL2'0000'
|
||||
WTOBUF DC CL80'FACT(..)=................................ '
|
||||
L DS PL16
|
||||
R DS PL16
|
||||
I DS PL16
|
||||
LTORG
|
||||
YREGS
|
||||
END FACTO
|
||||
71
Task/Factorial/68000-Assembly/factorial.68000
Normal file
71
Task/Factorial/68000-Assembly/factorial.68000
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
Factorial:
|
||||
;input: D0.W: number you wish to get the factorial of.
|
||||
;output: D0.L
|
||||
CMP.W #0,D0
|
||||
BEQ .isZero
|
||||
CMP.W #1,D0
|
||||
BEQ .isOne
|
||||
MOVEM.L D4-D5,-(SP)
|
||||
MOVE.W D0,D4
|
||||
MOVE.W D0,D5
|
||||
SUBQ.W #2,D5 ;D2 = LOOP COUNTER.
|
||||
;Since DBRA stops at FFFF we can't use it as our multiplier.
|
||||
;If we did, we'd always return 0!
|
||||
.loop:
|
||||
SUBQ.L #1,D4
|
||||
MOVE.L D1,-(SP)
|
||||
MOVE.L D4,D1
|
||||
JSR MULU_48 ;multiplies D0.L by D1.W
|
||||
EXG D0,D1 ;output is in D1 so we need to put it in D0
|
||||
MOVE.L (SP)+,D1
|
||||
DBRA D5,.loop
|
||||
MOVEM.L (SP)+,D4-D5
|
||||
RTS
|
||||
.isZero:
|
||||
.isOne:
|
||||
MOVEQ #1,D0
|
||||
RTS
|
||||
MULU_48:
|
||||
;"48-BIT" MULTIPLICATION.
|
||||
;OUTPUTS HIGH LONG IN D0, LOW LONG IN D1
|
||||
;INPUT: D0.L, D1.W = FACTORS
|
||||
MOVEM.L D2-D7,-(SP)
|
||||
SWAP D1
|
||||
CLR.W D1
|
||||
SWAP D1 ;CLEAR THE TOP WORD OF D1.
|
||||
|
||||
MOVE.L D1,D2
|
||||
EXG D0,D1 ;D1 IS OUR BASE VALUE, WE'LL USE BIT SHIFTS TO REPEATEDLY MULTIPLY.
|
||||
MOVEQ #0,D0 ;CLEAR UPPER LONG OF PRODUCT
|
||||
MOVE.L D1,D3 ;BACKUP OF "D1" (WHICH USED TO BE D0)
|
||||
|
||||
;EXAMPLE: $40000000*$225 = ($40000000 << 9) + ($40000000 << 5) + ($40000000 << 2) + $40000000
|
||||
;FACTOR OUT AS MANY POWERS OF 2 AS POSSIBLE.
|
||||
|
||||
MOVEQ #0,D0
|
||||
LSR.L #1,D2
|
||||
BCS .wasOdd ;if odd, leave D1 alone. Otherwise, clear it. This is our +1 for an odd second operand.
|
||||
MOVEQ #0,D1
|
||||
.wasOdd:
|
||||
MOVEQ #31-1,D6 ;30 BITS TO CHECK
|
||||
MOVEQ #1-1,D7 ;START AT BIT 1, MINUS 1 IS FOR DBRA CORRECTION FACTOR
|
||||
.shiftloop:
|
||||
LSR.L #1,D2
|
||||
BCC .noShift
|
||||
MOVE.W D7,-(SP)
|
||||
MOVEQ #0,D4
|
||||
MOVE.L D3,D5
|
||||
.innershiftloop:
|
||||
ANDI #%00001111,CCR ;clear extend flag
|
||||
ROXL.L D5
|
||||
ROXL.L D4
|
||||
DBRA D7,.innershiftloop
|
||||
ANDI #%00001111,CCR
|
||||
ADDX.L D5,D1
|
||||
ADDX.L D4,D0
|
||||
MOVE.W (SP)+,D7
|
||||
.noShift:
|
||||
addq.l #1,d7
|
||||
dbra d6,.shiftloop
|
||||
MOVEM.L (SP)+,D2-D7
|
||||
RTS
|
||||
110
Task/Factorial/AArch64-Assembly/factorial.aarch64
Normal file
110
Task/Factorial/AArch64-Assembly/factorial.aarch64
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program factorial64.s */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessLargeNumber: .asciz "Number N to large. \n"
|
||||
szMessNegNumber: .asciz "Number N is negative. \n"
|
||||
|
||||
szMessResult: .asciz "Resultat = @ \n" // message result
|
||||
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
sZoneConv: .skip 24
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
|
||||
mov x0,#-5
|
||||
bl factorial
|
||||
mov x0,#10
|
||||
bl factorial
|
||||
mov x0,#20
|
||||
bl factorial
|
||||
mov x0,#30
|
||||
bl factorial
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
|
||||
/********************************************/
|
||||
/* calculation */
|
||||
/********************************************/
|
||||
/* x0 contains number N */
|
||||
factorial:
|
||||
stp x1,lr,[sp,-16]! // save registers
|
||||
cmp x0,#0
|
||||
blt 99f
|
||||
beq 100f
|
||||
cmp x0,#1
|
||||
beq 100f
|
||||
bl calFactorial
|
||||
cmp x0,#-1 // overflow ?
|
||||
beq 98f
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl conversion10
|
||||
ldr x0,qAdrszMessResult
|
||||
ldr x1,qAdrsZoneConv
|
||||
bl strInsertAtCharInc // insert result at @ character
|
||||
bl affichageMess // display message
|
||||
b 100f
|
||||
|
||||
98: // display error message
|
||||
ldr x0,qAdrszMessLargeNumber
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99: // display error message
|
||||
ldr x0,qAdrszMessNegNumber
|
||||
bl affichageMess
|
||||
|
||||
100:
|
||||
ldp x1,lr,[sp],16 // restaur 2 registers
|
||||
ret // return to address lr x30
|
||||
|
||||
qAdrszMessNegNumber: .quad szMessNegNumber
|
||||
qAdrszMessLargeNumber: .quad szMessLargeNumber
|
||||
qAdrsZoneConv: .quad sZoneConv
|
||||
qAdrszMessResult: .quad szMessResult
|
||||
/******************************************************************/
|
||||
/* calculation */
|
||||
/******************************************************************/
|
||||
/* x0 contains the number N */
|
||||
calFactorial:
|
||||
cmp x0,1 // N = 1 ?
|
||||
beq 100f // yes -> return
|
||||
stp x20,lr,[sp,-16]! // save registers
|
||||
mov x20,x0 // save N in x20
|
||||
sub x0,x0,1 // call function with N - 1
|
||||
bl calFactorial
|
||||
cmp x0,-1 // error overflow ?
|
||||
beq 99f // yes -> return
|
||||
mul x10,x20,x0 // multiply result by N
|
||||
umulh x11,x20,x0 // x11 is the hi rd if <> 0 overflow
|
||||
cmp x11,0
|
||||
mov x11,-1 // if overflow -1 -> x0
|
||||
csel x0,x10,x11,eq // else x0 = x10
|
||||
|
||||
99:
|
||||
ldp x20,lr,[sp],16 // restaur 2 registers
|
||||
100:
|
||||
ret // return to address lr x30
|
||||
/********************************************************/
|
||||
/* File Include fonctions */
|
||||
/********************************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly */
|
||||
.include "../includeARM64.inc"
|
||||
8
Task/Factorial/ABAP/factorial-1.abap
Normal file
8
Task/Factorial/ABAP/factorial-1.abap
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
form factorial using iv_val type i.
|
||||
data: lv_res type i value 1.
|
||||
do iv_val times.
|
||||
multiply lv_res by sy-index.
|
||||
enddo.
|
||||
|
||||
iv_val = lv_res.
|
||||
endform.
|
||||
11
Task/Factorial/ABAP/factorial-2.abap
Normal file
11
Task/Factorial/ABAP/factorial-2.abap
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
form fac_rec using iv_val type i.
|
||||
data: lv_temp type i.
|
||||
|
||||
if iv_val = 0.
|
||||
iv_val = 1.
|
||||
else.
|
||||
lv_temp = iv_val - 1.
|
||||
perform fac_rec using lv_temp.
|
||||
multiply iv_val by lv_temp.
|
||||
endif.
|
||||
endform.
|
||||
14
Task/Factorial/ALGOL-60/factorial.alg
Normal file
14
Task/Factorial/ALGOL-60/factorial.alg
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
begin
|
||||
comment factorial - algol 60;
|
||||
integer procedure factorial(n); integer n;
|
||||
begin
|
||||
integer i,fact;
|
||||
fact:=1;
|
||||
for i:=2 step 1 until n do
|
||||
fact:=fact*i;
|
||||
factorial:=fact
|
||||
end;
|
||||
integer i;
|
||||
for i:=1 step 1 until 10 do outinteger(1,factorial(i));
|
||||
outstring(1,"\n")
|
||||
end
|
||||
5
Task/Factorial/ALGOL-68/factorial-1.alg
Normal file
5
Task/Factorial/ALGOL-68/factorial-1.alg
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
PROC factorial = (INT upb n)LONG LONG INT:(
|
||||
LONG LONG INT z := 1;
|
||||
FOR n TO upb n DO z *:= n OD;
|
||||
z
|
||||
); ~
|
||||
30
Task/Factorial/ALGOL-68/factorial-2.alg
Normal file
30
Task/Factorial/ALGOL-68/factorial-2.alg
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
INT g = 7;
|
||||
[]REAL p = []REAL(0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7)[@0];
|
||||
|
||||
PROC complex gamma = (COMPL in z)COMPL: (
|
||||
# Reflection formula #
|
||||
COMPL z := in z;
|
||||
IF re OF z < 0.5 THEN
|
||||
pi / (complex sin(pi*z)*complex gamma(1-z))
|
||||
ELSE
|
||||
z -:= 1;
|
||||
COMPL x := p[0];
|
||||
FOR i TO g+1 DO x +:= p[i]/(z+i) OD;
|
||||
COMPL t := z + g + 0.5;
|
||||
complex sqrt(2*pi) * t**(z+0.5) * complex exp(-t) * x
|
||||
FI
|
||||
);
|
||||
|
||||
OP ** = (COMPL z, p)COMPL: ( z=0|0|complex exp(complex ln(z)*p) );
|
||||
PROC factorial = (COMPL n)COMPL: complex gamma(n+1);
|
||||
|
||||
FORMAT compl fmt = $g(-16, 8)"⊥"g(-10, 8)$;
|
||||
|
||||
test:(
|
||||
printf(($q"factorial(-0.5)**2="f(compl fmt)l$, factorial(-0.5)**2));
|
||||
FOR i TO 9 DO
|
||||
printf(($q"factorial("d")="f(compl fmt)l$, i, factorial(i)))
|
||||
OD
|
||||
)
|
||||
7
Task/Factorial/ALGOL-68/factorial-3.alg
Normal file
7
Task/Factorial/ALGOL-68/factorial-3.alg
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
PROC factorial = (INT n)LONG LONG INT:
|
||||
CASE n+1 IN
|
||||
1,1,2,6,24,120,720 # a brief lookup #
|
||||
OUT
|
||||
n*factorial(n-1)
|
||||
ESAC
|
||||
; ~
|
||||
8
Task/Factorial/ALGOL-M/factorial.alg
Normal file
8
Task/Factorial/ALGOL-M/factorial.alg
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
INTEGER FUNCTION FACTORIAL( N ); INTEGER N;
|
||||
BEGIN
|
||||
INTEGER I, FACT;
|
||||
FACT := 1;
|
||||
FOR I := 2 STEP 1 UNTIL N DO
|
||||
FACT := FACT * I;
|
||||
FACTORIAL := FACT;
|
||||
END;
|
||||
15
Task/Factorial/ALGOL-W/factorial.alg
Normal file
15
Task/Factorial/ALGOL-W/factorial.alg
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
begin
|
||||
% computes factorial n iteratively %
|
||||
integer procedure factorial( integer value n ) ;
|
||||
if n < 2
|
||||
then 1
|
||||
else begin
|
||||
integer f;
|
||||
f := 2;
|
||||
for i := 3 until n do f := f * i;
|
||||
f
|
||||
end factorial ;
|
||||
|
||||
for t := 0 until 10 do write( "factorial: ", t, factorial( t ) );
|
||||
|
||||
end.
|
||||
1
Task/Factorial/ANT/factorial.ant
Normal file
1
Task/Factorial/ANT/factorial.ant
Normal file
|
|
@ -0,0 +1 @@
|
|||
factorial:{1 */ 1+range[x]} /Call: factorial[1000]
|
||||
2
Task/Factorial/APL/factorial-1.apl
Normal file
2
Task/Factorial/APL/factorial-1.apl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
!6
|
||||
720
|
||||
1
Task/Factorial/APL/factorial-2.apl
Normal file
1
Task/Factorial/APL/factorial-2.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
FACTORIAL←{×/⍳⍵} ⍝ OR: FACTORIAL←×/⍳
|
||||
2
Task/Factorial/APL/factorial-3.apl
Normal file
2
Task/Factorial/APL/factorial-3.apl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
FACTORIAL 6
|
||||
720
|
||||
176
Task/Factorial/ARM-Assembly/factorial.arm
Normal file
176
Task/Factorial/ARM-Assembly/factorial.arm
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program factorial.s */
|
||||
|
||||
/* Constantes */
|
||||
.equ STDOUT, 1 @ Linux output console
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
.equ WRITE, 4 @ Linux syscall
|
||||
|
||||
/*********************************/
|
||||
/* Initialized data */
|
||||
/*********************************/
|
||||
.data
|
||||
szMessLargeNumber: .asciz "Number N to large. \n"
|
||||
szMessNegNumber: .asciz "Number N is negative. \n"
|
||||
|
||||
szMessResult: .ascii "Resultat = " @ message result
|
||||
sMessValeur: .fill 12, 1, ' '
|
||||
.asciz "\n"
|
||||
/*********************************/
|
||||
/* UnInitialized data */
|
||||
/*********************************/
|
||||
.bss
|
||||
/*********************************/
|
||||
/* code section */
|
||||
/*********************************/
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
push {fp,lr} @ saves 2 registers
|
||||
|
||||
mov r0,#-5
|
||||
bl factorial
|
||||
mov r0,#10
|
||||
bl factorial
|
||||
mov r0,#20
|
||||
bl factorial
|
||||
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur 2 registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
|
||||
|
||||
/********************************************/
|
||||
/* calculation */
|
||||
/********************************************/
|
||||
/* r0 contains number N */
|
||||
factorial:
|
||||
push {r1,r2,lr} @ save registres
|
||||
cmp r0,#0
|
||||
blt 99f
|
||||
beq 100f
|
||||
cmp r0,#1
|
||||
beq 100f
|
||||
bl calFactorial
|
||||
cmp r0,#-1 @ overflow ?
|
||||
beq 98f
|
||||
ldr r1,iAdrsMessValeur
|
||||
bl conversion10 @ call function with 2 parameter (r0,r1)
|
||||
ldr r0,iAdrszMessResult
|
||||
bl affichageMess @ display message
|
||||
b 100f
|
||||
|
||||
98: @ display error message
|
||||
ldr r0,iAdrszMessLargeNumber
|
||||
bl affichageMess
|
||||
b 100f
|
||||
99: @ display error message
|
||||
ldr r0,iAdrszMessNegNumber
|
||||
bl affichageMess
|
||||
|
||||
100:
|
||||
pop {r1,r2,lr} @ restaur registers
|
||||
bx lr @ return
|
||||
iAdrszMessNegNumber: .int szMessNegNumber
|
||||
iAdrszMessLargeNumber: .int szMessLargeNumber
|
||||
iAdrsMessValeur: .int sMessValeur
|
||||
iAdrszMessResult: .int szMessResult
|
||||
/******************************************************************/
|
||||
/* calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the number N */
|
||||
calFactorial:
|
||||
cmp r0,#1 @ N = 1 ?
|
||||
bxeq lr @ yes -> return
|
||||
push {fp,lr} @ save registers
|
||||
sub sp,#4 @ 4 byte on the stack
|
||||
mov fp,sp @ fp <- start address stack
|
||||
str r0,[fp] @ fp contains N
|
||||
sub r0,#1 @ call function with N - 1
|
||||
bl calFactorial
|
||||
cmp r0,#-1 @ error overflow ?
|
||||
beq 100f @ yes -> return
|
||||
ldr r1,[fp] @ load N
|
||||
umull r0,r2,r1,r0 @ multiply result by N
|
||||
cmp r2,#0 @ r2 is the hi rd if <> 0 overflow
|
||||
movne r0,#-1 @ if overflow -1 -> r0
|
||||
|
||||
100:
|
||||
add sp,#4 @ free 4 bytes on stack
|
||||
pop {fp,lr} @ restau2 registers
|
||||
bx lr @ return
|
||||
|
||||
/******************************************************************/
|
||||
/* display text with size calculation */
|
||||
/******************************************************************/
|
||||
/* r0 contains the address of the message */
|
||||
affichageMess:
|
||||
push {fp,lr} /* save registres */
|
||||
push {r0,r1,r2,r7} /* save others registers */
|
||||
mov r2,#0 /* counter length */
|
||||
1: /* loop length calculation */
|
||||
ldrb r1,[r0,r2] /* read octet start position + index */
|
||||
cmp r1,#0 /* if 0 its over */
|
||||
addne r2,r2,#1 /* else add 1 in the length */
|
||||
bne 1b /* and loop */
|
||||
/* so here r2 contains the length of the message */
|
||||
mov r1,r0 /* address message in r1 */
|
||||
mov r0,#STDOUT /* code to write to the standard output Linux */
|
||||
mov r7, #WRITE /* code call system "write" */
|
||||
swi #0 /* call systeme */
|
||||
pop {r0,r1,r2,r7} /* restaur others registers */
|
||||
pop {fp,lr} /* restaur des 2 registres */
|
||||
bx lr /* return */
|
||||
/******************************************************************/
|
||||
/* Converting a register to a decimal */
|
||||
/******************************************************************/
|
||||
/* r0 contains value and r1 address area */
|
||||
conversion10:
|
||||
push {r1-r4,lr} /* save registers */
|
||||
mov r3,r1
|
||||
mov r2,#10
|
||||
|
||||
1: @ start loop
|
||||
bl divisionpar10 @ r0 <- dividende. quotient ->r0 reste -> r1
|
||||
add r1,#48 @ digit
|
||||
strb r1,[r3,r2] @ store digit on area
|
||||
sub r2,#1 @ previous position
|
||||
cmp r0,#0 @ stop if quotient = 0 */
|
||||
bne 1b @ else loop
|
||||
@ and move spaves in first on area
|
||||
mov r1,#' ' @ space
|
||||
2:
|
||||
strb r1,[r3,r2] @ store space in area
|
||||
subs r2,#1 @ @ previous position
|
||||
bge 2b @ loop if r2 >= zéro
|
||||
|
||||
100:
|
||||
pop {r1-r4,lr} @ restaur registres
|
||||
bx lr @return
|
||||
/***************************************************/
|
||||
/* division par 10 signé */
|
||||
/* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/*
|
||||
/* and http://www.hackersdelight.org/ */
|
||||
/***************************************************/
|
||||
/* r0 dividende */
|
||||
/* r0 quotient */
|
||||
/* r1 remainder */
|
||||
divisionpar10:
|
||||
/* r0 contains the argument to be divided by 10 */
|
||||
push {r2-r4} /* save registers */
|
||||
mov r4,r0
|
||||
ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */
|
||||
smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */
|
||||
mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */
|
||||
mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */
|
||||
add r0, r2, r1 /* r0 <- r2 + r1 */
|
||||
add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */
|
||||
sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */
|
||||
pop {r2-r4}
|
||||
bx lr /* leave function */
|
||||
.align 4
|
||||
.Ls_magic_number_10: .word 0x66666667
|
||||
|
||||
10
Task/Factorial/ATS/factorial-1.ats
Normal file
10
Task/Factorial/ATS/factorial-1.ats
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fun
|
||||
fact
|
||||
(
|
||||
n: int
|
||||
) : int = res where
|
||||
{
|
||||
var n: int = n
|
||||
var res: int = 1
|
||||
val () = while (n > 0) (res := res * n; n := n - 1)
|
||||
}
|
||||
5
Task/Factorial/ATS/factorial-2.ats
Normal file
5
Task/Factorial/ATS/factorial-2.ats
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
fun
|
||||
factorial
|
||||
(n:int): int =
|
||||
if n > 0 then n * factorial(n-1) else 1
|
||||
// end of [factorial]
|
||||
8
Task/Factorial/ATS/factorial-3.ats
Normal file
8
Task/Factorial/ATS/factorial-3.ats
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fun
|
||||
factorial
|
||||
(n:int): int = let
|
||||
fun loop(n: int, res: int): int =
|
||||
if n > 0 then loop(n-1, n*res) else res
|
||||
in
|
||||
loop(n, 1)
|
||||
end // end of [factorial]
|
||||
5
Task/Factorial/AWK/factorial-1.awk
Normal file
5
Task/Factorial/AWK/factorial-1.awk
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
function fact_r(n)
|
||||
{
|
||||
if ( n <= 1 ) return 1;
|
||||
return n*fact_r(n-1);
|
||||
}
|
||||
9
Task/Factorial/AWK/factorial-2.awk
Normal file
9
Task/Factorial/AWK/factorial-2.awk
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function fact(n)
|
||||
{
|
||||
if ( n < 1 ) return 1;
|
||||
r = 1
|
||||
for(m = 2; m <= n; m++) {
|
||||
r *= m;
|
||||
}
|
||||
return r
|
||||
}
|
||||
35
Task/Factorial/Action-/factorial.action
Normal file
35
Task/Factorial/Action-/factorial.action
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
CARD FUNC Factorial(INT n BYTE POINTER err)
|
||||
CARD i,res
|
||||
|
||||
IF n<0 THEN
|
||||
err^=1 RETURN (0)
|
||||
ELSEIF n>8 THEN
|
||||
err^=2 RETURN (0)
|
||||
FI
|
||||
|
||||
res=1
|
||||
FOR i=2 TO n
|
||||
DO
|
||||
res=res*i
|
||||
OD
|
||||
|
||||
err^=0
|
||||
RETURN (res)
|
||||
|
||||
PROC Main()
|
||||
INT i,f
|
||||
BYTE err
|
||||
|
||||
FOR i=-2 TO 10
|
||||
DO
|
||||
f=Factorial(i,@err)
|
||||
|
||||
IF err=0 THEN
|
||||
PrintF("%I!=%U%E",i,f)
|
||||
ELSEIF err=1 THEN
|
||||
PrintF("%I is negative value%E",i)
|
||||
ELSE
|
||||
PrintF("%I! is to big%E",i)
|
||||
FI
|
||||
OD
|
||||
RETURN
|
||||
11
Task/Factorial/ActionScript/factorial-1.as
Normal file
11
Task/Factorial/ActionScript/factorial-1.as
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
public static function factorial(n:int):int
|
||||
{
|
||||
if (n < 0)
|
||||
return 0;
|
||||
|
||||
var fact:int = 1;
|
||||
for (var i:int = 1; i <= n; i++)
|
||||
fact *= i;
|
||||
|
||||
return fact;
|
||||
}
|
||||
10
Task/Factorial/ActionScript/factorial-2.as
Normal file
10
Task/Factorial/ActionScript/factorial-2.as
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
public static function factorial(n:int):int
|
||||
{
|
||||
if (n < 0)
|
||||
return 0;
|
||||
|
||||
if (n == 0)
|
||||
return 1;
|
||||
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
9
Task/Factorial/Ada/factorial-1.ada
Normal file
9
Task/Factorial/Ada/factorial-1.ada
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function Factorial (N : Positive) return Positive is
|
||||
Result : Positive := N;
|
||||
Counter : Natural := N - 1;
|
||||
begin
|
||||
for I in reverse 1..Counter loop
|
||||
Result := Result * I;
|
||||
end loop;
|
||||
return Result;
|
||||
end Factorial;
|
||||
8
Task/Factorial/Ada/factorial-2.ada
Normal file
8
Task/Factorial/Ada/factorial-2.ada
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
function Factorial(N : Positive) return Positive is
|
||||
Result : Positive := 1;
|
||||
begin
|
||||
if N > 1 then
|
||||
Result := N * Factorial(N - 1);
|
||||
end if;
|
||||
return Result;
|
||||
end Factorial;
|
||||
62
Task/Factorial/Ada/factorial-3.ada
Normal file
62
Task/Factorial/Ada/factorial-3.ada
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
with Ada.Numerics.Generic_Complex_Types;
|
||||
with Ada.Numerics.Generic_Complex_Elementary_Functions;
|
||||
with Ada.Numerics.Generic_Elementary_Functions;
|
||||
with Ada.Text_IO.Complex_Io;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Factorial_Numeric_Approximation is
|
||||
type Real is digits 15;
|
||||
package Complex_Pck is new Ada.Numerics.Generic_Complex_Types(Real);
|
||||
use Complex_Pck;
|
||||
package Complex_Io is new Ada.Text_Io.Complex_Io(Complex_Pck);
|
||||
use Complex_IO;
|
||||
package Cmplx_Elem_Funcs is new Ada.Numerics.Generic_Complex_Elementary_Functions(Complex_Pck);
|
||||
use Cmplx_Elem_Funcs;
|
||||
|
||||
function Gamma(X : Complex) return Complex is
|
||||
package Elem_Funcs is new Ada.Numerics.Generic_Elementary_Functions(Real);
|
||||
use Elem_Funcs;
|
||||
use Ada.Numerics;
|
||||
-- Coefficients used by the GNU Scientific Library
|
||||
G : Natural := 7;
|
||||
P : constant array (Natural range 0..G + 1) of Real := (
|
||||
0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7);
|
||||
Z : Complex := X;
|
||||
Cx : Complex;
|
||||
Ct : Complex;
|
||||
begin
|
||||
if Re(Z) < 0.5 then
|
||||
return Pi / (Sin(Pi * Z) * Gamma(1.0 - Z));
|
||||
else
|
||||
Z := Z - 1.0;
|
||||
Set_Re(Cx, P(0));
|
||||
Set_Im(Cx, 0.0);
|
||||
for I in 1..P'Last loop
|
||||
Cx := Cx + (P(I) / (Z + Real(I)));
|
||||
end loop;
|
||||
Ct := Z + Real(G) + 0.5;
|
||||
return Sqrt(2.0 * Pi) * Ct**(Z + 0.5) * Exp(-Ct) * Cx;
|
||||
end if;
|
||||
end Gamma;
|
||||
|
||||
function Factorial(N : Complex) return Complex is
|
||||
begin
|
||||
return Gamma(N + 1.0);
|
||||
end Factorial;
|
||||
Arg : Complex;
|
||||
begin
|
||||
Put("factorial(-0.5)**2.0 = ");
|
||||
Set_Re(Arg, -0.5);
|
||||
Set_Im(Arg, 0.0);
|
||||
Put(Item => Factorial(Arg) **2.0, Fore => 1, Aft => 8, Exp => 0);
|
||||
New_Line;
|
||||
for I in 0..9 loop
|
||||
Set_Re(Arg, Real(I));
|
||||
Set_Im(Arg, 0.0);
|
||||
Put("factorial(" & Integer'Image(I) & ") = ");
|
||||
Put(Item => Factorial(Arg), Fore => 6, Aft => 8, Exp => 0);
|
||||
New_Line;
|
||||
end loop;
|
||||
end Factorial_Numeric_Approximation;
|
||||
7
Task/Factorial/Agda/factorial.agda
Normal file
7
Task/Factorial/Agda/factorial.agda
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
module Factorial where
|
||||
|
||||
open import Data.Nat using (ℕ ; zero ; suc ; _*_)
|
||||
|
||||
factorial : (n : ℕ) → ℕ
|
||||
factorial zero = 1
|
||||
factorial (suc n) = (suc n) * (factorial n)
|
||||
14
Task/Factorial/Aime/factorial.aime
Normal file
14
Task/Factorial/Aime/factorial.aime
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
integer
|
||||
factorial(integer n)
|
||||
{
|
||||
integer i, result;
|
||||
|
||||
result = 1;
|
||||
i = 1;
|
||||
while (i < n) {
|
||||
i += 1;
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
5
Task/Factorial/AmigaE/factorial-1.amiga
Normal file
5
Task/Factorial/AmigaE/factorial-1.amiga
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
PROC fact(x) IS IF x>=2 THEN x*fact(x-1) ELSE 1
|
||||
|
||||
PROC main()
|
||||
WriteF('5! = \d\n', fact(5))
|
||||
ENDPROC
|
||||
6
Task/Factorial/AmigaE/factorial-2.amiga
Normal file
6
Task/Factorial/AmigaE/factorial-2.amiga
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
PROC fact(x)
|
||||
DEF r, y
|
||||
IF x < 2 THEN RETURN 1
|
||||
r := 1; y := x;
|
||||
FOR x := 2 TO y DO r := r * x
|
||||
ENDPROC r
|
||||
11
Task/Factorial/Apex/factorial-1.apex
Normal file
11
Task/Factorial/Apex/factorial-1.apex
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
public static long fact(final Integer n) {
|
||||
if (n < 0) {
|
||||
System.debug('No negative numbers');
|
||||
return 0;
|
||||
}
|
||||
long ans = 1;
|
||||
for (Integer i = 1; i <= n; i++) {
|
||||
ans *= i;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
7
Task/Factorial/Apex/factorial-2.apex
Normal file
7
Task/Factorial/Apex/factorial-2.apex
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
public static long factRec(final Integer n) {
|
||||
if (n < 0){
|
||||
System.debug('No negative numbers');
|
||||
return 0;
|
||||
}
|
||||
return (n < 2) ? 1 : n * fact(n - 1);
|
||||
}
|
||||
8
Task/Factorial/AppleScript/factorial-1.applescript
Normal file
8
Task/Factorial/AppleScript/factorial-1.applescript
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
on factorial(x)
|
||||
if x < 0 then return 0
|
||||
set R to 1
|
||||
repeat while x > 1
|
||||
set {R, x} to {R * x, x - 1}
|
||||
end repeat
|
||||
return R
|
||||
end factorial
|
||||
8
Task/Factorial/AppleScript/factorial-2.applescript
Normal file
8
Task/Factorial/AppleScript/factorial-2.applescript
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
-- factorial :: Int -> Int
|
||||
on factorial(x)
|
||||
if x > 1 then
|
||||
x * (factorial(x - 1))
|
||||
else
|
||||
1
|
||||
end if
|
||||
end factorial
|
||||
72
Task/Factorial/AppleScript/factorial-3.applescript
Normal file
72
Task/Factorial/AppleScript/factorial-3.applescript
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
------------------------ FACTORIAL -----------------------
|
||||
|
||||
-- factorial :: Int -> Int
|
||||
on factorial(x)
|
||||
|
||||
product(enumFromTo(1, x))
|
||||
|
||||
end factorial
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on run
|
||||
|
||||
factorial(11)
|
||||
|
||||
--> 39916800
|
||||
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS -------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set xs to {}
|
||||
repeat with i from m to n
|
||||
set end of xs to i
|
||||
end repeat
|
||||
xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- product :: [Num] -> Num
|
||||
on product(xs)
|
||||
script multiply
|
||||
on |λ|(a, b)
|
||||
a * b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(multiply, 1, xs)
|
||||
end product
|
||||
7
Task/Factorial/Applesoft-BASIC/factorial-1.basic
Normal file
7
Task/Factorial/Applesoft-BASIC/factorial-1.basic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
100 N = 4 : GOSUB 200"FACTORIAL
|
||||
110 PRINT N
|
||||
120 END
|
||||
|
||||
200 N = INT(N)
|
||||
210 IF N > 1 THEN FOR I = N - 1 TO 2 STEP -1 : N = N * I : NEXT I
|
||||
220 RETURN
|
||||
9
Task/Factorial/Applesoft-BASIC/factorial-2.basic
Normal file
9
Task/Factorial/Applesoft-BASIC/factorial-2.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
10 A = 768:L = 7
|
||||
20 DATA 165,157,240,3
|
||||
30 DATA 32,149,217,96
|
||||
40 FOR I = A TO A + L
|
||||
50 READ B: POKE I,B: NEXT
|
||||
60 H = 256: POKE 12,A / H
|
||||
70 POKE 11,A - PEEK (12) * H
|
||||
80 DEF FN FA(N) = USR (N < 2) + N * FN FA(N - 1)
|
||||
90 PRINT FN FA(4)
|
||||
21
Task/Factorial/ArnoldC/factorial.arnoldc
Normal file
21
Task/Factorial/ArnoldC/factorial.arnoldc
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
LISTEN TO ME VERY CAREFULLY factorial
|
||||
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE n
|
||||
GIVE THESE PEOPLE AIR
|
||||
BECAUSE I'M GOING TO SAY PLEASE n
|
||||
BULLS***
|
||||
I'LL BE BACK 1
|
||||
YOU HAVE NO RESPECT FOR LOGIC
|
||||
HEY CHRISTMAS TREE product
|
||||
YOU SET US UP @NO PROBLEMO
|
||||
STICK AROUND n
|
||||
GET TO THE CHOPPER product
|
||||
HERE IS MY INVITATION product
|
||||
YOU'RE FIRED n
|
||||
ENOUGH TALK
|
||||
GET TO THE CHOPPER n
|
||||
HERE IS MY INVITATION n
|
||||
GET DOWN @NO PROBLEMO
|
||||
ENOUGH TALK
|
||||
CHILL
|
||||
I'LL BE BACK product
|
||||
HASTA LA VISTA, BABY
|
||||
4
Task/Factorial/Arturo/factorial-1.arturo
Normal file
4
Task/Factorial/Arturo/factorial-1.arturo
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
factorial: $[n][
|
||||
if? n>0 [n * factorial n-1]
|
||||
else [1]
|
||||
]
|
||||
3
Task/Factorial/Arturo/factorial-2.arturo
Normal file
3
Task/Factorial/Arturo/factorial-2.arturo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
factorial: $[n][
|
||||
fold.seed:1 1..n [a,b][a*b]
|
||||
]
|
||||
5
Task/Factorial/Arturo/factorial-3.arturo
Normal file
5
Task/Factorial/Arturo/factorial-3.arturo
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
factorial: $[n][product 1..n]
|
||||
|
||||
loop 1..19 [x][
|
||||
print ["Factorial of" x "=" factorial x]
|
||||
]
|
||||
7
Task/Factorial/AsciiDots/factorial.asciidots
Normal file
7
Task/Factorial/AsciiDots/factorial.asciidots
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/---------*--~-$#-&
|
||||
| /--;---\| [!]-\
|
||||
| *------++--*#1/
|
||||
| | /1#\ ||
|
||||
[*]*{-}-*~<+*?#-.
|
||||
*-------+-</
|
||||
\-#0----/
|
||||
9
Task/Factorial/AutoHotkey/factorial-1.ahk
Normal file
9
Task/Factorial/AutoHotkey/factorial-1.ahk
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
MsgBox % factorial(4)
|
||||
|
||||
factorial(n)
|
||||
{
|
||||
result := 1
|
||||
Loop, % n
|
||||
result *= A_Index
|
||||
Return result
|
||||
}
|
||||
6
Task/Factorial/AutoHotkey/factorial-2.ahk
Normal file
6
Task/Factorial/AutoHotkey/factorial-2.ahk
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
MsgBox % factorial(4)
|
||||
|
||||
factorial(n)
|
||||
{
|
||||
return n > 1 ? n-- * factorial(n) : 1
|
||||
}
|
||||
12
Task/Factorial/AutoIt/factorial-1.autoit
Normal file
12
Task/Factorial/AutoIt/factorial-1.autoit
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
;AutoIt Version: 3.2.10.0
|
||||
MsgBox (0,"Factorial",factorial(6))
|
||||
Func factorial($int)
|
||||
If $int < 0 Then
|
||||
Return 0
|
||||
EndIf
|
||||
$fact = 1
|
||||
For $i = 1 To $int
|
||||
$fact = $fact * $i
|
||||
Next
|
||||
Return $fact
|
||||
EndFunc
|
||||
10
Task/Factorial/AutoIt/factorial-2.autoit
Normal file
10
Task/Factorial/AutoIt/factorial-2.autoit
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
;AutoIt Version: 3.2.10.0
|
||||
MsgBox (0,"Factorial",factorial(6))
|
||||
Func factorial($int)
|
||||
if $int < 0 Then
|
||||
return 0
|
||||
Elseif $int == 0 Then
|
||||
return 1
|
||||
EndIf
|
||||
return $int * factorial($int - 1)
|
||||
EndFunc
|
||||
1
Task/Factorial/Avail/factorial-1.avail
Normal file
1
Task/Factorial/Avail/factorial-1.avail
Normal file
|
|
@ -0,0 +1 @@
|
|||
Assert: 7! = 5040;
|
||||
8
Task/Factorial/Avail/factorial-2.avail
Normal file
8
Task/Factorial/Avail/factorial-2.avail
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Method "_`!" is [n : [0..1] | 1];
|
||||
|
||||
Method "_`!" is
|
||||
[
|
||||
n : [2..∞)
|
||||
|
|
||||
left fold 2 to n through [k : [2..∞), s : [2..∞) | k × s]
|
||||
];
|
||||
7
Task/Factorial/Axe/factorial-1.axe
Normal file
7
Task/Factorial/Axe/factorial-1.axe
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Lbl FACT
|
||||
1→R
|
||||
For(I,1,r₁)
|
||||
R*I→R
|
||||
End
|
||||
R
|
||||
Return
|
||||
3
Task/Factorial/Axe/factorial-2.axe
Normal file
3
Task/Factorial/Axe/factorial-2.axe
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Lbl FACT
|
||||
r₁??1,r₁*FACT(r₁-1)
|
||||
Return
|
||||
8
Task/Factorial/BASIC/factorial-1.basic
Normal file
8
Task/Factorial/BASIC/factorial-1.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
FUNCTION factorial (n AS Integer) AS Integer
|
||||
DIM f AS Integer, i AS Integer
|
||||
f = 1
|
||||
FOR i = 2 TO n
|
||||
f = f*i
|
||||
NEXT i
|
||||
factorial = f
|
||||
END FUNCTION
|
||||
7
Task/Factorial/BASIC/factorial-2.basic
Normal file
7
Task/Factorial/BASIC/factorial-2.basic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
FUNCTION factorial (n AS Integer) AS Integer
|
||||
IF n < 2 THEN
|
||||
factorial = 1
|
||||
ELSE
|
||||
factorial = n * factorial(n-1)
|
||||
END IF
|
||||
END FUNCTION
|
||||
12
Task/Factorial/BASIC256/factorial-1.basic
Normal file
12
Task/Factorial/BASIC256/factorial-1.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
print "enter a number, n = ";
|
||||
input n
|
||||
print string(n) + "! = " + string(factorial(n))
|
||||
|
||||
function factorial(n)
|
||||
factorial = 1
|
||||
if n > 0 then
|
||||
for p = 1 to n
|
||||
factorial *= p
|
||||
next p
|
||||
end if
|
||||
end function
|
||||
11
Task/Factorial/BASIC256/factorial-2.basic
Normal file
11
Task/Factorial/BASIC256/factorial-2.basic
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
print "enter a number, n = ";
|
||||
input n
|
||||
print string(n) + "! = " + string(factorial(n))
|
||||
|
||||
function factorial(n)
|
||||
if n > 0 then
|
||||
factorial = n * factorial(n-1)
|
||||
else
|
||||
factorial = 1
|
||||
end if
|
||||
end function
|
||||
8
Task/Factorial/BBC-BASIC/factorial.basic
Normal file
8
Task/Factorial/BBC-BASIC/factorial.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
*FLOAT64
|
||||
@% = &1010
|
||||
|
||||
PRINT FNfactorial(18)
|
||||
END
|
||||
|
||||
DEF FNfactorial(n)
|
||||
IF n <= 1 THEN = 1 ELSE = n * FNfactorial(n-1)
|
||||
2
Task/Factorial/BQN/factorial.bqn
Normal file
2
Task/Factorial/BQN/factorial.bqn
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
Fac ← ×´1+↕
|
||||
! 720 ≡ Fac 6
|
||||
11
Task/Factorial/BaCon/factorial.bacon
Normal file
11
Task/Factorial/BaCon/factorial.bacon
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
' Factorial
|
||||
FUNCTION factorial(NUMBER n) TYPE NUMBER
|
||||
IF n <= 1 THEN
|
||||
RETURN 1
|
||||
ELSE
|
||||
RETURN n * factorial(n - 1)
|
||||
ENDIF
|
||||
END FUNCTION
|
||||
|
||||
n = VAL(TOKEN$(ARGUMENT$, 2))
|
||||
PRINT n, factorial(n) FORMAT "%ld! = %ld\n"
|
||||
10
Task/Factorial/Babel/factorial-1.pb
Normal file
10
Task/Factorial/Babel/factorial-1.pb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
((main
|
||||
{(0 1 2 3 4 5 6 7 8 9 10)
|
||||
{fact ! %d nl <<}
|
||||
each})
|
||||
|
||||
(fact
|
||||
{({dup 0 =}{ zap 1 }
|
||||
{dup 1 =}{ zap 1 }
|
||||
{1 }{ <- 1 {iter 1 + *} -> 1 - times })
|
||||
cond}))
|
||||
10
Task/Factorial/Babel/factorial-2.pb
Normal file
10
Task/Factorial/Babel/factorial-2.pb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
((main
|
||||
{(0 1 2 3 4 5 6 7 8 9 10)
|
||||
{fact ! %d nl <<}
|
||||
each})
|
||||
|
||||
(fact
|
||||
{({dup 0 =}{ zap 1 }
|
||||
{dup 1 =}{ zap 1 }
|
||||
{1 }{ dup 1 - fact ! *})
|
||||
cond}))
|
||||
9
Task/Factorial/Batch-File/factorial.bat
Normal file
9
Task/Factorial/Batch-File/factorial.bat
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
@echo off
|
||||
set /p x=
|
||||
set /a fs=%x%-1
|
||||
set y=%x%
|
||||
FOR /L %%a IN (%fs%, -1, 1) DO SET /a y*=%%a
|
||||
if %x% EQU 0 set y=1
|
||||
echo %y%
|
||||
pause
|
||||
exit
|
||||
7
Task/Factorial/Bc/factorial.bc
Normal file
7
Task/Factorial/Bc/factorial.bc
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#! /usr/bin/bc -q
|
||||
|
||||
define f(x) {
|
||||
if (x <= 1) return (1); return (f(x-1) * x)
|
||||
}
|
||||
f(1000)
|
||||
quit
|
||||
19
Task/Factorial/Beads/factorial.beads
Normal file
19
Task/Factorial/Beads/factorial.beads
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
beads 1 program Factorial
|
||||
// only works for cardinal numbers 0..N
|
||||
calc main_init
|
||||
log to_str(Iterative(4)) // 24
|
||||
log to_str(Recursive(5)) // 120
|
||||
|
||||
calc Iterative(
|
||||
n:num -- number of iterations
|
||||
):num -- result
|
||||
var total = 1
|
||||
loop from:2 to:n index:ix
|
||||
total = ix * total
|
||||
return total
|
||||
|
||||
calc Recursive ditto
|
||||
if n <= 1
|
||||
return 1
|
||||
else
|
||||
return n * Recursive(n-1)
|
||||
4
Task/Factorial/Beeswax/factorial-1.beeswax
Normal file
4
Task/Factorial/Beeswax/factorial-1.beeswax
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
p <
|
||||
_>1FT"pF>M"p~.~d
|
||||
>Pd >~{Np
|
||||
d <
|
||||
3
Task/Factorial/Beeswax/factorial-2.beeswax
Normal file
3
Task/Factorial/Beeswax/factorial-2.beeswax
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
p <
|
||||
_1FT"pF>M"p~.~d
|
||||
>Pd >~{;
|
||||
13
Task/Factorial/Beeswax/factorial-3.beeswax
Normal file
13
Task/Factorial/Beeswax/factorial-3.beeswax
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
julia> beeswax("factorial.bswx")
|
||||
i0
|
||||
1
|
||||
i1
|
||||
1
|
||||
i2
|
||||
2
|
||||
i3
|
||||
6
|
||||
i10
|
||||
3628800
|
||||
i22
|
||||
17196083355034583040
|
||||
3
Task/Factorial/Befunge/factorial.bf
Normal file
3
Task/Factorial/Befunge/factorial.bf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
&1\> :v v *<
|
||||
^-1:_$>\:|
|
||||
@.$<
|
||||
27
Task/Factorial/Bracmat/factorial-1.bracmat
Normal file
27
Task/Factorial/Bracmat/factorial-1.bracmat
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(
|
||||
=
|
||||
. !arg:0&1
|
||||
| !arg
|
||||
* ( (
|
||||
= r
|
||||
. !arg:?r
|
||||
&
|
||||
' (
|
||||
. !arg:0&1
|
||||
| !arg*(($r)$($r))$(!arg+-1)
|
||||
)
|
||||
)
|
||||
$ (
|
||||
= r
|
||||
. !arg:?r
|
||||
&
|
||||
' (
|
||||
. !arg:0&1
|
||||
| !arg*(($r)$($r))$(!arg+-1)
|
||||
)
|
||||
)
|
||||
)
|
||||
$ (!arg+-1)
|
||||
)
|
||||
$ 10
|
||||
: 3628800
|
||||
4
Task/Factorial/Bracmat/factorial-2.bracmat
Normal file
4
Task/Factorial/Bracmat/factorial-2.bracmat
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
( (=(r.!arg:?r&'(.!arg:0&1|!arg*(($r)$($r))$(!arg+-1)))):?g
|
||||
& (!g$!g):?f
|
||||
& !f$10
|
||||
)
|
||||
7
Task/Factorial/Bracmat/factorial-3.bracmat
Normal file
7
Task/Factorial/Bracmat/factorial-3.bracmat
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(factorial=
|
||||
r
|
||||
. !arg:?r
|
||||
& whl
|
||||
' (!arg:>1&(!arg+-1:?arg)*!r:?r)
|
||||
& !r
|
||||
);
|
||||
4
Task/Factorial/Brainf---/factorial.bf
Normal file
4
Task/Factorial/Brainf---/factorial.bf
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
>++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]<[>+<-]<[>+<-[>+<-[>
|
||||
+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-
|
||||
]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]<<<<[<[
|
||||
>+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<[<[>+>+<<-]>.<<<<<]>.>>>>]
|
||||
3
Task/Factorial/Brat/factorial.brat
Normal file
3
Task/Factorial/Brat/factorial.brat
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
factorial = { x |
|
||||
true? x == 0 1 { x * factorial(x - 1)}
|
||||
}
|
||||
2
Task/Factorial/Burlesque/factorial-1.blq
Normal file
2
Task/Factorial/Burlesque/factorial-1.blq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
blsq ) 6?!
|
||||
720
|
||||
12
Task/Factorial/Burlesque/factorial-2.blq
Normal file
12
Task/Factorial/Burlesque/factorial-2.blq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
blsq ) 1 6r@pd
|
||||
720
|
||||
blsq ) 1 6r@{?*}r[
|
||||
720
|
||||
blsq ) 2 6r@(.*)\/[[1+]e!.*
|
||||
720
|
||||
blsq ) 1 6r@p^{.*}5E!
|
||||
720
|
||||
blsq ) 6ropd
|
||||
720
|
||||
blsq ) 7ro)(.*){0 1 11}die!
|
||||
720
|
||||
8
Task/Factorial/C++/factorial-1.cpp
Normal file
8
Task/Factorial/C++/factorial-1.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <boost/iterator/counting_iterator.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
int factorial(int n)
|
||||
{
|
||||
// last is one-past-end
|
||||
return std::accumulate(boost::counting_iterator<int>(1), boost::counting_iterator<int>(n+1), 1, std::multiplies<int>());
|
||||
}
|
||||
8
Task/Factorial/C++/factorial-2.cpp
Normal file
8
Task/Factorial/C++/factorial-2.cpp
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
//iteration with while
|
||||
long long int factorial(long long int n)
|
||||
{
|
||||
long long int r = 1;
|
||||
while(1<n)
|
||||
r *= n--;
|
||||
return r;
|
||||
}
|
||||
19
Task/Factorial/C++/factorial-3.cpp
Normal file
19
Task/Factorial/C++/factorial-3.cpp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
template <int N>
|
||||
struct Factorial
|
||||
{
|
||||
enum { value = N * Factorial<N - 1>::value };
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Factorial<0>
|
||||
{
|
||||
enum { value = 1 };
|
||||
};
|
||||
|
||||
// Factorial<4>::value == 24
|
||||
// Factorial<0>::value == 1
|
||||
void foo()
|
||||
{
|
||||
int x = Factorial<4>::value; // == 24
|
||||
int y = Factorial<0>::value; // == 1
|
||||
}
|
||||
107
Task/Factorial/C++/factorial-4.cpp
Normal file
107
Task/Factorial/C++/factorial-4.cpp
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <vector>
|
||||
#include <boost/iterator/counting_iterator.hpp>
|
||||
|
||||
using ulli = unsigned long long int;
|
||||
|
||||
// bad style do-while and wrong for Factorial1(0LL) -> 0 !!!
|
||||
ulli Factorial1(ulli m_nValue) {
|
||||
ulli result = m_nValue;
|
||||
ulli result_next;
|
||||
ulli pc = m_nValue;
|
||||
do {
|
||||
result_next = result * (pc - 1);
|
||||
result = result_next;
|
||||
pc--;
|
||||
} while (pc > 2);
|
||||
return result;
|
||||
}
|
||||
|
||||
// iteration with while
|
||||
ulli Factorial2(ulli n) {
|
||||
ulli r = 1;
|
||||
while (1 < n)
|
||||
r *= n--;
|
||||
return r;
|
||||
}
|
||||
|
||||
// recursive
|
||||
ulli Factorial3(ulli n) {
|
||||
return n < 2 ? 1 : n * Factorial3(n - 1);
|
||||
}
|
||||
|
||||
// tail recursive
|
||||
inline ulli _fac_aux(ulli n, ulli acc) {
|
||||
return n < 1 ? acc : _fac_aux(n - 1, acc * n);
|
||||
}
|
||||
ulli Factorial4(ulli n) {
|
||||
return _fac_aux(n, 1);
|
||||
}
|
||||
|
||||
// accumulate with functor
|
||||
ulli Factorial5(ulli n) {
|
||||
// last is one-past-end
|
||||
return std::accumulate(boost::counting_iterator<ulli>(1ULL),
|
||||
boost::counting_iterator<ulli>(n + 1ULL), 1ULL,
|
||||
std::multiplies<ulli>());
|
||||
}
|
||||
|
||||
// accumulate with lambda
|
||||
ulli Factorial6(ulli n) {
|
||||
// last is one-past-end
|
||||
return std::accumulate(boost::counting_iterator<ulli>(1ULL),
|
||||
boost::counting_iterator<ulli>(n + 1ULL), 1ULL,
|
||||
[](ulli a, ulli b) { return a * b; });
|
||||
}
|
||||
|
||||
int main() {
|
||||
ulli v = 20; // max value with unsigned long long int
|
||||
ulli result;
|
||||
std::cout << std::fixed;
|
||||
using duration = std::chrono::duration<double, std::micro>;
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
result = Factorial1(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "do-while(1) result " << result << " took " << duration(t2 - t1).count() << " µs\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
result = Factorial2(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "while(2) result " << result << " took " << duration(t2 - t1).count() << " µs\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
result = Factorial3(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "recursive(3) result " << result << " took " << duration(t2 - t1).count() << " µs\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
result = Factorial3(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "tail recursive(4) result " << result << " took " << duration(t2 - t1).count() << " µs\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
result = Factorial5(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "std::accumulate(5) result " << result << " took " << duration(t2 - t1).count() << " µs\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto t1 = std::chrono::high_resolution_clock::now();
|
||||
result = Factorial6(v);
|
||||
auto t2 = std::chrono::high_resolution_clock::now();
|
||||
std::cout << "std::accumulate lambda(6) result " << result << " took " << duration(t2 - t1).count() << " µs\n";
|
||||
}
|
||||
}
|
||||
13
Task/Factorial/C-Shell/factorial.csh
Normal file
13
Task/Factorial/C-Shell/factorial.csh
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
alias factorial eval \''set factorial_args=( \!*:q ) \\
|
||||
@ factorial_n = $factorial_args[2] \\
|
||||
@ factorial_i = 1 \\
|
||||
while ( $factorial_n >= 2 ) \\
|
||||
@ factorial_i *= $factorial_n \\
|
||||
@ factorial_n -= 1 \\
|
||||
end \\
|
||||
@ $factorial_args[1] = $factorial_i \\
|
||||
'\'
|
||||
|
||||
factorial f 12
|
||||
echo $f
|
||||
# => 479001600
|
||||
22
Task/Factorial/C-sharp/factorial-1.cs
Normal file
22
Task/Factorial/C-sharp/factorial-1.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
|
||||
var accumulator = 1;
|
||||
for (var factor = 1; factor <= number; factor++)
|
||||
{
|
||||
accumulator *= factor;
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
17
Task/Factorial/C-sharp/factorial-2.cs
Normal file
17
Task/Factorial/C-sharp/factorial-2.cs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
|
||||
return number == 0 ? 1 : number * Factorial(number - 1);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
27
Task/Factorial/C-sharp/factorial-3.cs
Normal file
27
Task/Factorial/C-sharp/factorial-3.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
|
||||
return Factorial(number, 1);
|
||||
}
|
||||
|
||||
static int Factorial(int number, int accumulator)
|
||||
{
|
||||
if(number < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(number), number, "Must be zero or a positive number.");
|
||||
if(accumulator < 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(accumulator), accumulator, "Must be a positive number.");
|
||||
|
||||
return number == 0 ? accumulator : Factorial(number - 1, number * accumulator);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
15
Task/Factorial/C-sharp/factorial-4.cs
Normal file
15
Task/Factorial/C-sharp/factorial-4.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static int Factorial(int number)
|
||||
{
|
||||
return Enumerable.Range(1, number).Aggregate((accumulator, factor) => accumulator * factor);
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
Console.WriteLine(Factorial(10));
|
||||
}
|
||||
}
|
||||
31
Task/Factorial/C-sharp/factorial-5.cs
Normal file
31
Task/Factorial/C-sharp/factorial-5.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
using System.Linq;
|
||||
class Program
|
||||
{
|
||||
static BigInteger factorial(int n) // iterative
|
||||
{
|
||||
BigInteger acc = 1; for (int i = 1; i <= n; i++) acc *= i; return acc;
|
||||
}
|
||||
|
||||
static public BigInteger Factorial(int number) // functional
|
||||
{
|
||||
return Enumerable.Range(1, number).Aggregate(new BigInteger(1), (acc, num) => acc * num);
|
||||
}
|
||||
|
||||
static public BI FactorialQ(int number) // functional quick, uses prodtree method
|
||||
{
|
||||
var s = Enumerable.Range(1, number).Select(num => new BI(num)).ToArray();
|
||||
int top = s.Length, nt, i, j;
|
||||
while (top > 1) {
|
||||
for (i = 0, j = top, nt = top >> 1; i < nt; i++) s[i] *= s[--j];
|
||||
top = nt + ((top & 1) == 1 ? 1 : 0);
|
||||
}
|
||||
return s[0];
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine(Factorial(250));
|
||||
}
|
||||
}
|
||||
6
Task/Factorial/C/factorial-1.c
Normal file
6
Task/Factorial/C/factorial-1.c
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
int factorial(int n) {
|
||||
int result = 1;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
result *= i;
|
||||
return result;
|
||||
}
|
||||
8
Task/Factorial/C/factorial-2.c
Normal file
8
Task/Factorial/C/factorial-2.c
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
int factorialSafe(int n) {
|
||||
int result = 1;
|
||||
if(n<0)
|
||||
return -1;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
result *= i;
|
||||
return result;
|
||||
}
|
||||
3
Task/Factorial/C/factorial-3.c
Normal file
3
Task/Factorial/C/factorial-3.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
int factorial(int n) {
|
||||
return n == 0 ? 1 : n * factorial(n - 1);
|
||||
}
|
||||
3
Task/Factorial/C/factorial-4.c
Normal file
3
Task/Factorial/C/factorial-4.c
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
int factorialSafe(int n) {
|
||||
return n<0 ? -1 : n == 0 ? 1 : n * factorialSafe(n - 1);
|
||||
}
|
||||
11
Task/Factorial/C/factorial-5.c
Normal file
11
Task/Factorial/C/factorial-5.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
int fac_aux(int n, int acc) {
|
||||
return n < 1 ? acc : fac_aux(n - 1, acc * n);
|
||||
}
|
||||
|
||||
int fac_auxSafe(int n, int acc) {
|
||||
return n<0 ? -1 : n < 1 ? acc : fac_aux(n - 1, acc * n);
|
||||
}
|
||||
|
||||
int factorial(int n) {
|
||||
return fac_aux(n, 1);
|
||||
}
|
||||
41
Task/Factorial/C/factorial-6.c
Normal file
41
Task/Factorial/C/factorial-6.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define l11l 0xFFFF
|
||||
#define ll1 for
|
||||
#define ll111 if
|
||||
#define l1l1 unsigned
|
||||
#define l111 struct
|
||||
#define lll11 short
|
||||
#define ll11l long
|
||||
#define ll1ll putchar
|
||||
#define l1l1l(l) l=malloc(sizeof(l111 llll1));l->lll1l=1-1;l->ll1l1=1-1;
|
||||
#define l1ll1 *lllll++=l1ll%10000;l1ll/=10000;
|
||||
#define l1lll ll111(!l1->lll1l){l1l1l(l1->lll1l);l1->lll1l->ll1l1=l1;}\
|
||||
lllll=(l1=l1->lll1l)->lll;ll=1-1;
|
||||
#define llll 1000
|
||||
|
||||
|
||||
|
||||
|
||||
l111 llll1 {
|
||||
l111 llll1 *
|
||||
lll1l,*ll1l1 ;l1l1 lll11 lll [
|
||||
llll];};main (){l111 llll1 *ll11,*l1l,*
|
||||
l1, *ll1l, * malloc ( ) ; l1l1 ll11l l1ll ;
|
||||
ll11l l11,ll ,l;l1l1 lll11 *lll1,* lllll; ll1(l
|
||||
=1-1 ;l< 14; ll1ll("\t\"8)>l\"9!.)>vl" [l]^'L'),++l
|
||||
);scanf("%d",&l);l1l1l(l1l) l1l1l(ll11 ) (l1=l1l)->
|
||||
lll[l1l->lll[1-1] =1]=l11l;ll1(l11 =1+1;l11<=l;
|
||||
++l11){l1=ll11; lll1 = (ll1l=( ll11=l1l))->
|
||||
lll; lllll =( l1l=l1)->lll; ll=(l1ll=1-1
|
||||
);ll1(;ll1l-> lll1l||l11l!= *lll1;){l1ll
|
||||
+=l11**lll1++ ;l1ll1 ll111 (++ll>llll){
|
||||
l1lll lll1=( ll1l =ll1l-> lll1l)->lll;
|
||||
}}ll1(;l1ll; ){l1ll1 ll111 (++ll>=llll)
|
||||
{ l1lll} } * lllll=l11l;}
|
||||
ll1(l=(ll=1- 1);(l<llll)&&
|
||||
(l1->lll[ l] !=l11l);++l); ll1 (;l1;l1=
|
||||
l1->ll1l1,l= llll){ll1(--l ;l>=1-1;--l,
|
||||
++ll)printf( (ll)?((ll%19) ?"%04d":(ll=
|
||||
19,"\n%04d") ):"%4d",l1-> lll[l] ) ; }
|
||||
ll1ll(10); }
|
||||
9
Task/Factorial/C3/factorial-1.c3
Normal file
9
Task/Factorial/C3/factorial-1.c3
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fn int factorial(int n)
|
||||
{
|
||||
int result = 1;
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
4
Task/Factorial/C3/factorial-2.c3
Normal file
4
Task/Factorial/C3/factorial-2.c3
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fn int factorial(int n)
|
||||
{
|
||||
return n == 0 ? 1 : n * factorial(n - 1);
|
||||
}
|
||||
13
Task/Factorial/C3/factorial-3.c3
Normal file
13
Task/Factorial/C3/factorial-3.c3
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
macro int factorial($n)
|
||||
{
|
||||
$if ($n == 0):
|
||||
return 1;
|
||||
$else:
|
||||
return $n * @factorial($n - 1);
|
||||
$endif;
|
||||
}
|
||||
|
||||
fn void test()
|
||||
{
|
||||
int x = @factorial(10);
|
||||
}
|
||||
8
Task/Factorial/CLIPS/factorial.clips
Normal file
8
Task/Factorial/CLIPS/factorial.clips
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(deffunction factorial (?a)
|
||||
(if (or (not (integerp ?a)) (< ?a 0)) then
|
||||
(printout t "Factorial Error!" crlf)
|
||||
else
|
||||
(if (= ?a 0) then
|
||||
1
|
||||
else
|
||||
(* ?a (factorial (- ?a 1))))))
|
||||
15
Task/Factorial/CLU/factorial.clu
Normal file
15
Task/Factorial/CLU/factorial.clu
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
factorial = proc (n: int) returns (int) signals (negative)
|
||||
if n<0 then signal negative
|
||||
elseif n=0 then return(1)
|
||||
else return(n * factorial(n-1))
|
||||
end
|
||||
end factorial
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
|
||||
for i: int in int$from_to(0, 10) do
|
||||
fac: int := factorial(i)
|
||||
stream$putl(po, int$unparse(i) || "! = " || int$unparse(fac))
|
||||
end
|
||||
end start_up
|
||||
10
Task/Factorial/CMake/factorial.cmake
Normal file
10
Task/Factorial/CMake/factorial.cmake
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function(factorial var n)
|
||||
set(product 1)
|
||||
foreach(i RANGE 2 ${n})
|
||||
math(EXPR product "${product} * ${i}")
|
||||
endforeach(i)
|
||||
set(${var} ${product} PARENT_SCOPE)
|
||||
endfunction(factorial)
|
||||
|
||||
factorial(f 12)
|
||||
message("12! = ${f}")
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue