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 16:23

fact x = if x == 0 then 1 else x * fact(x-1)

Alternatively,
fact x = if x == 0 then 1 else x * fact(x-1);main=readLn >>= print.fact

Name: Anonymous 2009-06-15 16:28

Can your C do this?
f@(_:fs) = 0 : 1 : zipWith (+) f fs

Name: Anonymous 2009-06-15 16:36

from IntroductoryExercises import Fibonacci;
    main = fib;

Name: Anonymous 2009-06-15 17:32

: fact 1+ 1 tuck +do i * loop ;

Name: Anonymous 2009-06-15 17:58


#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;}

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:10

>>2-4
facts = scanl1 (*) [1..]

Name: Anonymous 2009-06-15 19:23

fact x = product [1..x]

Name: Anonymous 2009-06-15 19:26

>>8
see >>7

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

Name: Anonymous 2009-06-16 18:05

>>5,10
I also want to know what is this one.

Name: Anonymous 2009-06-16 18:16

>>11
>>10 is factor.
>>5 is forth, but with a horribly ineffiicient algorithm.

Name: Anonymous 2009-06-16 18:40

>>12
Ah thanks

Name: Anonymous 2009-06-16 18:51

>>9
I'm having trouble running it, what's going on here?

% 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

Name: Anonymous 2009-06-16 18:55

>>14
FOR FUCK'S SAKE WON'T YOU FAGGOTS LEARN IT'S THREE THREE ASTERISKS GODDAMN

Name: Anonymous 2009-06-16 22:53

Name: Anonymous 2009-06-17 0:23

NIGGER!

Name: Anonymous 2009-06-17 1:52

>>16
I'm not sure someone who doesn't know the difference between cities and metropolitan areas deserves to be taken seriously.

Name: Anonymous 2009-06-17 4:52

>>14
IHBT

Name: Anonymous 2009-06-17 5:13

fact:   mov rax, rdi
        dec rdi
.loop:  mul rdi
        dec rdi
        jnz .loop
        ret

Name: Anonymous 2009-06-17 8:49

>>20
lol @ inefficient algorithm
see >>7,10 for better ones.
especially the prime-swing one in >>7. implement that (with a better prime sieve) in assembly and unless you do something incredibly stupid it should be faster than GMP's factorial function.

Name: Anonymous 2009-06-17 9:34

>>21
>>7's algorithm uses my method for n < 20, and for n > 20 mine doesn't work anyway, so I don't see how you can call it inefficient.

Name: Anonymous 2009-06-17 9:37

>>22
(define factorial (λ(n) 120))
It works for n=5 super efficiently. The edge case where n is not 5 may not behave correctly.

Name: Anonymous 2009-06-17 9:38

>>22
But what about n = 20 ?

Name: Anonymous 2009-06-17 10:19

>>24
It make computer explode.

Name: Anonymous 2009-06-17 10:34

>>24
For n = 20, he uses the complex method and mine works fine.

Name: Anonymous 2009-06-17 18:15

>>22,26
it does work, and is a lot faster than your algorithm. how long does yours take for n = 100000?

Name: Anonymous 2009-06-18 6:13


public final long factorial(final int n) {
 return (n == 1 ? 1 : n * factorial(n - 1));
}


AM I DOING IT RIGHT?

Name: Anonymous 2009-06-18 6:31

>>28
No actually, your factorial not only doesn't output the correct answer on an unspecified number of legitimate inputs, but in fact, doesn't halt at all on a subset of said inputs. I suggest you rethink some dubious design decisions.

Name: Anonymous 2009-06-18 8:42

>>29
droves of diverse dubious design decisions are distributed throughout this thread.

Name: Anonymous 2009-06-18 10:53

>>29
By all means, be free to fix my code.

Name: Anonymous 2009-06-18 11:02

>>31
(define factorial-of-zero 1)

Name: Anonymous 2009-06-18 12:40

public final long factorial(final int n)
Java makes me lol.
Also, why are you implicitly typecasting int to long in that multiplication?

Name: Anonymous 2009-06-18 19:32

Also, why are you implicitly typecasting int to long in that multiplication?
you keep using that word... i do not think it means what you think it means.
http://www.merriam-webster.com/dictionary/typecast

Name: Anonymous 2009-06-19 1:48

def fact(x): return (1 if x==0 else x * fact(x-1))

Name: Anonymous 2009-06-19 2:16

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

Name: tehmeh 2009-06-19 14:31

>>36
cool sieve bro

def factorial(n):
    x=1
    while (n>0):
        x=x*n
        n=n-1
    return x

Name: Anonymous 2009-06-19 14:44

>>37
cool slow as fuck bro

you might as well be using ruby on fails

Name: tehmeh 2009-06-19 14:56

>>38
factorial(1000)

0.017403 seconds

Name: Anonymous 2009-06-19 15:05

>>39
0.007s in compiled Haskell, 0.01s interpreted. Who's SLOW AS FUCK now?

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