Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

One Line Factorial

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)

Name: Anonymous 2009-06-15 19:04

slow factorial is slow...
try this one:
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)


or this one:
product([], 1).
product([H|T], X) :- product(T, Y), X is H * Y.

factorial(N, R) :- numlist(2, N, Ns), product(Ns, R).

Name: Anonymous 2009-06-15 19:26

>>8
see >>7

also, : fact ( n -- m ) 1 + 1 tail-slice product ;

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List