Work
My personal assessment of my own code, once I had the eyes to make that kind of analysis, is that my code tends to be more utilitarian than artistic. I've seen things I have done redone now and again to be prettier, but not with any better functionality.
"Be bold, and mighty forces will come to your aid."
Not sure of the exact syntax of the language you are using, but I think this would be close to maximum efficiency (please excuse my pseudocode):
public function FizzBuzzLoop(int i)
{
str thistime = null;
if (i % 3 == 0) { thistime = "Fizz"; }
if (i % 5 == 0) { thistime = thistime + "Buzz"; }
if (thistime == null) { Console.WriteLine(i.ToString()); }
else { Console.WriteLine(thistime); }
if (i < 101) { FizzBuzzLoop(i+1); }
}
One less evaluation and it swaps in recursion in place of a while loop.
public function FizzBuzzLoop(int i)
{
str thistime = null;
if (i % 3 == 0) { thistime = "Fizz"; }
if (i % 5 == 0) { thistime = thistime + "Buzz"; }
if (thistime == null) { Console.WriteLine(i.ToString()); }
else { Console.WriteLine(thistime); }
if (i < 101) { FizzBuzzLoop(i+1); }
}
One less evaluation and it swaps in recursion in place of a while loop.
"ATTENTION: Customers browsing porn must hold magazines with both hands at all times!"
After debugging other people's code enough, I place readability on a very high level.GORDON wrote:My personal assessment of my own code, once I had the eyes to make that kind of analysis, is that my code tends to be more utilitarian than artistic. I've seen things I have done redone now and again to be prettier, but not with any better functionality.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
The recursion is unnecessary. And inefficient, as you will be increasing your memory allocation for the variables you create with each call, that won't go out of scope since your calls are now nested.TPRJones wrote:Not sure of the exact syntax of the language you are using, but I think this would be close to maximum efficiency (please excuse my pseudocode):
public function FizzBuzzLoop(int i)
{
str thistime = null;
if (i % 3 == 0) { thistime = "Fizz"; }
if (i % 5 == 0) { thistime = thistime + "Buzz"; }
if (thistime == null) { Console.WriteLine(i.ToString()); }
else { Console.WriteLine(thistime); }
if (i < 101) { FizzBuzzLoop(i+1); }
}
One less evaluation and it swaps in recursion in place of a while loop.
It's not me, it's someone else.
-
thibodeaux
- Posts: 8121
- Joined: Thu May 20, 2004 7:32 pm
TheCatt wrote:TPRJones wrote:Oh. Well, nevermind then.
Need a better language. Recursion is too awesome to sacrifice to bad memory management.
I'm not sure what language that would be possible in.
That Fibonacci solution for recursion is nice.
Inefficient as hell, too.
Some LISPs have "tail recursion" which can be implemented as a GOTO. No memory problems.
TPR's solution looks tail-recursive (iterative). Run-time is still big-omega(N). In fact, outside of butchering the logic inside the function, it'd be hard to make this recursive in a dangerous way. It's the opposite of the Fibonacci, which is (relatively) difficult to do non-recursively. Someone today, with some hints, finally got the good solution. First one this round.
Edited By Malcolm on 1407195179
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
GORDON wrote:So is there some rule in fizzbuzz that says I can't just write a single line of code: print " 1 2 fizz 4 buzz..." Etc?
I mean, as long as we are thinking outside the box. Did anyone say it had to be overly complicated?
Works fine until you print from 1 to N.
EDIT: Contractor managed to renege on his acceptance. Fucking recruiting firms and their visa bullshit.
Edited By Malcolm on 1407517648
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."