- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I'm not surprised by this one bit. I've had classes with people who were so completely ass-backwards with computer science that I've wondered how they managed to get this far in the curriculum.
You don't learn at college. You learn with real world experience. College gives you some nice groundwork and a pretty piece of paper at the end.
Admin
Mandatory Prolog solution:
Admin
If you want to use other languages....
!n
That is all that is needed in APL, of course if you wanted some protection for negative values you would add use the following which returns 0 for any negativ value.
{w≥0:!w⋄0}n
Then again that code needs about a page of comment just to understand it....
Admin
Stuff like this is the downside of group projects in college. Any group I was in was generally 3 or 4 people, and always had at least one Village Idiot. So anywhere between 25% and 33% of CS grads are morons.
Admin
select factorialValue from tblFactorial where intNumber='$myNumber'
Admin
Admin
Admin
I really wish people would stop teaching recursion using factorial as an example.
Factorial is the WORST possible example of recursion there is. It's slower and less efficient to do it recursively than iteratively.
Why don't they use actual recursive things for teaching recursion - like trees or something.
I learned recursion in school, and thankfully I was smart enough to realize when to use it and when not to. I've seen recursion used so many times in inapropriate places that it sickens me.
For a great discussion of it, try picking up a copy of "Code Complete"
Admin
Admin
Which is exactly why I think the college degree is useless after a few years in the field. Even the self taught should have the basics by then and both would have done real learning under the senior developers they worked under.
Admin
Admin
I wouldn't have believed this except that I was a homework grader my senior year in college and actually SAW things like this.
Admin
Hmm, amazingly nobody posted an actual iterative implementation yet... Here goes (C++):
I always think it's funny to see people's suggestions for actual implementations and then find some obvious flaws in them. You'd think people would check these things over at least half a dozen times before submitting them to a forum that mocks bad code... So now it's just waiting for Murphy's law to kick in and people to start pointing out any mistakes I might have left in this one, despite checking it over many times and actually compiling and running it ;)
Admin
Admin
Recursion is always slower than iteration, it is obvious. However, efficiency is not everything, sometime you give up on some efficiency for clarity or simplicity, this is where you use recursion. Now, if the students learn it when they can't handle complex code (in the middle of the first course, probably), factorial is a very good example, calculated as defined.
By the way, I can't understand how such an ignorant could graduate. I've seen some graduates that could not handle new concepts, and that had problems handling the details of algorithms, some that were (even after several years of actual work) mostly useless, but this is a school-level question, its not real-world! If he could not answer that, how did he passed exams? I'm tempted to put the blame on American colleges and claim that here (in Israel) this level of uselessness is impossible...
Admin
Public Function Factorial(ByVal n As Integer) As Integer If n > 2 Then Return n * Factorial(n - 1) Else Return n End If End Function
Quick VB.net function.
Admin
Admin
Admin
Don't you think i has the right to be incremented as well? It seems to me that your python version returns 42 instead of 12 (however, I don't know python so I might be wrong...)
Admin
I found asking people to explain what object orientated programing is was a good way to filter. It's amazing that some people can attempt to answer that question without using the word "object". I do that one in the phone interview.
Admin
Admin
Admin
agreed. I didn't put much effort into it :P. Sad part is that the code I wrote above would probably pass as an acceptable solution.
Admin
All your base are belong to computer science students.
Admin
I don't get it. I always thought factorials to be calculated by
or something similar.Admin
Another mind destroyed by languages without proper support for tail-recursion...
Admin
I had a fellow coworker who recently received a CS diploma.
Looking over some string manipulation in VBA to parse some text, they asked "How did you think to come up with using all this mid, left, right, len stuff to get what you need?"
sigh
Admin
He did not know iteration or recursion but knew about Factorials?
HIRE HIM. PUT HIM IN SALES!
{He has potential as a Consultant}
Admin
Recursion is not always slower than iteration, especially if your own time, time to understand, or time-to-market is a consideration.
For example many compilers use recursive-descent parsing, which is usually tons cleaner, more readable, and less buggy than stack-based or other kinds of parsers.
Admin
"A style of programming in which each entity comprises both information and the operations that manipulate that information; all entities are regarded as self-contained and autonomous, communicating only by invoking each other's operations."
Admin
Doing factorial through recursion is a terrible requirement itself.
Admin
Fools, everyone knows that the only way to define a factorial is
Admin
Admin
Ugh, I hate that O(1) solution because it turns an integer function into floating point math. Also, everything has to be rounded before being truncated into integers. Blech. I'll stick with a Fast Fibonacci algorithm like the one used by GMP.
Admin
Brilliant! Why have bad code, when you can have a test for bad code that's even worse? No functions were harmed (or even touched) in the making of this test.
CAPTCHA craaazy
Admin
Admin
It is funny, but even factorial(0) will return, eventually. You'll just have to wait for the overflow...
Admin
Don't bother; there have already been plenty posted to this thread...
Captch: the name of the man responsible for a brazillian factorial deaths.
Admin
Admin
Well done. And you're not hired because of your lack of ability to explain a concept in SIMPLE TERMS. That's why I like the question, it weeds out both people who don't actually know the concept and those who know the concept but can't communicate for shit.
Admin
And you can't filter out smart-asses too!
Admin
Having said that - how does it change the substance of my point, exactly?
Admin
Admin
ha ha ha. how droll. those wacky IT grads.
Admin
function factorial( $a ) { if ( $a <= 0 ) return( 1 ); return ( $a * factorial( $a - 1 ) ); }
for more fanciness, you can do if($a < 0) return( NAN ), and handle all integers.
Admin
Actually, the if statement will always be false. therefore, it will recurse infinitely.
Admin
Recursive implementation is inefficient, but still the factorial and the Fibonacci sequence are very good educational aids for learning about recursion. It's simple, for one thing. You guys seem to mix what is good in school, with what is good in real life.
Admin
Are you selling this computer which can calculate basic math functions in O(1) time for arbitrarily large/precise arguments? Because I would very much like to buy one.
Admin
That was the whole point of fixing it. 12 is not the ultimate answet to life, the universe and everything.
Admin
You get to hear that strange whooshing sound a lot, don't you?