Hacker News new | past | comments | ask | show | jobs | submit login

I worked at a hedge fund where one of the phone-screen brainteasers was "How many zeros are at the end of 35!?"

The very not-smart people would try to use Excel for this, and get hilariously (and obviously) incorrect results due to rounding errors.




Took me a few seconds to parse the question (that it was about "35 factorial" and not about some binary representation of "35" ended with interrobang "?!")

One possible solution in Python:

  import operator
  fact = str(reduce(operator.mul, xrange(2,36)))
  print len(fact) - len(fact.rstrip('0'))

  >>> 8
Please forgive me for using strings for numerical operation, it was for a brevity's sake (I still remember Leah Culver getting chastised here on HN).


Took me a while to figure out where the "extra" 8th zero came out.... namely 25. That there are at least 7 zeros is evident, since 5 and 2 is needed for each 0, and 5s are more scarce than 2s in 35!


Prime p (e.g. 5) occurs exactly k times in factors of n! .

k being a sum from 1 to m of a sequence whose term is [n / p^m] ([] meaning taking inteer part) where m is such that p^m =< n that is m being the integer part of logarithm base p of n.

In this example

  [log5(35)] = 2
so

  k = [35/5^1] + [35/5^2]
Sory for bump-posting, but I had perfectly nothing better to do.


Aha, so that's the elegant way how this was supposed to be solved (in your head and on the spot).


It's basically an honesty test, since the interviewer explicitly says not to use paper or a calculator (which would include Excel). Quants are given harder questions, and I doubt that a trader would be dinged for not knowing the correct approach, since it's not a brainteaser that would correlate highly to trading acumen. But the Excel answer (14, I believe) is a very wrong one.


Interesting, 35! according to various tools:

  10333147966386100000000000000000000000000 (Open Office Calc)
  10333147966386100000000000000000000000000 (Excel)
  10333147966386144000000000000000000000000 (Gnumeric)
  10333147966386145000000000000000000000000 (Windows Calculator)
  10333147966386144929666651337523200000000 (Python)


Python is correct. It's promoting to bignum, while the others are using floats, presumably, and passing them off as integers. I think they ought to display 1.0333147966386144e40.

Your average wannabe-banker/Wharton undergrad has never heard of Python, however, but has used Excel. Quants are familiar with Python, but they get harder problems.

Most quants don't, however, get to use Python. For some inexplicable reason, a lot of them are mired in C++, of all languages, and tend to be poor-to-mediocre programmers. There are exceptions, though; the one I worked at used an FP language and had excellent programmers.


I think they ought to display 1.0333147966386144e40.

Actually, they did show the result by default as floats in scientific notation, it was me who changed number display options for easier visual comparison.

I assumed quants preferred C++ because of performance. When I occasionally have to go back from Python to C/C++, I'm surprised much faster it is.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: