I tested myself with MATLAB (which makes it quite easy) out of some unnecessary curiosity—it took me about seven minutes, a fair part of which was debugging.
% FizzBuzz - print all numbers from 1 to 100, replacing multiples of 3 with
% "fizz", multiples of 5 with "buzz", and multiples of 3 and 5 with
% "fizzbuzz".
clear
clc
for i = 1:100
fb = '';
if length(find(factor(i)==3)) > 0
fb = [fb 'fizz'];
end
if length(find(factor(i)==5)) > 0
fb = [fb 'buzz'];
end
if length(fb) > 0
fprintf([fb '\n'])
else
fprintf('%5.0f\n', i)
end
end
A better program (by which I mean “faster”, not “clearer” or “easier to modify” or “easier to maintain”) would replace the tests with something less intensive—for example, incrementing two counters (one for 3 and one for 5) and zeroing them when they hit their respective desired factors.
I wouldn’t be; I’d take it as (anecdotal) evidence that the craft of programming is systematically undertaught. By which I mean, the tiny, nano-level rules of how best to interact with this strange medium that is code.
(Recently added to my growing backlog of possibly-top-level-post-worthy topics is “how and why programming may be a usefull skill for rationalists to pick up”...)
I have to admit, I was looking up functions in the docs, too—I would have been a bit faster working in pseudocode on paper.
Edit: Also, my training is in engineering, not comp. sci. - the programming curriculum at my school consists of one MATLAB course.
(Recently added to my growing backlog of possibly-top-level-post-worthy topics is “how and why programming may be a usefull skill for rationalists to pick up”...)
Querying my brain for cached thoughts:
Programming encourages clear thinking—like evolution, it is immune to rationalization.
Thinking in terms of algorithms, rather than problem-answer pairs, and the former generalize.
I tested myself with MATLAB (which makes it quite easy) out of some unnecessary curiosity—it took me about seven minutes, a fair part of which was debugging.
I feel rather ashamed of that, actually.
As everyone else seems to be posting their code:
A better program (by which I mean “faster”, not “clearer” or “easier to modify” or “easier to maintain”) would replace the tests with something less intensive—for example, incrementing two counters (one for 3 and one for 5) and zeroing them when they hit their respective desired factors.
I wouldn’t be; I’d take it as (anecdotal) evidence that the craft of programming is systematically undertaught. By which I mean, the tiny, nano-level rules of how best to interact with this strange medium that is code.
(Recently added to my growing backlog of possibly-top-level-post-worthy topics is “how and why programming may be a usefull skill for rationalists to pick up”...)
I have to admit, I was looking up functions in the docs, too—I would have been a bit faster working in pseudocode on paper.
Edit: Also, my training is in engineering, not comp. sci. - the programming curriculum at my school consists of one MATLAB course.
Querying my brain for cached thoughts:
Programming encourages clear thinking—like evolution, it is immune to rationalization.
Thinking in terms of algorithms, rather than problem-answer pairs, and the former generalize.