3 ms·
When I was a bored and under-challenged student in the early days of university, one of my tests during a programming exam was to write a program that spit out
by shrikant 4y ago
When I was a bored and under-challenged student in the early days of university, one of my tests during a programming exam was to write a program that spit out the factorial of an input integer.
For shits and giggles, I just just wrote a series of `if...then` statements for 0 to 5, and only actually calculated the factorial for inputs >=6. I passed after the examiner just input 3 and 5 and was satisfied that the output was correct. Oops.
- WalterBright 4y agoDoing a table lookup for common cases and computing for the less common ones is perfectly valid!
- Jeff_Brown 4y agoAnd for some problems it's even efficient. Not factorial, though.
- Jeff_Brown 4y agoOh you're right, hive mind, it's efficient. Sorry.
- dxbydt 4y agoNot 100% sure but I believe this is how we landed the lunar module on the moon the first time...tan/arctan/both were too hard to compute on the processors of those days so they discretized into half angles & stored the tangents in a lookup table.
- hgsgm 4y agoThat's how early calculators worked, and some fast math libraries today too.
- speed_spread 4y agoThe Pentium FDIV bug was due to a bad value in a lookup table.
- thaw13579 4y agoThis is the way. Next, you could cache those values for >=6 when you compute them and also use the previously cached values for sub-problem solutions.