code_junkie

Code Golf: Factorials

Nov 11th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. ?!
  2.  
  3. ,>++++++[<-------->-]<[->+>+<<]>>->>+<<<[>[<[->[-<<+>>>+<]>[-<+>]<<]<[->+<]>>-]<.>>>-]>>>[.-]
  4.  
  5. ,
  6. >++++++ Put 6 in next cell
  7. [<-------->-] Subtract 8 six times to subtract 48
  8. <
  9. [->+>+<<] Move (0) to (1) and (2)
  10. >>- Decrement one from (2) as we want to multiply n * n minus 1
  11. >>+ Store 1 in (4) to allow distinguishing 0 separately
  12. <<< Go to (1)
  13. [ A makeshift if($_ != 0)
  14. >[ While (2)
  15. <[ While(1)
  16. - Subtract one from (1) for multiplication by repeated addition
  17. >[-<<+>>>+<] Add (2) to (0) and (3)
  18. >[-<+>] Move data from (3) to (2)
  19. <<
  20. ]
  21. <[->+<] Copy (0) to (1) for next round of multiplication
  22. >>- Decrement (2) to go to n minus 2 and so on
  23. ]
  24. <.>>>- Print output from (1) and make (4) = 0 to stop the if
  25. ]
  26. >>>[.-] If we're at (4) (and it is nonzero) we have a 0 as input; so print 1 and stop;
  27.  
  28. [->+>+<<]>>->>+<<<[>[<[->[-<<+>>>+<]>[-<+>]<<]<[->+<]>>-]<.>>>-]>>>[.-]
  29.  
  30. n->product[1..n]
  31.  
  32. fac n = product [1..n]
  33.  
  34. fac n = foldr (*) 1 [1..n]
  35.  
  36. #include <iostream>
  37.  
  38. template <int N>
  39. struct Factorial
  40. {
  41. enum { value = N * Factorial<N - 1>::value };
  42. };
  43.  
  44. template <>
  45. struct Factorial<0>
  46. {
  47. enum { value = 1 };
  48. };
  49.  
  50. int main()
  51. {
  52. std::cout << "4!=" << Factorial<4>::value << std::endl;
  53. }
  54.  
  55. #AT&T syntax
  56. mov %eax, %ebx
  57. again:
  58. dec %ebx
  59. .byte 0x74, 4 #jz (short) done
  60. mul %ebx
  61. .byte 0xEB, -7 #jmp (short) again
  62. done:
  63.  
  64. set r=1
  65. for /l %%i in (1,1,%1) do call set/a r=%%r%%*%%i
  66. echo %r%
  67.  
  68. // uint32_t factorial(uint32_t n, uint32_t *result, uint32_t length);
  69. //
  70. // stores n! in the buffer result as a little-endian bignum. length is
  71. // size of the buffer in (32-bit) words. It is the caller's responsibility
  72. // to allocate and free the result buffer. If the buffer is not large
  73. // enough to contain n!, 0 is returned. On successful exit, the return
  74. // value is the number of (32-bit) words of the buffer that were used to
  75. // store the result.
  76.  
  77. _factorial:
  78. push {r4-r7}
  79. tst r2, r2
  80. beq Lerror
  81. movs r3, #1
  82. str r3, [r1]
  83. tst r0, r0
  84. beq Ldone
  85. Lloop:
  86. eors r6, r6
  87. movs r7, r3
  88. Lmultiply:
  89. movs r5, r6
  90. eors r6, r6
  91. ldr r4, [r1]
  92. umlal r5, r6, r0, r4
  93. str r5, [r1], #4
  94. subs r7, $1
  95. bne Lmultiply
  96. tst r6, r6
  97. beq LnoOverflow
  98. adds r3, $1
  99. cmp r3, r2
  100. bhi Lerror
  101. str r6, [r1], #4
  102. LnoOverflow:
  103. sub r1, r1, r3, lsl #2
  104. subs r0, $1
  105. bne Lloop
  106. Ldone:
  107. mov r0, r3
  108. Lexit:
  109. pop {r4-r7}
  110. bx lr
  111. Lerror:
  112. eors r0, r0
  113. b Lexit
  114.  
  115. int f(int v){return v<2?1:v*f(v-1);}
  116.  
  117. (Condition) ? (Return this if true) : (Return this if false)
  118.  
  119. if (v<2)
  120. {
  121. return 1;
  122. }
  123. else
  124. {
  125. return v*f(v-1);
  126. }
  127.  
  128. f=lambda n:n<2and 1or n*f(n-1)
  129.  
  130. sub f($n){[*]1..$n}
  131.  
  132. sub f{[*]1..$^n}
  133.  
  134. sub postfix:<!>($n){[*]1..$n}
  135.  
  136. {[*]1..$^n}
  137.  
  138. say {[*]1..$^n}(5) # 120
  139.  
  140. def f i;i<2?1:i*f(i-1);end
  141.  
  142. ,{1+}%{*}*
  143.  
  144. def f(n):return n and n*f(n-1)or 1
  145.  
  146. int f(int n){return n?n*f(n-1):1;}
  147.  
  148. sub f{$_[0]?$_[0]*f($_[0]-1):1;}
  149.  
  150. dc -e'?d[1-dsa*lad1<b]dsbxszp' <<<1000
  151.  
  152. dc -e'?[q]sQ[d1=Qd1-lFx*]dsFxp' <<<1000
  153.  
  154. int f(int n){return n>1?f(n-1)*n:1;}
  155.  
  156. let f n = [1..n] |> Seq.fold ( * ) 1
  157.  
  158. seq $n|paste -sd*|bc
  159.  
  160. def f(n:Int)=(1/:(1 to n))(_*_)
  161.  
  162. f=lambda x:+(x<2)or x*f(x-1)
  163.  
  164. fact=:!
  165.  
  166. fact=:*/@:(1+i.)
  167.  
  168. fact = foldr1 (*) . n -> [1..n]
  169.  
  170. If[#<=#2,#,#0[#,2#2]#0[#-#2,2#2]]&[#,1]&
  171.  
  172. If[#<1,1,#1#0[#-1]]&[#,0]&
  173.  
  174. Times@@Range@#&
  175.  
  176. Func<int,int>f=null;f=v=>v<2?1:v*f(v-1);
  177.  
  178. def f(n):return (1 if n<2 else n*f(n-1))
  179.  
  180. fun f 0=1|f n=n*f(n-1)
  181.  
  182. let rec f n = if n=0 then 1 else n*f(n-1);;
  183.  
  184. def f(n): return reduce(lambda x,y: x*y,range(1,n+1))
  185.  
  186. F(n){return n>1?n*F(n-1):1;}
  187.  
  188. decimal f(int v) { return v > 1 ? v * f(v - 1) : 1; }
Add Comment
Please, Sign In to add comment