June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -10,4 +10,8 @@ Write a function to return the factorial of a number.
Solutions can be iterative or recursive.
Support for trapping negative &nbsp; <big> ''n'' </big> &nbsp; errors is optional.
;Related task:
* &nbsp; [[Primorial numbers]]
<br><br>

View file

@ -1,3 +1,8 @@
int factorial(int n) {
return n == 0 ? 1 : n * factorial(n - 1);
int factorialSafe(int n) {
int result = 1;
if(n<0)
return -1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}

View file

@ -1,7 +1,3 @@
int fac_aux(int n, int acc) {
return n < 1 ? acc : fac_aux(n - 1, acc * n);
}
int factorial(int n) {
return fac_aux(n, 1);
return n == 0 ? 1 : n * factorial(n - 1);
}

View file

@ -0,0 +1,3 @@
int factorialSafe(int n) {
return n<0 ? -1 : n == 0 ? 1 : n * factorialSafe(n - 1);
}

View file

@ -0,0 +1,13 @@
int fac_aux(int n, int acc) {
return n < 1 ? acc : fac_aux(n - 1, acc * n);
}
/*Handles negative n, Abhishek Ghosh, 1st November 2017*/
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);
}

View 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); }

View file

@ -0,0 +1,10 @@
;; Project : Factorial
;; Date : 2018/03/09
;; Author : Gal Zsolt [~ CalmoSoft ~]
;; Email : <calmosoft@gmail.com>
(defun factorial (n)
(cond ((= n 1) 1)
(t (* n (factorial (- n 1))))))
(format t "~a" "factorial of 8: ")
(factorial 8)

View file

@ -0,0 +1,7 @@
factorialAux : Int -> Int -> Int
factorialAux a acc =
if a < 2 then acc else factorialAux (a - 1) (a * acc)
factorial : Int -> Int
factorial a =
factorialAux a 1

View file

@ -0,0 +1,5 @@
import List exposing (product, range)
factorial : Int -> Int
factorial a =
product (range 1 a)

View file

@ -0,0 +1,17 @@
package main
import (
"fmt"
"math"
)
func factorial(n float64) float64 {
return math.Gamma(n + 1)
}
func main() {
for i := 0.; i <= 10; i++ {
fmt.Println(i, factorial(i))
}
fmt.Println(100, factorial(100))
}

View file

@ -0,0 +1,26 @@
package main
import (
"fmt"
"math"
"math/big"
)
func lfactorial(n float64) float64 {
l, _ := math.Lgamma(n + 1)
return l
}
func factorial(n float64) *big.Float {
i, frac := math.Modf(lfactorial(n) * math.Log2E)
z := big.NewFloat(math.Exp2(frac))
return z.SetMantExp(z, int(i))
}
func main() {
for i := 0.; i <= 10; i++ {
fmt.Println(i, factorial(i))
}
fmt.Println(100, factorial(100))
fmt.Println(800, factorial(800))
}

View file

@ -0,0 +1,9 @@
U64 Factorial(U64 n) {
U64 i, result = 1;
for (i = 1; i <= n; ++i)
result *= i;
return result;
}
Print("1: %d\n", Factorial(1));
Print("10: %d\n", Factorial(10));

View file

@ -0,0 +1,11 @@
I64 Factorial(I64 n) {
if (n == 0)
return 1;
if (n < 0)
return -1 * ((-1 * n) * Factorial((-1 * n) - 1));
return n * Factorial(n - 1));
}
Print("+1: %d\n", Factorial(1));
Print("+10: %d\n", Factorial(10));
Print("-10: %d\n", Factorial(-10));

View file

@ -1,20 +1,28 @@
(function (n) {
(() => {
'use strict';
// factorial :: Int -> Int
let factorial = (n) => range(1, n).reduce(product, 1);
const factorial = n =>
enumFromTo(1, n)
.reduce(product, 1);
const test = () =>
factorial(18);
// --> 6402373705728000
// GENERIC FUNCTIONS ----------------------------------
// product :: Num -> Num -> Num
let product = (a, b) => a * b,
const product = (a, b) => a * b;
// range :: Int -> Int -> [Int]
range = (m, n) =>
Array.from({
length: (n - m) + 1
}, (_, i) => m + i)
// range :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
return factorial(n);
})(18);
// MAIN ------
return test();
})();

View file

@ -1,9 +1,12 @@
julia> help(factorial)
Loading help data...
Base.factorial(n)
function fact(n::Integer)
n < 0 && return zero(n)
f = one(n)
for i in 2:n
f *= i
end
return f
end
Factorial of n
Base.factorial(n, k)
Compute "factorial(n)/factorial(k)"
for i in 10:20
println("$i -> ", fact(i))
end

View file

@ -1,10 +1,2 @@
function factorial(n::Integer)
if n < 0
return zero(n)
end
f = one(n)
for i = 2:n
f *= i
end
return f
end
fact2(n::Integer) = prod(Base.OneTo(n))
@show fact2(20)

View file

@ -0,0 +1,26 @@
MODULE Factorial;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
PROCEDURE Factorial(n : CARDINAL) : CARDINAL;
VAR result : CARDINAL;
BEGIN
result := 1;
WHILE n#0 DO
result := result * n;
DEC(n)
END;
RETURN result
END Factorial;
VAR
buf : ARRAY[0..63] OF CHAR;
n : CARDINAL;
BEGIN
FOR n:=0 TO 10 DO
FormatString("%2c! = %7c\n", buf, n, Factorial(n));
WriteString(buf)
END;
ReadChar
END Factorial.

View file

@ -1,3 +1,2 @@
proc factorial(x): int =
if x > 0: x * factorial(x - 1)
else: 1
import math
let i:int = fac(x)

View file

@ -1,4 +1,3 @@
proc factorial(x): int =
result = 1
for i in 1..x+1:
result *= i
if x > 0: x * factorial(x - 1)
else: 1

View file

@ -0,0 +1,4 @@
proc factorial(x: int): int =
result = 1
for i in 2..x:
result *= i

View file

@ -0,0 +1,12 @@
let rec factorial n =
let rec loop acc = function
| 0 -> acc
| n -> loop (Z.mul (Z.of_int n) acc) (n - 1)
in loop Z.one n
let () =
if not !Sys.interactive then
begin
Sys.argv.(1) |> int_of_string |> factorial |> Z.print;
print_newline ()
end

View file

@ -0,0 +1,7 @@
int fact(int n){
if(n <= 1){
return 1;
} else{
return n*fact(n-1);
}
}

View file

@ -1,14 +1,15 @@
begin
integer procedure factorial( n );
integer procedure factorial(n);
integer n;
begin
integer fact, i;
fact := 1;
if n > 1 then
for i := 2 step 1 until n do
fact := fact * i;
for i := 2 step 1 until n do
fact := fact * i;
factorial := fact
end;
outint( factorial( 6 ), 5);
outimage
integer f; outtext("factorials:"); outimage;
for f := 0, 1, 2, 6, 9 do begin
outint(f, 2); outint(factorial(f), 8); outimage
end
end

View file

@ -0,0 +1,28 @@
Option Explicit
Sub Main()
Dim i As Integer
For i = 1 To 17
Debug.Print "Factorial " & i & " , recursive : " & FactRec(i) & ", iterative : " & FactIter(i)
Next
Debug.Print "Factorial 120, recursive : " & FactRec(120) & ", iterative : " & FactIter(120)
End Sub
Private Function FactRec(Nb As Integer) As String
If Nb > 170 Or Nb < 0 Then FactRec = 0: Exit Function
If Nb = 1 Or Nb = 0 Then
FactRec = 1
Else
FactRec = Nb * FactRec(Nb - 1)
End If
End Function
Private Function FactIter(Nb As Integer)
If Nb > 170 Or Nb < 0 Then FactIter = 0: Exit Function
Dim i As Integer, F
F = 1
For i = 1 To Nb
F = F * i
Next i
FactIter = F
End Function

View file

@ -1,30 +1,74 @@
use std.textio.all;
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
USE ieee.numeric_std.ALL;
entity rc is
end entity rc;
ENTITY Factorial IS
GENERIC (
Nbin : INTEGER := 3 ; -- number of bit to input number
Nbou : INTEGER := 13) ; -- number of bit to output factorial
PORT (
clk : IN STD_LOGIC ; -- clock of circuit
sr : IN STD_LOGIC_VECTOR(1 DOWNTO 0); -- set and reset
N : IN STD_LOGIC_VECTOR(Nbin-1 DOWNTO 0) ; -- max number
Fn : OUT STD_LOGIC_VECTOR(Nbou-1 DOWNTO 0)); -- factorial of "n"
END Factorial ;
architecture beh of rc is
function fact(n:integer) return integer is
variable f: integer := 1;
variable i: integer;
begin
for i in 2 to n loop
f := f*i;
end loop;
return f;
end;
ARCHITECTURE Behavior OF Factorial IS
---------------------- Program Multiplication --------------------------------
FUNCTION Mult ( CONSTANT MFa : IN UNSIGNED ;
CONSTANT MI : IN UNSIGNED ) RETURN UNSIGNED IS
VARIABLE Z : UNSIGNED(MFa'RANGE) ;
VARIABLE U : UNSIGNED(MI'RANGE) ;
BEGIN
Z := TO_UNSIGNED(0, MFa'LENGTH) ; -- to obtain the multiplication
U := MI ; -- regressive counter
LOOP
Z := Z + MFa ; -- make multiplication
U := U - 1 ;
EXIT WHEN U = 0 ;
END LOOP ;
RETURN Z ;
END Mult ;
-------------------Program Factorial ---------------------------------------
FUNCTION Fact (CONSTANT Nx : IN NATURAL ) RETURN UNSIGNED IS
VARIABLE C : NATURAL RANGE 0 TO 2**Nbin-1 ;
VARIABLE I : UNSIGNED(Nbin-1 DOWNTO 0) ;
VARIABLE Fa : UNSIGNED(Nbou-1 DOWNTO 0) ;
BEGIN
C := 0 ; -- counter
I := TO_UNSIGNED(1, Nbin) ;
Fa := TO_UNSIGNED(1, Nbou) ;
LOOP
EXIT WHEN C = Nx ; -- end loop
C := C + 1 ; -- progressive couter
Fa := Mult (Fa , I ); -- call function to make a multiplication
I := I + 1 ; --
END LOOP ;
RETURN Fa ;
END Fact ;
--------------------- Program TO Call Factorial Function ------------------------------------------------------
TYPE Table IS ARRAY (0 TO 2**Nbin-1) OF UNSIGNED(Nbou-1 DOWNTO 0) ;
FUNCTION Call_Fact RETURN Table IS
VARIABLE Fc : Table ;
BEGIN
FOR c IN 0 TO 2**Nbin-1 LOOP
Fc(c) := Fact(c) ;
END LOOP ;
RETURN Fc ;
END FUNCTION Call_Fact;
CONSTANT Result : Table := Call_Fact ;
------------------------------------------------------------------------------------------------------------
SIGNAL Nin : STD_LOGIC_VECTOR(N'RANGE) ;
BEGIN -- start of architecture
begin
process
variable i: integer;
variable l: line;
begin
for i in 0 to 5 loop
write(l, i);
write(l, string'(" "));
write(l, fact(i));
writeline(output, l);
end loop;
wait;
end process;
end;
Nin <= N WHEN RISING_EDGE(clk) AND sr = "10" ELSE
(OTHERS => '0') WHEN RISING_EDGE(clk) AND sr = "01" ELSE
UNAFFECTED;
Fn <= STD_LOGIC_VECTOR(Result(TO_INTEGER(UNSIGNED(Nin)))) WHEN RISING_EDGE(clk) ;
END Behavior ;