Re:Ask HN: Average coder at dead-end. What now?
by patio11 on Monday June 21, 2010 @01:37AM
(#1448081)
Let's see: x / y * y == x iff x % y == 0, if your language handles integer division like most do.
If your language doesn't do integer division that way, the naive approach is even easier if you know how to round: x / y == round(x / y) iff x % y == 0.
There are many, many other approaches which will work, too. I saw one guy hand-build an array of ints, initialize to zero, loop over it once with arr[i++] = 0; arr[i++] = 0; arr[i++] = 3 to set the multiples of 3, then do the same thing with the fives except checking to see if there was already a 3 there, then looping over the array a fourth time to handle the actual printing.
That is the kind of competent, worksmanlike programming that runs the world while the can't-do-FizzBuzz guys are hopefully not touching the code too much.
If your language doesn't do integer division that way, the naive approach is even easier if you know how to round: x / y == round(x / y) iff x % y == 0.
There are many, many other approaches which will work, too. I saw one guy hand-build an array of ints, initialize to zero, loop over it once with arr[i++] = 0; arr[i++] = 0; arr[i++] = 3 to set the multiples of 3, then do the same thing with the fives except checking to see if there was already a 3 there, then looping over the array a fourth time to handle the actual printing.
That is the kind of competent, worksmanlike programming that runs the world while the can't-do-FizzBuzz guys are hopefully not touching the code too much.