Serving the Quantitative Finance Community

• 1
• 2

mbunea
Topic Author
Posts: 112
Joined: March 26th, 2015, 11:39 am

### Stupid brainteaser

I stared at this for a long time and couldn't figure out what it does. Seems like a good problem to have each employee take and if they don't solve it, should be fired.int what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}

oas
Posts: 6
Joined: June 1st, 2012, 9:06 pm

### Stupid brainteaser

The person that should be fired is the one that wrote the code.

MHill
Posts: 489
Joined: February 26th, 2010, 11:32 pm

### Stupid brainteaser

Looked to me like it was trying to get month-end dates, but as I've never used C, I had to find out what the operators do. I'm still not clear on bits of it.m ^ 2 ? ... : ...seems to be the equivalent of 'if m=2, do the stuff after the colon, else do the stuff before the colon.'So if m (month) isn't February, return 31 - --m % 7 % 2'--m % 7 % 2' returns 1 if the month doesn't have 31 days (every second month has <31 days until you get to August, then you need every second month from July. This is the % 7 % 2 bit).So we return 31 where there are 31 days in the month, 30 otherwise.If m is February, return '29 - (y & 3 || y & 15 && !(y % 25))'I don't understand the operators here. I get that 'y & 3' returns 0 if the year is a multple of 4 (ie a leap year). I don't get the rest of it.

mbunea
Topic Author
Posts: 112
Joined: March 26th, 2015, 11:39 am

### Stupid brainteaser

I'm joking about firing, in case it isn't clear. This is a problem given in a local computer science journal, for high school kids. A workmate of mine who is training internship students presented them the problem and no one figured out what it does so he asked several colleagues, including me, to try solve it, so he can get an idea on how difficult it is. No one figured it out. Then he told us what if y were for year and m for month. Suddenly the purpose of the code became obvious.Now it seems almost embarassing how we coulnd't make the connection. There's 31 and 29 there also, they now seem like clues.

MHill
Posts: 489
Joined: February 26th, 2010, 11:32 pm

### Stupid brainteaser

Yeah, it was the 'm' and 'y' that made me guess it was a date function, and the 31 and 29 that it was for month ends. I did wonder briefly if it was for finding the last working day / Friday / Wednesday of the month, which is why I looked up C operators.I still don't get what the last bit of the 'February' code does. 'Y & 15' must return zero if the year is a multiple of 16. I don't see what use this is.I don't understand what 'NOT (y modulo 25)' means.And so I don't get what happens when you combine the three elements.Unless it's something to do with not having leap years on the turn of the century / millennium?(And is that really how 'millennium' is spelled?)

oas
Posts: 6
Joined: June 1st, 2012, 9:06 pm

### Stupid brainteaser

A nice explanation of the leap year part of the algorithm is here: http://stackoverflow.com/questions/3220 ... cally-in-c
Last edited by oas on April 21st, 2015, 10:00 pm, edited 1 time in total.

MHill
Posts: 489
Joined: February 26th, 2010, 11:32 pm

### Stupid brainteaser

Thanks! Would never have worked that one out.

katastrofa
Posts: 10249
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Stupid brainteaser

Quotem ^ 2 ? ... : ...seems to be the equivalent of 'if m=2, do the stuff after the colon, else do the stuff before the colon.'^ is bitwise XOR

ExSan
Posts: 4647
Joined: April 12th, 2003, 10:40 am

### Stupid brainteaser

QuoteOriginally posted by: mbuneaI stared at this for a long time and couldn't figure out what it does. Seems like a good problem to have each employee take and if they don't solve it, should be fired.int what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}and the brainteaser is ?

mbunea
Topic Author
Posts: 112
Joined: March 26th, 2015, 11:39 am

### Stupid brainteaser

Psychological question actually, check if person realizes y is for year and m for month.

katastrofa
Posts: 10249
Joined: August 16th, 2007, 5:36 am
Location: Alpha Centauri

### Stupid brainteaser

I thought m was for mother.

Cuchulainn
Posts: 64980
Joined: July 16th, 2004, 7:38 am
Location: Drosophila melanogaster
Contact:

### Stupid brainteaser

Quoteint what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}Too much whitespace.
Last edited by Cuchulainn on April 22nd, 2015, 10:00 pm, edited 1 time in total.
"Compatibility means deliberately repeating other people's mistakes."
David Wheeler

http://www.datasimfinancial.com
http://www.datasim.nl

daveangel
Posts: 17031
Joined: October 20th, 2003, 4:05 pm

### Stupid brainteaser

QuoteOriginally posted by: CuchulainnQuoteint what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}Too much whitespace.more obfuscation please and write it in FORTRAN
knowledge comes, wisdom lingers

Cuchulainn
Posts: 64980
Joined: July 16th, 2004, 7:38 am
Location: Drosophila melanogaster
Contact:

### Stupid brainteaser

QuoteOriginally posted by: daveangelQuoteOriginally posted by: CuchulainnQuoteint what(int y, int m) { return m ^ 2 ? 31 - --m % 7 % 2 : 29 - (y & 3 || y & 15 && !(y % 25));}Too much whitespace.more obfuscation please and write it in FORTRANFortran66 and punch cards, of course.
"Compatibility means deliberately repeating other people's mistakes."
David Wheeler

http://www.datasimfinancial.com
http://www.datasim.nl

DevonFangs
Posts: 3004
Joined: November 9th, 2009, 1:49 pm

### Stupid brainteaser

Here's a global pattern matcher:main(int c,char**v){return!m(v[1],v[2]);}m(char*s,char*t){return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}(hint)