/* Factorial code golf in Picat. https://codegolf.stackexchange.com/questions/210071/the-vanilla-factorial-challenge """ The vanilla factorial challenge Task Given a non-negative integer n , evaluate the factorial n! The factorial is defined as follows: n!={1n×(n−1)!n=0n>0 Rules All default I/O methods are allowed. Standard loopholes are forbidden. Built-ins are allowed. There is no time or memory limit. Giving imprecise or incorrect results for large inputs due to the limit of the native number format is fine, as long as the underlying algorithm is correct. Specifically, it is not allowed to abuse the native number type to trivialize the challenge, which is one of the standard loopholes. This is code-golf. Shortest code in bytes wins, but feel free to participate in various esolangs (especially the ones hindered by the restrictions of the former challenge). Test cases 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 11! = 39916800 12! = 479001600 Note: We already have the old factorial challenge, but it has some restrictions on the domain, performance, and banning built-ins. As the consensus here was to create a separate challenge without those restrictions so that more esolangs can participate, here it goes. Also, we discussed whether we should close the old one as a duplicate of this, and we decided to leave it open. """ Here are some different approaches, some shorter/longer than others. The shortest one is 11 chars: l(N) = prod(1..N). (or 10 chars excluding the final period). This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> println(f=[f(N):N in 1..10]), println(g=[g(N):N in 1..10]), println(h=[h(N):N in 1..10]), println(i=[i(N):N in 1..10]), println(j=[J :N in 1..10, j(N,J)]), println(k=[k(N):N in 1..10]), println(l=[l(N):N in 1..10]), println(m=[m(N):N in 1..10]), println(n=[n(N):N in 1..10]), println(o=[o(N):N in 1..10]), println('!'=['!'(N):N in 1..10]), nl. go => true. % 24 chars f(1)=1. f(N)=N*(f(N-1)). % 22 chars g(N)=cond(N==1,1,N*g(N-1)). % 15 chars h(N)=fold(*,1,1..N). % 13 chars i(N)=factorial(N). % 29 chars '!'(1)=1. '!'(N)=N*('!'(N-1)). % 38 chars j(1,1). j(N,F):-N> 1,j(N-1,F1),F=N*F1. % 34 chars k(N)=F=>F=1,while(N>1) F:=F*N, N:=N-1 end. % 11 chars l(N)=prod(1..N). % OK, this one borders to the silly % 69 chars m(N)=F=>X=new_list(N),X::1..N,all_different(X),F #= prod(X),solve(X++[F]). % 36 chars n(N)=F=>F=1,foreach(I in 1..N)F:=F*I end. % 30 chars o(N)=prod(M.keys)=>M=new_set(1..N).