• (cs)

    More functions should disregard input values and just return 12. It would make life easier.

  • Justin (unregistered)

    No No, it's not twelve. 'i' gets modified in the loop. So, we have a function that returns the answer to life, the universe, and everything.

  • Foobar (unregistered) in reply to Justin
    Justin:
    No No, it's not twelve. 'i' gets modified in the loop. So, we have a function that returns the answer to life, the universe, and everything.
    Perfect. That's all everybody needs. I would hire him!
  • (cs) in reply to Justin
    Justin:
    No No, it's not twelve. 'i' gets modified in the loop. So, we have a function that returns the answer to life, the universe, and everything.

    Likely the submission was altered slightly to make it do just that, but it's pretty awesome nonetheless.

    Sadly, when I got out of college no one really expected much from me even though the college I went to stressed things like team work and not breaking the build. :P

  • (cs)

    Doesn't use the input parameter and doesn't reuse the variable a, except to hold the number. Course, we all were dumb at one point, but at least we know what recursion was when we graduated.

  • (cs) in reply to Justin
    Justin:
    No No, it's not twelve. 'i' gets modified in the loop. So, we have a function that returns the answer to life, the universe, and everything.

    Yes we know it is modified in the loop. That's why it is twelve and not 10.

  • Anon (unregistered) in reply to Justin
    Justin:
    No No, it's not twelve. 'i' gets modified in the loop. So, we have a function that returns the answer to life, the universe, and everything.

    Yes it is twelve. First loop i = 1. i = 1 * 2 = 2, then the i is incremented to 3. Next loop i = 2. i = 3 * 4 = 12, 12 is > 10 so the loop ends. QED

  • Anon (unregistered) in reply to Anon
    Anon:
    Justin:
    No No, it's not twelve. 'i' gets modified in the loop. So, we have a function that returns the answer to life, the universe, and everything.

    Yes it is twelve. First loop i = 1. i = 1 * 2 = 2, then the i is incremented to 3. Next loop i = 2. i = 3 * 4 = 12, 12 is > 10 so the loop ends. QED

    Whoops, that should be i = 3 on the second line

  • (cs)

    Graduates who don't know what recursion is are less common than forum users who don't know how C-style for loops work, I guess.

  • (cs)

    This is shocking. I just recently graduated, and I am shocked at that code, I especially love his manipulation of 'i' in the loop and not using the all important 'n' what so ever ..amazing!

  • (cs) in reply to Welbog
    Welbog:
    More functions should disregard input values and just return 12. It would make life easier.

    Are you saying that n! != 12 for all n?

  • Joseph (unregistered) in reply to KevB

    I just find it amazing that people who code like that can actually graduate!

  • (cs) in reply to bstorer
    bstorer:
    Welbog:
    More functions should disregard input values and just return 12. It would make life easier.
    Are you saying that n! != 12 for all n?
    In fact I'm saying n! != 12 for any n! (That last ! is for emphasis.)
  • OhNoesTheyBeStealingOurFactorials (unregistered)

    I used to interview candidates at my previous job. Often asked this question. Have to say this candidate is in the top 50 percent... :)

  • Protiuz (unregistered)

    My former boss asked a person he was interviewing what the definition of a 'class' was in reference to c++... They responded with "A class is what I attended at school to learn about c++". The didn't get hired...

  • (cs) in reply to OhNoesTheyBeStealingOurFactorials
    OhNoesTheyBeStealingOurFactorials:
    I used to interview candidates at my previous job. Often asked this question. Have to say this candidate is in the top 50 percent... :)
    I think I speak for all of us when I ask you to provide examples of bad factorial implementations from interviews.
  • (cs) in reply to pitchingchris
    pitchingchris:
    Doesn't use the input parameter and doesn't reuse the variable a, except to hold the number. Course, we all were dumb at one point, but at least we know what recursion was when we graduated.

    I never regurgitated from college, hell I never went, and even I know what recursion is.

  • (cs) in reply to Joseph
    Joseph:
    I just find it amazing that people who code like that can actually graduate!
    Don't be. In my senior level software engineering class, we worked on a program that had been developed by, I believe, the two previous semesters. I rewrote so much of the code it was mind-boggling. There were 3 separate functions to convert a MFC CString into a char*: one worked properly, but was still slower than the built-in methods, one had an off-by-one error, and one didn't always allocate enough space, so you could do buffer overruns with it. I was blown away at the time, because these people had all presumably graduated and found jobs. This site will never run out of content.
  • (cs) in reply to Welbog
    Welbog:
    bstorer:
    Welbog:
    More functions should disregard input values and just return 12. It would make life easier.
    Are you saying that n! != 12 for all n?
    In fact I'm saying n! != 12 for any n! (That last ! is for emphasis.)

    Ha! Shows what you know: 3! = 12 (base 4) 4! = 12 (base 22) 5! = 12 (base 118)

    On a related note, does anyone want to solve for z in G(z) = 12 where G is the Gamma function? Because that's like a factorial.

  • (cs) in reply to bstorer
    bstorer:
    Joseph:
    I just find it amazing that people who code like that can actually graduate!
    Don't be. In my senior level software engineering class, we worked on a program that had been developed by, I believe, the two previous semesters. I rewrote so much of the code it was mind-boggling. There were 3 separate functions to convert a MFC CString into a char*: one worked properly, but was still slower than the built-in methods, one had an off-by-one error, and one didn't always allocate enough space, so you could do buffer overruns with it. I was blown away at the time, because these people had all presumably graduated and found jobs. This site will never run out of content.

    Dear god, why do people insist on reinventing the wheel? We REJECT candidates who use their own sort algorithms in their homework assignment.

  • (cs) in reply to bstorer
    bstorer:
    Welbog:
    bstorer:
    Welbog:
    More functions should disregard input values and just return 12. It would make life easier.
    Are you saying that n! != 12 for all n?
    In fact I'm saying n! != 12 for any n! (That last ! is for emphasis.)
    Ha! Shows what you know: 3! = 12 (base 4) 4! = 12 (base 22) 5! = 12 (base 118)

    On a related note, does anyone want to solve for z in G(z) = 12 where G is the Gamma function? Because that's like a factorial.

    Using other bases is cheating. I'm telling!

  • (cs) in reply to bstorer
    bstorer:
    I was blown away at the time, because these people had all presumably graduated and found jobs. This site will never run out of content.

    Oh, of course they found jobs. Jobs where you ask "Do you want fries with that?" and give the customer exactly 12 fries, no matter how big a serving they wanted.

  • (cs) in reply to Welbog
    Welbog:
    bstorer:
    Welbog:
    bstorer:
    Welbog:
    More functions should disregard input values and just return 12. It would make life easier.
    Are you saying that n! != 12 for all n?
    In fact I'm saying n! != 12 for any n! (That last ! is for emphasis.)
    Ha! Shows what you know: 3! = 12 (base 4) 4! = 12 (base 22) 5! = 12 (base 118)

    On a related note, does anyone want to solve for z in G(z) = 12 where G is the Gamma function? Because that's like a factorial.

    Using other bases is cheating. I'm telling!

    And 2! = 12 (base... uh, 0?)

  • Noamsml (unregistered) in reply to KattMan
    KattMan:
    pitchingchris:
    Doesn't use the input parameter and doesn't reuse the variable a, except to hold the number. Course, we all were dumb at one point, but at least we know what recursion was when we graduated.

    I never regurgitated from college, hell I never went, and even I know what recursion is.

    Same here. I'm a f'ing high school student and even I know what recursion is.

    Oh, and by the way:

    
    int factorial(int n)
    {
    if (n=0) return 1;
    else return n * factorial n;
    
    //if the universe has gone insane, return 42 (cannot be a factorial)
    return 42;
    }
    
    
  • ElQuberto (unregistered)

    He could have made it interesting by throwing in a "+i* (i % 2 == 0 ? -1 : 1)" or something.

  • ElQuberto (unregistered)

    He could have made it interesting by throwing in a "+i* (i % 2 == 0 ? -1 : 1)" or something.

  • Cochrane (unregistered)

    I pity the guy who wrote that. He spend possibly several years of his life, trying to learn something that would keep him fed throughout the rest of his life, but he didn't learn anything. He is completely useless now. That's a very depressing thought.

  • Not a computer science graduate (unregistered)

    Written in a minute or two by myself, with no reference to anything:

    function factorial( $a )
    {
    	if ( $a <= 1 )
    		return( $a );
    		
    	return ( $a * factorial( $a - 1 ) );
    }

    Recursion! Factorials! Rubbish, but actually works! Doesn't return twelve!

    Unfortunately, I didn't study computer science at university - in fact, I have no computing qualifications whatsoever. Sorry.

  • (cs) in reply to Cochrane
    Cochrane:
    I pity the guy who wrote that. He spend possibly several years of his life, trying to learn something that would keep him fed throughout the rest of his life, but he didn't learn anything. He is completely useless now. That's a very depressing thought.
    Not if you're competing against him for a job...
  • Kardi (unregistered) in reply to Cochrane
    Cochrane:
    I pity the guy who wrote that. He spend possibly several years of his life, trying to learn something that would keep him fed throughout the rest of his life, but he didn't learn anything. He is completely useless now. That's a very depressing thought.

    I pitty the university and future people that apply for jobs out of said school. He is clearly a reflection of the program and how anyone can pass.

    Colleges are allowing too many students to move forward these days and there are far too few courses that weed out the weak. Infact, a Comp Sci 101 course should have been enough to address this

  • Will (unregistered) in reply to Not a computer science graduate
    Not a computer science graduate:
    Written in a minute or two by myself, with no reference to anything:
    function factorial( $a )
    {
    	if ( $a <= 1 )
    		return( $a );
    		
    	return ( $a * factorial( $a - 1 ) );
    }

    Recursion! Factorials! Rubbish, but actually works! Doesn't return twelve!

    Unfortunately, I didn't study computer science at university - in fact, I have no computing qualifications whatsoever. Sorry.

    Isn't factorial(0) defined as 1?

    Captcha: muhahaha!

  • (cs) in reply to Kardi

    I dunno if I agree that this guy is a reflection of how bad the school from which he graduated is.

    I had plenty of difficult computer science classes in college, and I was just a computer science minor. Several people in several of my classes either failed or withdrew from the class half-way through because they just weren't getting it. Still, though those classes were good at weeding out the morons, a few exceptionally skilled slackers managed to make it through to their senior year. I got stuck with them on group projects. They were useless, lazy, and they didn't seem to know crap. How they managed to pass their previous classes was and still is beyond me.

  • tlf (unregistered) in reply to Noamsml
    Noamsml:

    Same here. I'm a f'ing high school student and even I know what recursion is.

    Oh, and by the way:

    
    int factorial(int n)
    {
    if (n=0) return 1;
    else return n * factorial n;
    
    //if the universe has gone insane, return 42 (cannot be a factorial)
    return 42;
    }
    
    

    Nice code. Will never return though...

    Captcha: alarm - what this code raised internally (in me)

  • Andorbal (unregistered)

    Some CS programs are really MIS programs in disguise. Don't be fooled!

  • (cs) in reply to Noamsml
    Noamsml:
    KattMan:
    pitchingchris:
    Doesn't use the input parameter and doesn't reuse the variable a, except to hold the number. Course, we all were dumb at one point, but at least we know what recursion was when we graduated.

    I never regurgitated from college, hell I never went, and even I know what recursion is.

    Same here. I'm a f'ing high school student and even I know what recursion is.

    Oh, and by the way:

    
    int factorial(int n)
    {
    if (n=0) return 1;
    else return n * factorial n;
    
    //if the universe has gone insane, return 42 (cannot be a factorial)
    return 42;
    }
    
    

    But evidently you haven't gotten to the part about infinite loops yet.

    else return n * factorial n-1;
  • Fj_ (unregistered) in reply to Anon

    LOL WTF?

    It actually returns 13, because 12 is incremented one last time before the comparison.

  • FalconNL (unregistered) in reply to Noamsml
    Noamsml:
    Same here. I'm a f'ing high school student and even I know what recursion is.

    Oh, and by the way:

    
    int factorial(int n)
    {
    if (n=0) return 1;
    else return n * factorial n;
    
    //if the universe has gone insane, return 42 (cannot be a factorial)
    return 42;
    }
    
    

    At least the code in the story produces an answer when n > 1 ;)

  • DZ-Jay (unregistered) in reply to Noamsml
    int factorial(int n)

    {

    if (n=0) return 1;

    else return n * factorial n;

    //if the universe has gone insane, return 42 (cannot be a factorial)

    return 42;

    }

    Congratulations! You have just discovered Infinite Recursion (TM), or more commonly known as the stack overflow.

    -dZ. 
    
  • (cs) in reply to Fj_
    Fj_:
    It actually returns 13, because 12 is incremented one last time before the comparison.
    I suggest learning more about for loops.
  • Fj_ (unregistered) in reply to Fj_

    Ooops, but it isn't stored though =/

  • NiceWTF (unregistered)

    Well, but at least he knew what a factorial is! (mostly...anyway)

  • Kthx Bai (unregistered) in reply to Cochrane
    Cochrane:
    didn't learn anything. He is completely useless now

    In other words, perfect management material... from here its all up up up the ziggurat lickity split!

  • (cs)

    Here's the (obviously) corrected version in Python:

    i = 1
    a = 0
    while i < 10:
            i = i * (i + 1)
            a = i
    print a
    
  • sxeraverx (unregistered)

    4.5224! = 12

  • Justin (unregistered) in reply to Anon

    Quite right. Looks like I needed to sleep some more last night!

  • (cs) in reply to Joseph
    Joseph:
    I just find it amazing that people who code like that can actually graduate!

    Garbage In - Garbage Out

    I sat behind a girl in my Comp Sci classes who declared to the entire class that multiplying a positive number by a negative number yielded a positive number.

    How can you expect to build a competent computer scientist on that kind of foundation? Imagine what she cranked out for recursive algorithms.

  • thisisnotmyname (unregistered) in reply to DZ-Jay
    DZ-Jay:
    int factorial(int n)

    {

    if (n=0) return 1;

    else return n * factorial n;

    //if the universe has gone insane, return 42 (cannot be a factorial)

    return 42;

    }

    Congratulations! You have just discovered Infinite Recursion (TM), or more commonly known as the stack overflow.

    -dZ. </div></BLOCKQUOTE>
    

    Not sure what language this is, but I think you want if (n == 0) return 1;

  • sxeraverx (unregistered) in reply to sxeraverx

    Sorry about that, gamma(4.5224) = 12, so:

    3.5224! = 12

    My bad.

  • Guru Buckaroo (unregistered) in reply to DZ-Jay
    DZ-Jay:
    int factorial(int n)

    {

    if (n=0) return 1;

    else return n * factorial n;

    //if the universe has gone insane, return 42 (cannot be a factorial)

    return 42;

    }

    Congratulations! You have just discovered Infinite Recursion (TM), or more commonly known as the stack overflow.

    -dZ. </div></BLOCKQUOTE>
    

    What you have all failed to notice, is that this function WILL return, immediately, the value it is called with. After all, that first if is using an assignment operator, not a comparison operator.

    'course, it won't compile.... but who's perfect...

    Captcha: gygax (thief)

  • Guru Buckaroo (unregistered) in reply to Guru Buckaroo

    Damn, misread the code myself. This is what happens when I try to debug before caffeine.

Leave a comment on “F'd Factorial”

Log In or post as a guest

Replying to comment #:

« Return to Article