- 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 don't see the wtf. He got the right answer, sure, a bunch of ifs could be removed if he payed more attention to the properties of the numbers he have chosen. Why bother, though? It seems like he probably just rewrote the problem's question in PHP, that's excelent.
Now, using if instead of &&, well, it might hurt performance a little bit, but really, who's counting?
Admin
To those who are suggesting he is dumb for starting at 1 instead of 2520:
The correct answer is 232792560. Congratulations. You've saved .001% of the runtime. My hats off to you.
To those suggesting counting down from 20!: 2432902008176640000 is 20!, that'll take 10 billion times longer than counting up. TRWTF is definitely in the comments
Admin
Personally, there's no way I would come up with an elegant solution to that problem. Frankly, even though I've taken a ton of math throughout school I've forgotten most of it as my day to day life never demands it. So I would end up writing some sort of brute force method.
That said, the code in the OP is a pretty poor way of doing it brute force wise. To me, writing it in such a way demonstrates that you're not thinking in terms of good programming practices.
At the very least, you could do something like this (this is very off the cuff with not much thought behind it and I'm sure there's a better way to do it even via brute force):
That of course ignores the initial information and math tricks of doing it elegantly. If I knew the elegant way of calculating it mathematically, I could reflect it better in my code. But I don't, that's what google is for. Therefore brute force. It's also not in php, but it can be easily ported to php.
It's ugly, I know, but it's the first thing that pops into my mind on how to go about brute forcing it.
Addendum (2008-10-08 14:34): Oh yeah, and my code should check for overflow as well to keep it from looping infinitely.
Admin
Say what you like, but this is a good question. Not as good as asking them to write atoi, IMHO, but a good question nonetheless. When I've used this question at my company we have always favored interaction and dialogue over getting the math right. BTW, if you interact well -AND- get the math right, you're in.
Read Chapter 15 of DeMarco & Lister's Peopleware. In short, you wouldn't hire a juggler without first having seeing him juggle... It's easy enough to argue about appropriateness of any question... for instance, instead of a math puzzle why not ask questions about how to parse XML and select a particular node? Well, that's stuff that's often readily looked up in online help; if you don't do it on a daily basis, why not look it up?
Admin
Touche.
Admin
The picture looks shopped.
Admin
Well, you can, um, ask them questions. I've never needed to resort to silly little tests like this one to determine if someone knows what they claim.
Ask them about their projects, how they were structured, why they were structured, how they came to that decision, etc. For example, if they list 'AJAX' on their resume, ask them to describe to you how that works exactly. If they list 8 different sql servers they've worked with, ask them to describe the differences between them.
If the interviewer is technically competent, they can smell out someone full of BS immediately. If they arent technically competent, they shouldnt be the one interviewing programmers.
Admin
that picture may be, but the machine is real. I first saw it on Discovery Channel (could find the tape and rip it if you want). Oh, it also has a wikipedia entry. It also requires ~16MW of power...
Admin
If you're so concerned about speed, run this guys 'inefficient' function once, and put the results into an array in code, or in a db. Then you never have to run the process again.
Admin
The solution in my favourite language (name withheld to avoid flaming):
(2520 * 1:up) \ NOT ($ % 1:to(20)) ~= 0;
Admin
is impressed
Admin
See? A few times ... a very few times, these "puzzle" interviews actually make sense.
Admin
dude the php takes only 4 Secs to Finish so PHP > PERL :P
BTW my code which by me looks better takes 46 sec's :
Admin
Thanks for the pointer to the great puzzles!
For the algorithm I came up with...
Compute the prime factors of all the numbers from x to 2 Eliminate the duplicate terms* Multiply the remaining terms for the win.
captcha: bene (Latin prefix for good or well.)
Admin
Not picking, but you missed something there. If you solve your way, you get 9,699,690 (not right). Not divisible, for example, by 16 (2^4). Right answer would be 2^4 (for 16), 3^2 (for 9), 5,7,11,13,17,19 -> 232,792,560. Not primes, but prime factorization of EVERY number 1-20, then taking the largest count per number. 10 is covered by 2 & 5, so don't need anything extra. 9 is 3x3, and for 2 you have 2,4,8,16, so 2x2x2x2 gets it. Points for effort, though.
Admin
4 microseconds on my machine
Admin
Not picking, but running that code prints 232792560. The inner while loop is to get the 2^4/3^2 (you only need to find the max power of each prime < max to account for all prime factorizations < max).
Admin
First, it is shocking how many of the "better" answers are incorrect. Incorrect code has negative value, people!
Second, those defending brute force on the basis of "it doesn't take all that long to run" are idiots. What if the problem had been to run to 25? It's blatantly clear that the interviewee has no idea about actual runtime. The fact that it runs in less than an hour (day, year, century) is happenstance. O(n!) is really, really bad, and if I ever get a solution of that sort, I'm going to immediate raise questions with the customer about reuse.
Third, the joker talking about asymptotic behavior for a problem this small is another sort of idiot. Behavior at the limit is just that--at the limit. At the limit, you dedicate a warehouse to servers that precalculate answers & do a lookup when you need an answer.
Forth, this sort of loop nesting is a code stench. It is unmaintainable (same problem with some of the "improved" versions). Unmaintainable code has a strong negative component to its value. (Same problem with just hard coding the value--it's mAgick!)
Fifth, the use of a problem from the internet is a WTF. If someone were to be working through the problem set in their spare time, their performance will be unjustly inflated. The best coding problems I've faced (by far) have come directly from production code/production problems.
Sixth, interview code problems are entirely about approach. Someone who attacks twenty problems without ever asking for clarification or detail is a coding sUper gEnius. whoo! (That whoo! is the sound of the train whistle coming down the tracks that your dynamite shack is now on) Someone who presents this solution without interacting has just left a O(n!) s*** bomb in your code. Hire if you like manual minefield clearing as a hobby.
Admin
You'd hate me. When I don't know something, I google it to find out. What the hell else are you going to do?
Admin
I just want to bask in the lispy glow for a bit...
Admin
Sure, there are more efficient solutions. But we have to know the assumptions behind the problem. As stated, this is a one-shot problem: Find the one number that meets this condition. Once you get the answer, there is no reason to ever run the program again. Under those circumstances, it makes a whole lot more sense to spend ten minutes writing a program that takes twenty minutes to run then to spend two days optimizing it to run in three seconds. Especially given that the brute force solution is much easier to verify than a more sophisticated solution.
Reminds me of a programming contest that a math teacher in my high school conducted: Write a program that prints all the prime numbers less than 100. Winner is the program that produces accurate results with the fastest run time.
The entry that had the fastest run time consisted of one line of BASIC code:
The teacher disqualified it. It wasn't my submission, but I wish it was: It's hard to imagine a more efficient solution.
In real life, if a client tells me that as part of an advertising campaign they need to find a customer in Wisconsin who has bought more than $500 worth of widgets in the past year and who has a name that starts with a "W" because they want the advertising slogan to have all words that start with "W" -- sure, I could try to guess what similar advertising campaigns they might have in the future, generalize the problem by making a bunch of these specifics into parameters, develop user-friendly input screens for these parameters and optimize the queries to extract it. But it would all be a guess, so why bother? And it would take at least days of work, depending on how fancy I wanted to get. Surely the better solution would be to hand-type a SQL query with everything hard coded and give them an answer in ten minutes.
Admin
Very nice :-)
Admin
Surely Max is A LOT higher than that (you haven't multiplied by 2-10 ???? Which would give 2432902008176640000
Which we could continually multiply by any of the numbers 1-20 ad infinitum.....???
Admin
Then offer test questions for which recursion makes the most sense and see if the candidates can spit something out. Like maybe "Assume qsort doesn't exist. Write a function that can quicksort an arbitrary sized array of integers". Or better yet, think back to complex problems that have been posed by your own in-house projects and ask candidates how they would have solved THOSE.
I've only used the LCM algorithm once, in college, in a computer logic class. That's how generally useful it is and how likely a programmer is to remember it. So unless it is of dire importance to your specific project, it is of little value to require.
Perhaps another test would be "can you turn this general algorithm description into PHP code?" similar to what we had to do in compiler construction when we wrote a full LR parser in Java. Of course a classmate and I both improved the algorithm by optimizing it down from hours to parse Pascal grammer, to under a minute. Too bad full LR parsers aren't used in the real world :)
Admin
Obviously, the challenge is to get it to arbitrarily scale; eg go up to n=25, n=50, ... the biggest problem you have is the sheer size of the resultant integer.
For n=1...42, you get 219060189739591200. Go higher than 42 or 43, and you need something above a 64-bit integer.
Accepting the limitation of only being able to compute a 64-bit integer, I came up with the below code, which can compute the n=42 example above in a fraction of a second.
It may still be O(n!) in the theoretical worst case (real world scenarios would be much lower), but since you can't exceed a 64-bit result without an arbitrary-precision integer library, I suppose it's probably the optimal solution when limited to built-in integer types.
It also only requires 32-bit integers to compute n=20.
Admin
http://codepad.org/N4U8ZK3g
Admin
I would hire a man with a shovel, obviously. Because if the requirements changed to a hole 1km wide and 500m deep I wouldn't have lost anything by digging the original hole with a shovel, nor would I have to throw it away, nor re-do it a different way; there's been no lost effort and the hole is maintainable whatever method has been used to dig it.
As analogies go, I've seen worse - but only just.
:)
Admin
Oh yes, we are so impressed with your inability to grasp basic arithmatic, which you call "puzzles".
Any idiot who graduated from elementary school should realize he doesn't have to check for 1 through 10 if 11 through 20 passed.
But hey, you keep writing your bloated code and being smug about your incompetance.
Admin
does it in 9 seconds on a 1.3GHz Athlon XP Thoroughbred.
#include<stdio.h>
int main() { int i = 2520;
while (1) { if ( (i%20 == 0) && (i%19 == 0) && (i%18 == 0) && (i%17 == 0) && (i%16 == 0) && (i%15 == 0) && (i%14 == 0) && (i%13 == 0) && (i%12 == 0) && (i%11 == 0) ) { printf("%d\n", i); break; } i+=20; }
return 0; }
does it in 0.345 seconds on the same machine (not counting compile time)
Admin
Oops, sorry. Still had that previous post in mind. I meant to say it is O(sum{1..n}), eg n(5) = (1+2+3+4+5) = 15, and not n(5) = (12345) = 120. Quite the difference there, but the overhead still grows exponentially as n increases (eg 5=15 (3x), 6=21 (3.5x), 7=28 (4x), ...)
Admin
It's O(n^2). And simpler than all the other correct solutions. Nice :)
Admin
I'm laughing here that so many people are commenting that this is a horrifically complex math problem, or that "you'd need a degree in math to solve it", or proposing solutions with loops or GCDs or other such stupidness.
The reason I got first post is because I went .. it has to be divisible by every prime less than 20 ... oh yeah, and repeated factors of 16 and 9 will get in the way too.
So I open up calc and type
16957111317*19
and got first post.
Any of you complaining about the complexity of this problem, or proposing any solution that involves any code AT ALL, then I am glad you do not work anywhere close to me.
Admin
And how much time took him to write that program? 10 minutes? So, if the requirements changed, and he had to redo the program to support numbers up to 1000, he lost only 10 minutes of writing the initial program. Instead of googling (was he even allowed to do that?) how to find a smallest common multiple. But, I am not a programmer (can only make simple programs) so what do I know...
Admin
What if Max = 5?? Even running the loop 2 to max you'd get 234*5 = 120?? Looking for 60, though...
Admin
It instantly reminds me of a snippet of php code posted on wtf-like site in which case the force turned out not brute enough... The task was to resize image to fit given dimensions, and solution was like ( in c ) x=x0; y=y0; while ( x>xmax || y>ymax) { x*=0.99999; // decrease dimension a little bit more until image fits y*=0.99999; } image_resize(x,y); ... It were ok is hoster weren't killing processes consuming 100% cpu time. But it required too much math...
Admin
A loopy Sol'n - No Knowledge of primes req'd - and it's kindof intuitive... reduce an array of integers from 1 to 20 into what needs to be added to element i to make (i+1) a factor of the product of the elements preceding it....
Obviously, if you want extendibility you could generate the array on the fly, put all this into it's own method etc, etc, etc....
Admin
I like PHP. I really do. But it's a web language. It's for building web sites & web apps. I wouldn't use it to solve any of the problems on Project Euler. Sure, it could be used to solve those problems... I just wouldn't do it. I also wouldn't take a job where I'm expected to be a math genius. That's not what my experience in web app development has led me to be.
Admin
Admin
Yay fr Project Euler. Spend all my spare time on it. In about two months I haves solved 63. How about you guys?
Admin
6 minute attempt (written in notepad, thus untested):
Admin
Can anyone solve this:
http://projecteuler.net/index.php?section=problems&id=202
Admin
In fact, I would go so far as to say that it was indented properly. :)
Admin
You assume that we all face north when using a computer
Admin
=LCM(A1:A20)
Admin
So you've never used encryption?
Admin
Why would someone want to reuse such function? It's purpose seems just to spit out a number, after the number is known the whole function is not even needed anymore. Are you proposing creating new problems for solve before you even got to solve the first one?
What's unmaintainable about it? Mind clearing that up? Hard coding the value is just magic if you don't explain in your comments how you came to that result. If you explain, then it's pretty clear. That's just bull. Any respectful programmer have writen his share of code, why don't the interviewer just ask for that? Makes it a lot easier on everyone.Admin
That talk about mintainable is much bull. We cannot know all possible uses of the code in advance. As long as you write something that's reasonable and your code is clean enough to be understood. If someday the time comes where the requirement changes, things can be changed because everyone will understand your code and will be able to modify it to make the new things work.
Admin
WTF == Productivity measured in lines of code written per day.
Admin
It's idiotic to just assume that the code will never be used again and it's even more idiotic to write shit one-off code in an interview. In an interview you want to show your best not show your worst to save a couple of minutes. Most anyone can hack together a shit solution and in general companies prefer to pick new employees from outside the shit pile.
I've personally been burned more than once when I had to spend time rewriting a “one off” piece of code that's now been used 20+ times (and much worse than that). It's generally good habit if nothing else unless you're really pressed for time or know 100% it will only be used once (which if likely rare).
It's quite clear to anyone with any interview sense that a reusable solution shows much more of your abilities. In a more practical sense it's a decent assumption that your want a flexible solution if you have the time for one and aren't certain you don't need one. Note that in an interview you have the time for it and unless you ask (note: it's amazing what your mouth can be used for) you can't rule out it being used for other problem parameters.
It's quite common where I work to first ask for a specific piece of data or code then to ask it with different parameters. Sometime the first one is enough, other times it's not and sometimes there's a big gap between the two. Sometimes things you assume shouldn't ever be asked for again are by someone working on something just slightly different. Sometimes requirements change and you need to modify your code appropriately then regenerate whatever it generates.
I personally find it much more efficient to write decently sane and reusable code instead of making idiotic assumptions that waste more of my time in the long run. Then again that is job specific however like I said it never hurts to put some effort into an interview.
Of course, thinking ahead and planing are vital skills if you want to be anything more than a glorified monkey. If you don't do that then you end up in dead ends when coding, waste time and aren't worth it for any non code monkey positions (ie: more efficient to hire someone who can think rather than someone who can think AND a bag of meat to type on a keyboard for them). Someone who can prevent disasters by seeing and avoiding them (or just preparing for them) it worth more than someone who plows straight into them unsuspectingly.
The interviews wants to know, among other things:
Admin
TRWTF is that still many commenters think that optimizing the loop is the way to go. You don't have to search for the answer, you have to calculate it. And it's basic math they teach at university