Name: Anonymous 2009-06-15 15:56
for(int a1=1,a2=1,i=scanf_s("%d",&i)+i-1;(i--)>2; a1=(a2+=a1)-a1,(i==2)?printf_s("Result=%d\n",a2):NULL)
fact x = if x == 0 then 1 else x * fact(x-1)fact x = if x == 0 then 1 else x * fact(x-1);main=readLn >>= print.fact
f@(_:fs) = 0 : 1 : zipWith (+) f fs
from IntroductoryExercises import Fibonacci;
main = fib;
#include <stdio.h>
#include <stdlib.h>
#define grunnurr(m) fprintf(stderr,"grunnur: %s\n",#m)
#define grun(c) exit(c)
#define nnnur(s) atoi(s)+1
#define NUR main
#define nn =
#define n *
#define nnur 1
#define GRUNNUR return 0;
#define grunn while
#define GRU(f,d) printf(f,d)
#define nnn "%d\n"
#define grunnur NNUR[nnur]
#define _(a,b) b=a
#define ur
typedef char gruun;
typedef int GRUN;
GRUN NUR(GRUN NURR,gruun n n NNUR){GRUN nur,grun nn nnur;grunn(nnur&&!grunnur){grunnurr(GRUNNuR);grun(nur);}grun nn nnnur(grunnur);_(grun,nur);grun nn nnur;grunn(--nur)grun nn ur grun n nur;_(grun,nur);GRU(nnn,nur);GRUNNUR;}
primes = 2 : 3 : 5 : 7 : [k + r | k <- [0, 30..], r <- [11, 13, 17, 19, 23, 29, 31, 37], primeTest (k + r)]
where primeTest n = all ((0 /=) . mod n) . takeWhile ((n >=) . join (*)) $ drop 3 primes
bitCount 0 = 0
bitCount n = bitCount (shiftR n 1) + n .&. 1
swing n | n < 33 = genericIndex smallOddSwing n
| True = product pList
where smallOddSwing = [1, 1, 1, 3, 3, 15, 5, 35, 35, 315, 63, 693, 231, 3003, 429, 6435, 6435, 109395, 12155, 230945, 46189, 969969, 88179, 2028117, 676039, 16900975, 1300075, 35102025, 5014575, 145422675, 9694845, 300540195, 300540195]
pListA q p prime = let x = div q prime in case x of
0 -> case p of
1 -> []
_ -> [p]
_ -> pListA x (p * prime ^ (mod x 2)) prime
pListB = (filter ((1==) . flip (.&.) 1 . div n) . takeWhile (<= div n 3) $ dropWhile ((<= n) . join (*)) primes)
pListC = takeWhile (<= n) $ dropWhile (<= shiftR n 1) primes
pList = (concatMap (pListA n 1) . takeWhile ((n >=) . join (*)) $ tail primes) ++ pListB ++ pListC
recFactorial n | n < 2 = 1
| True = join (*) (recFactorial $ div n 2) * swing n
factorial :: Int -> Integer
factorial n | n < 20 = product [2..fromIntegral n]
| True = shiftL (recFactorial $ fromIntegral n) (n - bitCount n)product([], 1).
product([H|T], X) :- product(T, Y), X is H * Y.
factorial(N, R) :- numlist(2, N, Ns), product(Ns, R).
% ghci
GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude>
Prelude>
Prelude> fact x = product [1..x]
<interactive>:1:7: parse error on input `='
Prelude> let fact x = product [1..x]
Prelude> fact 7
* Exception: Stack overflow
fact: mov rax, rdi
dec rdi
.loop: mul rdi
dec rdi
jnz .loop
ret
public final long factorial(final int n) {
return (n == 1 ? 1 : n * factorial(n - 1));
}public final long factorial(final int n)int to long in that multiplication?
from math import sqrt
from itertools import takewhile, dropwhile
def eratosthenes():
D = {}
q = 2
while 1:
if q not in D:
yield q
D[q*q] = [q]
else:
for p in D[q]:
D.setdefault(p+q,[]).append(p)
del D[q]
q += 1
def bitcount(n):
r = 0;
while n > 0:
r += n & 1
n >>=1
return r
def take(n, g):
for i in range(n): yield g.next()
def swing(n):
primes = list(takewhile(lambda x: x <= n, eratosthenes()))
smalloddswing = [1,1,1,3,3,15,5,35,35,315,63,693,231,3003,429,6435,6435,109395,12155,230945,46189,969969,88179,2028117,676039,16900975,1300075,35102025,5014575,145422675,9694845,300540195,300540195]
if n < 33: return smalloddswing[n]
primelist = []
rootn = long(sqrt(n))
primesa = takewhile(lambda x: x <= rootn, dropwhile(lambda x: x < 3, primes))
primesb = takewhile(lambda x: x <= n // 3, dropwhile(lambda x: x <= rootn, primes))
for prime in primesa:
q = n // prime
p = 1
while q > 0:
if q & 1 == 1: p *= prime
q //= prime
if p > 1: primelist.append(p)
return reduce(lambda x, y: x * y, list(takewhile(lambda x: x <= n, dropwhile(lambda x: x <= n // 2, primes))) + primelist + filter(lambda x: n // x & 1 == 1, primesb), 1)
def recfactorial(n):
if n < 2: return 1
return recfactorial(n // 2) ** 2 * swing(n)
def factorial(n):
if n < 20: return reduce(lambda x, y: x * y, range(2,n + 1), 1)
return recfactorial(n) << (n - bitcount(n))