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

Pages: 1-4041-

Elegant as fuck

Name: Anonymous 2011-05-15 20:42

def fib(times):
    a, b = 0,1
    for _ in range(times):
        a, b = b, a+b
    return a


I think I just creamed my pants.

Name: Anonymous 2011-05-15 20:45

one word: THE FORCED INDENTATION OF CODE

Name: Anonymous 2011-05-15 21:18

a, b = b, a+b
Wait, how exactly does this resolve to the Fibonacci series?

Name: Anonymous 2011-05-15 21:29

>>3
fib: ; calculates (ECX-1)th fibonacci number in EAX
 mov eax 1 | cdq
 :fibloop
 add eax edx | xchg eax edx
 loop :fibloop
 ret

Name: Java Suit 2011-05-15 21:40

JAVA

Name: Anonymous 2011-05-15 21:43

>>1
SLOW AS FUCK

(define fib
    (let* ((√5 (sqrt 5))
           (φ (/ (add1 √5) 2)))
      (lambda (n)
        (round (/ (expt φ n) √5)))))

Name: Anonymous 2011-05-15 21:47

So it's functionally similar to a = b + (b = a)?

Name: Anonymous 2011-05-15 22:08

>>4
fib:
:fibloop

There's something wrong with you're assembler.

Name: Anonymous 2011-05-15 22:36

>>8
fib: ; calculates (ECX-1)th fibonacci number in EAX
mov eax 1 | cdq
:fibloop
add eax edx | xchg eax edx
loop:fibloop
ret

Name: Anonymous 2011-05-15 22:42

>>9
fib:
:fibloop

The're's something's wrong with you're assembler.

Name: Anonymous 2011-05-16 1:06

U MENA fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

Name: Anonymous 2011-05-16 1:19

you mena


def fib n
  a, b = 0, 1
  n.times {a, b = b, a + b}
  return a
end

?

Name: Anonymous 2011-05-16 1:36

How's this?

int fib (int a, int b, int n)
{
    if (n) return fib (b, a+b, n-1);
    else return a;
}

and you call it like this:

fib (1, 1, times)

All in glorious C.

Name: Anonymous 2011-05-16 2:01

>>13
Please give me a status report on your anus.

Name: Anonymous 2011-05-16 2:20


(define (fib n)
  (fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
  (cond ((= count 0) b)
        ((even? count)
         (fib-iter a
                   b
                   (+ (* p p) (* q q))
                   (+ (* q q) (* 2 q p))
                   (/ count 2)))
        (else (fib-iter (+ (* b q) (* a q) (* a p))
                        (+ (* b p) (* a q))
                        p
                        q
                        (- count 1)))))

SICP QUALITY

Name: Anonymous 2011-05-16 3:45

>>13
fib(100);

Name: Anonymous 2011-05-16 5:32

check out my Algol DSL

var fibs = -> n (
    var rec;
    rec = -> a b i (
        if !i then
            a;
        else
            rec(b,a+b,i-1);
        end;
    );
    rec 0 1 n;
);

print fibs 10;

Name: Anonymous 2011-05-16 5:45

>>14
Welcome to DrJava interpreter console
anus.isEmpty()
true

Name: Anonymous 2011-05-16 5:45

>>14
Welcome to DrJava interpreter console
> anus.isEmpty()
true
>

dicks

Name: Anonymous 2011-05-16 7:49

>>17
U MENA PERL WITHOUT SIGILS

Name: Anonymous 2011-05-16 11:07

Name: Anonymous 2011-05-16 14:57

>>13
fib(5, 5, 8);
OH WHOOPS LOOKS LIKE THATS NOT FIBS NOW IS IT?


def fib(n, a=0, b=1):
  if n:
    return fib(n-1, b, a+b)
  else:
    return a



print fib(100)

Name: Anonymous 2011-05-16 15:25

>>21
ARE YOU FUCKING RETARDED >>13 IS JUST A TAIL-RECURSIVE SOLUTION WITHOUT A SUGARY WRAPPER FUNCTION AND IT SAYS IT RIGHT THERE IN >>13 HOW YOU CALL IT.

Name: Anonymous 2011-05-16 16:25

>>23
AND IT SAYS IT RIGHT THERE IN >>13 HOW YOU CALL IT.
It should be fib(0, 1, times).

Name: Anonymous 2011-05-16 16:53

>>23
Do any (popular) C compilers even optimize tail calls?

Name: Anonymous 2011-05-16 17:19

>>1
sub fibs()
{
  my ($a, $b) = (0, 1);
  for (0 .. shift) {
    ($a, $b) = ($b, $a+$b);
  }
  return $a
}

Name: Anonymous 2011-05-16 17:35

>>25
Yes. I'm led to believe GCC does it at some optimisation level higher than 0.

Name: Anonymous 2011-05-16 17:49

Have you read your ``Proper Tail Recursion in C'' today?

http://www.complang.tuwien.ac.at/schani/diplarb.ps

Name: Anonymous 2011-05-16 18:24

>>25
Only when the tailcalled procedure's signature is compatible with the caller's.

Name: Anonymous 2011-05-16 18:51

proper anus recursion as fuck

Name: Anonymous 2011-05-16 18:59

>>28
It seems expensive as fuck on x86.

Name: Anonymous 2011-05-16 19:20

>>31
Uh? It's just turning a tail call into a goto. Though I haven't read that thing in >>28

Name: Anonymous 2011-05-16 20:01

>>32
You also have to rearrange the stack. On x86, the return address is stored on the stack, so it's one more thing to worry about while rearranging.
No problem if you're using a calling convention that uses registers for the return value and, possibly, the arguments.

Name: Anonymous 2011-05-16 21:03

fib = lambda n : return n + fib(n-1) if n and n - 1 else 1

Name: Anonymous 2011-05-16 21:56

fib(10);
fib is in the stdlib. I win.

Name: Anonymous 2011-05-16 22:25

>>1-5,7-35
try fib(1000000000).
too bad your fib is slow as fuck.

>>6
try fib(1000000000).
too bad your fib is inaccurate as fuck.

Name: Anonymous 2011-05-16 22:47

>>36
too bad your fib is inaccurate as fuck.
(define fib           
  (let* ((√5 (inexact->exact (sqrt 5)))
         (φ (/ (add1 √5) 2)))
    (lambda (n)
      (/ (expt φ n) √5))))

Name: Anonymous 2011-05-16 23:07

>>36
After fib 1000000 and higher, it'll be getting quite slow( takes one minute to calculate 10^6 fibs here ). This would be true even in an implementation which uses fixed size bignums and is written in assembly (the difference in the generated assembly for the loop between the C, Common Lisp and x86 asm version isn't too big here to actually make a big enough difference). >>6's solution might be faster and I plan on trying it, but it is indeed inaccurate as irrational numbers are not computable with infinite precision (if real numbers can even exist consistently as anything but ideal mathematical objects is a mystery to me), but the result could be accurate enough given enough precision. I don't feel like waiting hours for fib 10^9 to finish computing using the accurate algorithm, so if we want to test accuracy, mind giving me a hash of the result or better yet the result itself?

Name: Anonymous 2011-05-16 23:13

>>36
Prelude> let fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Prelude> fibs(1000000000)

<interactive>:1:0:
    Couldn't match expected type `t1 -> t'
           against inferred type `[Integer]'
    In the expression: fibs (1000000000)
    In the definition of `it': it = fibs (1000000000)

Name: Anonymous 2011-05-17 0:12

First, for the second command, you would use fibs !! 1000000000, as fibs is a list, not a function.
Second, the space requirements will literally murder your computer, as the system allocates a billion bignums.

Name: Anonymous 2011-05-17 0:16

>>40
First, for the second command, you would use fibs !! 1000000000, as fibs is a list, not a function.
No shit, I was joking. Not to mention that you shouldn't even use parentheses for function calls in Haskell.

Name: Anonymous 2011-05-17 1:26

>>37
(                    
  (     ((   (               (      )))
         (  (  (       )  )))
    (       ( )
      (  (        )   ))))

Name: Anonymous 2011-05-17 1:32

>>42
      (     ):
    , = ,
              (     ):
        , = , +
         

Name: Anonymous 2011-05-17 1:47

>>43
Much better.

Name: Anonymous 2011-05-17 2:24


#include <stdio.h>

#define NMAX 10001

void fib (unsigned int times)
{
    unsigned char a[NMAX] = {0}, b[NMAX] = {0}, temp[NMAX] = {0};
    unsigned int i, t = 0;
    b[0] = 1; b[1] = 0; a[0] = a[1] = 1;
    while (times-- - 1)
    {
        for (i = 0; i < a[0]+1; i++)
            temp[i] = a[i];
        for (i = 1; i <= a[0] || i <= b[0] || t; i++, t /= 10)
            a[i] = (t += a[i] + b[i]) % 10;
        a[0] = i-1;
        for (i = 0; i < a[0]+1; i++)
            b[i] = temp[i];
    }
    for (i = a[0]; i > 0; i--)
        printf ("%d", a[i]);
}

int main (void)
{
    fib (1000);
    return 0;
}


There, now it works with bignums, not very elegant though. (I'm the C guy from >>13)

Name: Anonymous 2011-05-17 2:45

>>45
fib(18446744073709551617)

Name: Anonymous 2011-05-17 8:14

>>45
#        <     . >

#                

         (                  )
{
                   [    ] = { },  [    ] = { },     [    ] = { };
                  ,   =  ;
     [ ] =  ;  [ ] =  ;  [ ] =  [ ] =  ;
          (     -- -  )
    {
            (  =  ;   <  [ ]+ ;  ++)
                [ ] =  [ ];
            (  =  ;   <=  [ ] ||   <=  [ ] ||  ;  ++,   /=   )
             [ ] = (  +=  [ ] +  [ ]) %   ;
         [ ] =  - ;
            (  =  ;   <  [ ]+ ;  ++)
             [ ] =     [ ];
    }
        (  =  [ ];   >  ;  --)
               ("% ",  [ ]);
}

         (    )
{
        (    );
            ;
}

Name: Anonymous 2011-05-17 8:35

>>47
still better than lisp

Name: Anonymous 2011-05-17 9:13

>>48
Lol no. At least lisp is consistent with its parenthesis.

Name: Anonymous 2011-05-17 9:17

>>49
#        (     . )

#               

         (                  )
(
                   (    ) = ( ),  (    ) = ( ),     (    ) = ( );
                  ,   =  ;
     ( ) =  ;  ( ) =  ;  ( ) =  ( ) =  ;
          (     -- -  )
    (
            (  =  ;   <  ( )+ ;  ++)
                ( ) =  ( );
            (  =  ;   <=  ( ) ||   <=  ( ) ||  ;  ++,   /=   )
             ( ) = (  +=  ( ) +  ( )) %   ;
         ( ) =  - ;
            (  =  ;   <  ( )+ ;  ++)
             ( ) =     ( );
    )
        (  =  ( );   >  ;  --)
               ("% ",  ( ));
)

         (    )
(
        (    );
            ;
)

Name: Anonymous 2011-05-17 9:37

>>50
Invalid C code.

Name: Anonymous 2011-05-17 9:46

>>51
Valid gay poster.

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