Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,3 +1,5 @@
The '''Factorial Function''' of a positive integer, ''n'', is defined as the product of the sequence ''n'', ''n''-1, ''n''-2, ...1 and the factorial of zero, 0, is [[wp:Factorial#Definition|defined]] as being 1.
Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative n errors is optional.
Write a function to return the factorial of a number.
Solutions can be iterative or recursive.
Support for trapping negative n errors is optional.

View file

@ -4,4 +4,5 @@ category:
- Memoization
- Classic CS problems and programs
- Arithmetic
- Simple
note: Arithmetic operations

View 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

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

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

View file

@ -1,16 +0,0 @@
main:
{ argv 0 th $d
fac
%d cr << }
fac!:
{ dup zero?
{ dup one?
{ cp 1 - fac * }
{ zap 1 }
if }
{ zap 1 }
if }
one?! : { 1 = }
zero?!: { 0 = }

View file

@ -1,4 +1,4 @@
uint factorial(in uint n) pure nothrow
uint factorial(in uint n) pure nothrow @nogc
in {
assert(n <= 12);
} body {

View file

@ -1,4 +1,4 @@
uint factorial(in uint n) pure nothrow
uint factorial(in uint n) pure nothrow @nogc
in {
assert(n <= 12);
} body {

View file

@ -1,6 +1,6 @@
import std.stdio, std.algorithm, std.range;
uint factorial(in uint n) pure nothrow
uint factorial(in uint n) pure nothrow @nogc
in {
assert(n <= 12);
} body {

View file

@ -2,7 +2,7 @@ uint factorial(in uint n) pure nothrow
in {
assert(n <= 12);
} body {
static uint inner(uint n, uint acc) pure nothrow {
static uint inner(uint n, uint acc) pure nothrow @nogc {
if (n < 1)
return acc;
else

View file

@ -0,0 +1,14 @@
defmodule Factorial do
# Simple recursive function
def fac(0), do: 1
def fac(n) when n > 0, do: n * fac(n - 1)
# Tail recursive function
def fac_tail(n), do: fac_tail(n, 1)
def fac_tail(0, acc), do: acc
def fac_tail(n, acc) when n > 0, do: fac_tail(n - 1, acc * n)
# Using Enumeration features
def fac_reduce(0), do: 0
def fac_reduce(n) when n > 0, do: Enum.reduce(1..n, 1, &*/2)
end

View file

@ -0,0 +1,14 @@
: factorial ( n -- d )
dup 33 u> -24 and throw
dup 2 < IF
drop 1.
ELSE
1.
rot 1+ 2 DO
i 1 m*/
LOOP
THEN ;
33 factorial d. 8683317618811886495518194401280000000 ok
-5 factorial d.
:2: Invalid numeric argument

View file

@ -1 +1 @@
factorial n = foldl (*) 1 [1..n]
factorial = product . enumFromTo 1

View file

@ -1 +1 @@
factorials = scanl (*) 1 [1..]
factorial n = foldl (*) 1 [1..n]

View file

@ -1,3 +1 @@
factorial :: Integral -> Integral
factorial 0 = 1
factorial n = n * factorial (n-1)
factorials = scanl (*) 1 [1..]

View file

@ -1,5 +1,3 @@
fac n
| n >= 0 = go 1 n
| otherwise = error "Negative factorial!"
where go acc 0 = acc
go acc n = go (acc * n) (n - 1)
factorial :: Integral -> Integral
factorial 0 = 1
factorial n = n * factorial (n-1)

View file

@ -0,0 +1,5 @@
fac n
| n >= 0 = go 1 n
| otherwise = error "Negative factorial!"
where go acc 0 = acc
go acc n = go (acc * n) (n - 1)

View file

@ -0,0 +1,8 @@
{-# LANGUAGE PostfixOperators #-}
(!) 0 = 1
(!) n = n * ((n-1)!)
main = do
print (5!)
print ((4!)!)

View file

@ -0,0 +1,16 @@
:- module factorial.
:- interface.
:- import_module integer.
:- func factorial(integer) = integer.
:- implementation.
:- pragma memo(factorial/1).
factorial(N) =
( N =< integer(0)
-> integer(1)
; factorial(N - integer(1)) * N
).

View file

@ -0,0 +1,18 @@
:- module test_factorial.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module factorial.
:- import_module char, integer, list, string.
main(!IO) :-
command_line_arguments(Args, !IO),
filter(is_all_digits, Args, CleanArgs),
Arg1 = list.det_index0(CleanArgs, 0),
Number = integer.det_from_string(Arg1),
Result = factorial(Number),
Fmt = integer.to_string,
io.format("factorial(%s) = %s\n", [s(Fmt(Number)), s(Fmt(Result))], !IO).

View file

@ -0,0 +1 @@
factorial(100) = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

View file

@ -1 +1 @@
fact(n)=prod(k=2,n,k)
fact(n)=factorback([2..n])

View file

@ -0,0 +1,3 @@
use ntheory qw/factorial/;
# factorial returns a UV (native unsigned int) or Math::BigInt depending on size
say length( factorial(10000) );

View file

@ -0,0 +1,2 @@
use bigint;
say length( 10000->bfac );

View file

@ -0,0 +1,2 @@
use Math::GMP;
say length( Math::GMP->new(10000)->bfac );

View file

@ -0,0 +1,2 @@
use Math::Pari qw/ifact/;
say length( ifact(10000) );

View file

@ -1,2 +1,2 @@
(de fact (N)
(apply * (range 1 N) )
(apply * (range 1 N) ) )

View file

@ -1,4 +1,5 @@
from operator import mul
from functools import reduce
def factorial(n):
return reduce(mul, xrange(1,n+1), 1)
return reduce(mul, range(1,n+1), 1)

View file

@ -1,10 +1,27 @@
>>> for i in range(6):
print i, factorial(i)
from cmath import *
0 1
1 1
2 2
3 6
4 24
5 120
>>>
# Coefficients used by the GNU Scientific Library
g = 7
p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
def gamma(z):
z = complex(z)
# Reflection formula
if z.real < 0.5:
return pi / (sin(pi*z)*gamma(1-z))
else:
z -= 1
x = p[0]
for i in range(1, g+2):
x += p[i]/(z+i)
t = z + g + 0.5
return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
def factorial(n):
return gamma(n+1)
print "factorial(-0.5)**2=",factorial(-0.5)**2
for i in range(10):
print "factorial(%d)=%s"%(i,factorial(i))

View file

@ -1,27 +1,5 @@
from cmath import *
# Coefficients used by the GNU Scientific Library
g = 7
p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
def gamma(z):
z = complex(z)
# Reflection formula
if z.real < 0.5:
return pi / (sin(pi*z)*gamma(1-z))
else:
z -= 1
x = p[0]
for i in range(1, g+2):
x += p[i]/(z+i)
t = z + g + 0.5
return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
def factorial(n):
return gamma(n+1)
print "factorial(-0.5)**2=",factorial(-0.5)**2
for i in range(10):
print "factorial(%d)=%s"%(i,factorial(i))
z=1
if n>1:
z=n*factorial(n-1)
return z

View file

@ -1,34 +1,27 @@
/*REXX program computes the factorial of a non-negative integer, and */
/* automatically adjusts the number of digits to accommodate the answer.*/
/*This version allows for faster multiplying of #s (no trailing zeros).*/
numeric digits 100 /*start with 100 digits. */
numeric form /*indicates we want scientric form*/
parse arg n .; if n=='' then n=0 /*get argument from command line. */
/*════════════════════════════════════where the rubber meets the road. */
!=1 /*define factorial product so far.*/
do j=2 to n /*compute factorial the hard way. */
o!=! /*save old ! in case of overflow. */
!=!*j /*multiple the factorial with J, */
/* and strip all trailing zeroes. */
if pos('E',!)\==0 then do /*is ! in exponential notation? */
d=digits() /*D is current digs*/
numeric digits d+d%10 /*add ten percent. */
!=o!*j /*recalculate for the lost digit. */
end
!=strip(!,'tail-end',0) /*kill some electrons, strip 0's. */
end /*(above) only 1st letter is used.*/
/*let's perform some housekeeping.*/
if pos('E',!)\==0 then !=strip(!/1,"T",0) /*! in exponential notation?*/
v=5; tz=0
do while v<=n /*calculate # of trailing zeroes. */
tz=tz+n%v
v=v*5
end /*while v≤n*/
!=! || copies(0,tz) /*add some water to rehydrate !. */
/*REXX program computes the factorial of a number, striping trailing 0's*/
numeric digits 200 /*start with two hundred digits. */
parse arg N .; if N=='' then N=0 /*get argument from command line.*/
!=1 /*define factorial produce so far*/
/*═══════════════════════════════════════where the rubber meets the road*/
do j=2 to N /*compute factorial the hard way.*/
old!=! /*save old ! in case of overflow.*/
!=!*j /*multiple old factorial with J.*/
if pos('E',!)\==0 then do /*is ! in exponential notation?*/
d=digits() /*D temporarly stores # digits.*/
numeric digits d+d%10 /*add 10% do digits.*/
!=old!*j /*recalculate for the lost digits*/
end /*IFF ≡ if and only if. [↓] */
if right(!,1)==0 then !=strip(!,,0) /*strip trailing zeroes IFF the*/
end /*j*/ /* [↑] right-most digit is zero.*/
z=0 /*the number of trailing zeroes. */
do v=5 by 0 while v<=N /*calculate # of trailing zeroes.*/
z=z+N%v /*bump Z if multiple power of 5. */
v=v*5 /*calculate the next power of 5. */
end /*while v≤N*/ /* [↑] advance V by ourselves.*/
/*══════════════════════════════════════════════════════════════════════*/
say n'! is ['length(!) "digits]:" /*display # of digits in factorial*/
say /*add some whitespace to output, */
say ! /* ... and display the ! product.*/
!=! || copies(0,z) /*add water to rehydrate the !. */
if z==0 then z='no' /*use gooder English for message.*/
say N'! is ['length(!) " digits with " z ' trailing zeroes]:'
say /*display blank line (whitespace)*/
say ! /* ··· and display the ! product.*/
/*stick a fork in it, we're done.*/

View file

@ -1,5 +1,4 @@
rascal>factorial_iter(10)
int: 3628800
rascal>factorial_iter2(10)
int: 3628800
public int factorial_rec(int n){
if(n>1) return n*factorial_rec(n-1);
else return 1;
}

View file

@ -0,0 +1,6 @@
function f=factoriter(n)
f=1
for i=2:n
f=f*i
end
endfunction

View file

@ -0,0 +1,5 @@
function f=factorrec(n)
if n==0 then f=1
else f=n*factorrec(n-1)
end
endfunction

View file

@ -0,0 +1,3 @@
10→N
N! ---> 362880
prod(seq(I,I,1,N)) ---> 362880