 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
So you got experience with C and embedded systems? Great, you're the perfect candidate for our PHP job!
The other way around would be a much bigger WTF of course, but it's still a waste of a lowlevel programmer.
Admin
Roman numerals give you fun problems anyway. There's never been a formal way of writing them; clocks, for example, tend to use IIII for 4, whereas people will tend to write it as IV.
Admin
Well obviously he should have called out to a web service using XML. There must be one somewhere that can tell you if a given number is prime.
Just hope it isn't encrypted over https. Because in setting up the crypto you'll probably need some prime numbers! Recursion lurks wherever you look. If you don't see it at first, look closer.
Admin
Admin
When asked to prove that all odd numbers greather than two are prime:
The mathematician said: "3 is prime, 5 is prime, 7 is prime, ... and so on."
The engineer said: "3 is prime, 5 is prime, 7 is prime, 9 is experimental error, 11 is prime..."
The arts major said: "What does 'prime' mean?"
Admin
Man, why are we fiddling with all this Mersenne stuff?! According to him we can have 100,000 digit primes just by checking MOD 2, 3, 5 and 7!
Admin
It also has the benefit of making those pesky NPhard problems run in polynomial time (of the length of the input)
Joe
Admin
No, factorising prime numbers is very easy ... by definition, the sole factor is the number itself. It's factorising large NONprime numbers that gets tricky, and that's the bit that's needed for breaking publickey crypto systems like RSA.
Admin
PHP is an extendable language. Knowledge of C would be good because you can write your extensions in it.
Admin
At least he was honest. His crap code DOES represent his work very well.
Admin
Done?
Admin
I think everyone is missing the point.
It doesn't really matter if the prime test is correct. This is an interview to see if the guy can code. So judge the code, not the algorithm. There is plenty of evidence in the first sample (which worked for a small set of numbers) that the coding is sloppy and misguided.
Only if the code is good should you even consider looking at if the algorithm is good.
My other thought was, even if they guy's code was great, would you hire him after such a bad interview? If his code was great, and you turned him down, you could be in a whole lot of bother if he challenged the decision any the grounds of any form of discrimination he chooses as your only hard evidence is a good piece of code!
Admin
if (($Number > 0) AND ($Number < 1)) { return (FALSE) } else { return (TRUE) }
I see his level of expertise right there. If he can't do
return ($Number > 0) AND ($Number < 1)
then his code it too wordy for consideration.
Admin
Admin
The real WTF is the apparent lack of WTF punchline. I mean, Boris' "homework" answer approaches CodeSOD levels, but it was rejected along with Boris, yielding an uncharacteristically happy ending for everyone except Boris.
WTF?
Admin
And we see yours...
Admin
I was looking at some code I wrote a few years ago and testing for 2, 3, 5 & 7 are sufficient... if your using SPRP (Strong Probable Prime) instead of modulus.
If n < 25,000,000,000 is a 2, 3, 5 and 7SPRP, then either n = 3,215,031,751 or n is prime. This covers all unsigned 32 bit integers.
int Prime (unsigned n) { if (n == 3215031751) return FALSE; if (n <= 10) { return (n == 2)  (n == 3)  (n == 5)  (n == 7); } if ((n % 2) == 0) return FALSE; return b_SPRP (n,2) && b_SPRP (n,3) && b_SPRP (n,5) && b_SPRP (n,7); }
Sources: http://primes.utm.edu/prove/prove2_3.html http://www.olivierlanglois.net/archive/prime_cpp.htm
And yes, I am the Daniel fixed a bug in the second link.
Admin
% R
Admin
TRWTF is interviewing Boris instead of Natasha.
Admin
Admin
Admin
The tough part is that you get into recursion checking if the number is prime inside the loop. More efficient to just run the modulus test.
Admin
Doris?
Admin
99999997 is not prime.
1297 * 77101 = 99999997
You sir, fail at prime numbers.
Admin
Actually, the real WTF was given by Phil in one of his comments. Phil's colleagues wanted to hire Boris anyway.
Admin
I had already pointed that one out at the end of page 1.
Admin
TRWTF was Boris being "eager to gain experience in" PHP.
Admin
Well, that's what I get for not refreshing the comments page...
Captcha: abico. I abico any responsibility for my failure to properly read all comments.
Admin
Presentation matters. A bad programmer can still get a job(depending on who's hiring). And, companies without engineering talent can succeed if they have presentation skills (obviously by ripping their customers off).
Admin
This is why, if there are 40+ comments, I wait until there are 50 to add mine. Posts don't exist if they're below the fold.
Admin
You can fold your monitor?
Admin
When I was in high school, we once had a programming contest where the problem was to write a program that would produce a list of all the prime numbers less than 100. Entries were to be judged based on accuracy, conciseness and efficiency. Namely, any programs that produced incorrect results failed. Working programs were then scored by multiplying the number of lines of code (excluding comments) by the runtime, and low score wins. (Which, by the way, I think is an excellent scoring system.)
A friend of mine submitted a program that looked basically like this (I don't have the original listing, but this was the basic idea):
10 PRINT "2,3,5,7,11,13,17,19,23,29,31,37,41,43,47" 20 PRINT "53,59,61,67,71,73,79,83,89,97"
His entry had the fewest lines of code and the fastest runtime, so under the rules, he should have won. He also commented that he thought the algorithm was very easy to understand and so the program was highly maintainable. But the teachers disqualified him.
Admin
I can write a function to factor a prime number very easily.
Perhaps you meant that factoring a composite number is very difficult? It's certainly not conceptually difficult  you can always do it with the simple "loop through successive integers attempting to divide" method and when you get a hit, pass the quotient back in to the same function recursively. e.g. say you start with 455. Divide by 2? No. 3? No. 5? Yes, quotient is 91. So first factor is 5. Now repeat process using 91. Divide by 5? (Note we can start with the divisor that last succeeded, as we already know it's not divisible by smaller numbers.) Anyway, divide by 5? No. 7? Yes. Quotient is 13. So add 7 to list of factors. Now work on 13. We start at 7, we're already past the square root, so we're done. 13 is the last factor. Prime factorization is therefore 5713.
Perhaps you meant that finding prime factors of very large composites in some reasonable amount of time is difficult.
Admin
I don't think that's quite right. You see, if it's trivial to determine if a number N is prime, that necessarily means you know at least one factor: 1 < F < N. If this is the case, then recursively apply this test to N/F and you've trivially factored N. But because factoring N is nontrivial, it seems that this wouldn't be possible.
This is similar to the reason why it's impossible to create a general compression algorithm that can compress N bits into N1 bits without loss. Recursive application necessarily yields a contradiction.
Admin
I am constantly amazed by how many commenters here are unable to conceive of a bruteforce algorithm to solve a welldefined problem.
Admin
BAD! NO! I know that PHP is TRWTF, but you should at least know why your suggestion is wrong before you make the claim.
A PHP variable, under the hood, is a struct that stores the base type, and the typed representations of the variable for each base type. (Yes, for each of the 6 base types: int, float, string, bool, object, and reference. The WTF is strong with PHP.)
When you test a variable using the == operator, it evaluates the type of the lvalue to find the correctlytyped internal value of the rvalue to compare against the lvalue.
When you test a variable using the === operator, it evaluates the type of the lvalue against the type of the rvalue, and if that matches, it then evaluates the value of the lvalue against the value of the rvalue.
The reason your third checkInteger fails is because it coerces the type of the lvalue. When you cast (int)$num, it's creating a copy of $num, but setting its type to int. Then you're comparing typefortype against an uncoerced $num. If $num is anything but an int already, this implementation of checkInteger will fail.
A correctly idiotproofed PHP function should always treat "1" the same as 1. If a PHPhackclaimingtobeadev called checkInteger("1"), it should work correctly and return true.
But wait! There's another WTF buried here. Since the (int)$num takes the internal integer representation from $num and makes it the "real" value of that instance of $num, then compares it to the original $num's internal integer instance, this function effectively proves nothing but identity.
Think of PHP variables as a struct:
If you have one that's set up like so:
When you cast it to an int, you get this:
So when you compare PHPVar1>intVar == PHPVar2>intVar, you always get true, regardless of the value of either ones typeEnum.
Repeat after me: PHP is TRWTF.
Admin
Yeah, I'm pretty buff.
Admin
Admin
Admin
I did that (mumble, mumble) years ago in junior high school.
Admin
CCD, Can't Code Disease.
Regarding foreigners and phone interviews, one of my favorite phone questions is "can you write a regex that checks if parentheses are correct in an arithmetic expression?"  and I know the right candidate when they start laughing. Mostly it happens if you call a candidate in Poland; they have an excellent education there. (Disclaimer: I've never even been to Poland.)
Admin
(And no, it's not possible that Boris may just be a great C/embedded programmer. Even though he won't have to checkInteger in C, there's plenty of WTFs in those code samples to make this absolutely certain.)
Admin
No, to me it smacks of someone who has much too big an ego (billable code, what?) and not enough clue of anything.
Admin
I had an interview where I had to write a prime number algorithm on a white board, in C#. Didn't do too bad at it, but not too good either. So I felt challenged, and that afternoon I went back to the drawing board.
I wrote a single linq statement that correctly calculated the prime numbers up to some arbitrary number.
var cnt=1000000;
var query = (from two in Enumerable.Range(2,1) select two) .Union (from candidate in Enumerable.Range(3, cnt2).Where(p => p % 2 != 0) select candidate).AsParallel() .Except ((from candidate in Enumerable.Range(3, cnt2).Where(p => p % 2 != 0).AsParallel() from q1 in Enumerable.Range(3, (int)Math.Sqrt(candidate)).Where(p => p % 2 != 0).AsParallel() where q1 < candidate && candidate % q1 == 0 select candidate) .Distinct()).OrderBy(x => x);
foreach (int i in query) Console.WriteLine(i);
Admin
(1) Test last digit for even/odd. (2) Test last digit to determine whether divisible by 5. (3) Starting with 3, test for divisibility by numbers ending in 3, 7, 9, or 1.
I just knocked about another 20% off your execution time. You're welcome.
Admin
...why is nobody commenting on the fact that checkInteger and checkDouble are exactly identical.
Boris was not just an idiot, he was a liar.
Admin
An engineer, physicist and Computer Scientist we're asked to show whether all odd numbers were prime.
The engineer (a bit miffed that we was being bothered with such an irrelevant problem) suggested that until someone (not him) could offer a counter example of an odd number that was not prime, we should accept that all odd numbers might be prime.
The physicist chose some odd numbers, 3,5,7,11 and concluded that as all of these were prime, it might be a reasonable hypothesis that all odd numbers should be prime.
The Computer Scientist (possibly Boris) went away and laboured on a a complex algorithm to determine whether a given input was Prime. He tested it extensively before declaring that all odd numbers must be prime: 3....PRIME 3....PRIME 3....PRIME 3....PRIME 3....PRIME ...
Admin
As a preliminary exercise it filters out some of the useless  and a few questions (at a subsequent interview) about how their solution works and obstacles they found in creating their program usually exposes those who understand what their program does (irrespective of whether they wrote it or copied it) and those who definitely copied it without understanding how it works. Of those who can explain how it works, it doesn't really bother me whether they made it themselves or copied someone else's directly (the fact they understand the workings is reasonable assurance that they won't blindly copy stuff that doesn't work  and may be more inclined to use inbuilt API's than the gungho developer who believes they can make a good Calendar/Date class).
Do you want a candidate who has a limited amount of skill that will never develop, or one that is happy to teach themselves by googling for ideas?
Admin
You use these sort of algorithms to create lookup tables. If you need to find whether a number beyond your list is prime, you can always extend it (using the same formula).
Startup: calculate primes to 19 Eg: Test if 9 is Prime  Lookup. Test if 25 is prime  better extend list to 29 and lookup Test if 23 is prime  lookup test if 34 is prime  extend list to 37 and lookup etc....
of course if it's a one off (or a trivial case (%2) then it might be overkill)  but in the real world nothing is ever a oneoff.
Admin
But the candidate failed, so score one for that assignment.
I'd say "ability to use google" is necessary but not sufficient for most programming jobs.
Anyway, you could use this assignment also to check coding and documentation style, unless they just copy it directly from the internet (which you should be able to discover if you also know how to use google).