Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

The Six Most Common Species Of Code

[Nov 17, 2013] Willa's World The Six Most Common Species Of Code

Michael Mandrus said...

A CS 101 student would never write a recursive function.
November 9, 2013 at 9:16 PM  
 
 

saurabh singh said...

@Michael Depending on the teacher taking the CS101. I am pretty much sure every one in my batch would have written a recursive function over an iterative solution
 
November 10, 2013 at 12:00 AM  

Animesh said...

@Saurabh, Michael
Recursive function for Fibonacci will be very inefficient. Its running time will grow exponentially with X. :) Watch this video : http://www.youtube.com/watch?v=GM9sA5PtznY
November 10, 2013 at 1:12 AM  
 

Abhishek said...

I hope someone noticed the point of this post and which in my view is that the more you know things, the more constrained your view becomes, instead of thinking in a straightforward manner, we think of all the ways something could go wrong and more often than not, it holds us back from doing anything.
November 10, 2013 at 2:43 AM  
 

Rooney said...

@Animesh

Recursion is not used without memoization. With it, it's a linear algorithm.
November 10, 2013 at 4:39 AM  
 

RockNes said...

Math guy's program will go in an infinite loop if b is a non integer number :/
November 10, 2013 at 7:35 AM  
 

meenu iyyer said...

This is sooo true and really funny.. i have been there and done that for all the different roles [Excpet the cat ofcourse ;)] .... code written at a large comany is the best.. ROFL!
November 10, 2013 at 7:39 AM  
 

KARTIK SACHAN said...

why u guys senti by looking at this ?
this if for fun only :) :)
November 10, 2013 at 8:43 AM  
 

thanbo said...

The Math PhD's closed-form answer is less efficient than the basic iterative/recursive solution. It requres 2n (or for the rounding version, n) multiplications to do the exponentiation, while the brute-force solution requires n additions, which on most processors are faster than multiplications.
November 10, 2013 at 9:18 AM  
 

virgincoder said...

LOL ! Funny ! I laughed so much when I saw the "Code Written At a Large Company" part ! LOL
November 10, 2013 at 9:48 AM  
 

Subhrajit said...

so no one gives a sh!t about the computational complexity. All the recursive implementations have exponential complexity. And I seriously have no clue what the author tried to prove with the totally gibberish large company or math PhD code.

How about the following:
int fibonacci(int x){
if (x <= 2)
return 1;
else {
int sum = 1, oldsum = 1, tmpsum;
for (int a = 3; a <= x; a++) {
tmpsum = sum;
sum = sum + oldsum;
oldsum = tmpsum;
}
return sum;
}
}

It has linear complexity.
November 10, 2013 at 11:51 AM  
 

Unknown said...

A database specialist would write

SELECT Value FROM dbo.Fibonacci WHERE n = @n;
 
November 10, 2013 at 12:36 PM  
 

Herman Saksono said...

I would be surprised if a large company has a fibonacci method that runs on O(2^n) time.
November 10, 2013 at 3:48 PM  
 
  •   
  • Gabrielle said...

    When I took my bachelor degree, I used "cat" species code for my homework. The code worked, but guess what? Got 0 because my teacher didn't understand any sh*t I wrote :))
    November 10, 2013 at 9:31 PM  
     
  •  
  • Rahul Thakur said...

    I get the humour, but for those suggesting improvements, here's a simpler one -

    void fibonacci(int number_of_terms){
    int T1=0, T2=1;
    do{
    T1 = T1 + T2;
    T2 = T1 - T2;
    printf("%d\n", T2);
    number_of_terms--;
    } while(number_of_terms > 0);
    }

    This is in C btw, and here's a compiled version - http://ideone.com/gs0Duz
    November 10, 2013 at 10:02 PM  
  •   
  • aMIT sHaKyA said...

    Here we go, complete imagination of author went to a toss. And post has become dead ground for recursive algo complexity discussion. Screw you coding wannabes.

    Too good post. Don't do CS graffiti here.
    November 10, 2013 at 10:50 PM  
  •   
  •  
  • HuzursuZ said...

    i do as

    f(n)=( (1+sqrt(5))^n - (1-sqrt(5))^n ) / (sqrt(5)*2^n)

    so what i become ?
    November 11, 2013 at 1:35 AM  
  •  
  • Paul K said...

    Had a good laugh :D

    Having worked at 5 *very* different companies in 5 years, I can testify that there is a lot of truth to this!

    (Except perhaps the one with the cat)
    November 11, 2013 at 5:56 AM  
  •  
  • Mads said...

    And code written by a student, that paid attention during algoritms, knows how to google and did remember to trust only reliable sources of information...

    http://introcs.cs.princeton.edu/java/23recursion/Fibonacci.java.html
    November 11, 2013 at 6:21 AM  
     

    ac said...

    is missing the kernel guy code:

    int fib(int n) {
    if (n < 0) {
    #ifdef HAVE_ERRNO
    errno = EDOM;
    #endif
    return -1;
    }

    return n == 0
    ? 0
    : (n == 1
    ? 1
    : (n == 2
    ? 2
    : fib(n - 2) * fib(n-1)
    )
    );
    }
     

    sarath chandra said...

    Lol so true, code written at large company does look like that, (why? :()
    November 11, 2013 at 6:42 AM  
     

    Daniel Dwire said...

    This comment has been removed by the author.
    November 11, 2013 at 6:52 AM  
     

    tsndiffopera said...

    Phew! Then who'd write a O(lg(n)) algorithm using matrix exponentiation ? Only me? :P

    [{1 1},{1 0}]^n = [{F_(n+1) F_n},{F_n F_(n-1)}]

    Also, x^n = x^(n/2)*x^(n/2)

    which has O(lg(n)) ;)
    November 11, 2013 at 7:08 AM  
     

    joe random said...

    Just to be pedantic for my CS/math bros:
    The CS101 code doesn't need recursion or memoization, and that would occur to most students, since that's how people do it by hand: they take the last two numbers, add them together, and get a new number. Then they can forget the oldest number. A simple for loop takes care of that. Admittedly, this is explicit memoization.

    But worse, the code by a "math phd" isn't any faster than that, and is inexact if there is rounding error, unless it uses an overcomplicated math framework that handles sqrts symbolically.

    Still, if you change the (math phd?) exponentiation function to do successive squaring, you get the best running time so far, O(log n). A CS101 student could even work out how to do it without a heavyweight math library, since all of the intermediate computations are on numbers of the form (a+b*sqrt(5))/2^n where a,b, and n are integers. So you only need integer arithmetic.

    There other O(log n) algorithms, such as ones exploiting the recurrences
    F_(2n-1) = (F_n)^2 + (F_(n-2))^2
    and
    F_(2n) = (2F_(n-1) + F_n)F_n

    Sincerely,
    a math phd candidate
    November 11, 2013 at 7:20 AM  

    Леха Чебара said...

    cat style looks like perl code
    November 11, 2013 at 8:08 AM  
     
     

    Haskell said...

    The Math PhD would use haskell and produce an infinite list of fibonacci results.
    November 11, 2013 at 8:38 AM  
     

    Siberiano said...

    I think math phd should write that in Lisp.

    A simple version would be, but you may expand to add other parameter forms.

    (defun fib (x) (if (< x 2) x (fib (- x 1) (- x 2))))
    November 11, 2013 at 9:02 AM  
     

    Maciek Napora said...

    My most beloved school of coding is so called 'Weimar school'. It used by Germans for writing embedded code, mainly safety critical code. It goes something like this:

    #define ONE 0U
    #define TWO 1U
    #define E_OK 0U
    #define THRE 16U
    #define HUNDRED 100U
    uint8_t UDS_tx_buff_au8[HUNDRED + ONE]

    uint8_t panic(uint16_t kondition_u16)
    {
    uint8_t temp_u8;

    /* I am evaluating kondition */
    if(kondition_u16 > THRE)
    {
    UDS_tx_buff_au8[ONE] = ONE;
    UDS_tx_buff_au8[TWO] = TWO;
    temp_u8 = HUNDRED;
    }
    else
    {
    UDS_tx_buff_au8[ONE] = ONE;
    UDS_tx_buff_au8[TWO] = ONE;
    temp_u8 = HUNDRED;
    }

    return temp_u8;
    }

    F$ck ya common sense, logical expresions folding and ROM saving.
    MISRA and QAC said so. German engineering knows that;D
    November 11, 2013 at 9:53 AM  
     

    AVichare said...

    Hmmm ... a functional programmer writing in C may write:

    return ((x == 1) || (x == 2)) ? 1 : (fibonacci (x - 1) + fibonacci (x - 2));

    arguing that: (a) tail recursion would take care of recursion costs, and (b) why bother with control flow if we only need the values.

    Reminds me of Perlis' quip: C programmers know the cost of everything and value of nothing, while Lisp programmers know the value of everything but the cost of nothing. :-)

    Thanks for a fun post.
    November 11, 2013 at 9:59 AM  
     

    Srikant Lanka said...

    Has anybody noticed that the smartest code with best practices is actually written by the cat?? Dude your cat is awesome..

    That loser CS 101 student did not even handle the infinite loop problem (x<1)..

    Soft Kitty, Warm Kitty, little ball of fur, Happy Kitty, Sleepy Kitty, purr purr, purr #respect
    November 11, 2013 at 11:28 AM  
     

    Justin Holmes said...

    A hackathon coder would use this:

    int getFibonacciNumber(int n) {
    int table[] = {-1, 1,1,2,3,5,6,13};
    if ((unsigned int)n > 13)
    return -1;
    return table[n];
    }
    November 11, 2013 at 12:44 PM  

    Milad Ekhteraei said...


    F_n = F_{n-1} + F_{n-2},\!\

    F_n = F_{n-1} + F_{n-2},\!\


    F_{n-2} = F_n - F_{n-1}

    F_{-n} = (-1)^{n+1} F_n

    F_{n}=\sum_{k=0}^{\lfloor\frac{n-1}{2}\rfloor} \tbinom {n-k-1} k

     

    Michael Wexler said...

    Code written by CS 101 student has too much indentation and looks too clean. In reality, the code would be flush against the left margin, no indents, no whitespace between operators/operands, and would probably have redundant comments on every other line (to please the prof), e.g. "//This is for the case x = 1 //This is for the case x == 2"

    Tyler Bartnick said...

    Funny because I am a CS 101 student and I did in fact write a recursive function without the help of outside resources for one of the functions needed in a project.

    Welcome to Karna said...

    Code as written by a hacker:

    public int fib(int n) { return (n > 2) ? fib(n-1)+fib(n-2):0; }

    Code as written as a seasoned: developer


    import org.apache.commons.math;
    public int fib(int n) {
    return Math.fibonacci(n);
    }
    November 11, 2013 at 10:13 PM  

    Mehrzad Karami said...

    So true, Going through this I had a flashback of all companies i have worked with in the last 15 years.
    More you know, the more constrained you are

    Meng Lin said...

    Comman, at least there will be unit tests in the code produced at a large company, lol
    November 12, 2013 at 6:19 AM  
     

    juzhax said...

    echo "bye";


    I like PHP.
     

    kasyap1125 said...

    I am going to write cat code in my company tomorrow :) :P
    November 12, 2013 at 9:35 AM  
     
     

    Simon Richard Clarkstone said...

    Code written by a type theorist. (It calculates Fibonacci numbers in the Haskell type system.)


    {-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-}
    data Zero
    data Succ n
    class Add a b c | a b -> c
    instance Add Zero b b
    instance Add a b c => Add (Succ a) b (Succ c)
    class Fibb a b | a -> b
    instance Fibb Zero Zero
    instance Fibb (Succ Zero) (Succ Zero)
    instance (Fibb a r1, Fibb (Succ a) r2, Add r1 r2 b) => Fibb (Succ (Succ a)) b


    To calculate, you need to create placeholder values with appropriate types, and ask the interpreter what type the combination of the two would have.

    *Main> let fibb = undefined :: (Fibb a b) => a -> b
    *Main> let six = undefined :: Succ (Succ (Succ (Succ (Succ (Succ Zero)))))
    *Main> :t fibb six
    fibb six
    :: Succ (Succ (Succ (Succ (Succ (Succ (Succ (Succ Zero)))))))
    November 12, 2013 at 10:04 AM  

    Jayabalan said...

    thinking ... should get CAT.

    Denis Ivin said...

    Sorry, couldn't resist... Bad Indian code http://pastie.org/8475451
    November 12, 2013 at 10:52 AM  
     

    Kevin Rogovin said...

    Just a thought: one can compute Fib(n) in O(1). There is a nice closed from for Fib(n) to derived it consider that it satisfies:

    Fib(n+2) - Fib(n+1) - Fib(n) = 0

    nice, linear and homogeneous.

    The punchline is that

    Fib(n) = c0 * b0^n + c1*b1^n

    where b0 and b1 solve for

    x^2 - x - 1 =0 [Golden ratio!]

    and c0 and c1 are so that

    co + c1 = Fib(0) = 1
    c0*b0 + b1*b1 = Fib(1) = 1

    Though, accuracy might be an issue.
    November 12, 2013 at 10:57 AM  

    Jack Kern said...

    And then there's the smart way to do it:

    https://gist.github.com/ElectricJack/7441844
    November 12, 2013 at 5:13 PM  

    Prabandham Srinidhi said...

    And this is how it is done in ruby :)

    def fibonaci(n)
    a=[0]
    (o..n).map {|x| x<=1? a[x]=x :(a[x] = a[x-1]+a[x-2])}
    puts a.inspect
    end
    end

    102524021510033218601 said...

    :D
    Who gonna write the DP code? :)
    November 12, 2013 at 9:52 PM  
     
     

    XProger said...

    return int(0.5+(pow(1.618033988749895, n) / 2.23606797749979));

    Daniel Dinnyes said...

    The real Math Ph.D. wouldn't use `one()` or `add(one(), one(), one(), one(), one())` when there is already a `zero()` defined. Rather he would write it using induction, like
    `succ(zero())`, or `succ(succ(succ(succ(succ(zero())))))`. Hope that helps ;)

    Sergio Daniel Lepore said...

    Hahahahahahaha
    November 15, 2013 at 6:18 AM