User talk:Jaigoda/Programming

"else if" is redundant :D-- Relyk 03:13, 18 October 2011 (UTC)
 * Point taken. I think in another slightly different version I had something that actually used "else" which meant the use of "elif". Or something. -- Jai . -  03:42, October 18 2011 (UTC)
 * Yeah you can just use if..else by mutual exclusion. Also your double print(n) step is unnecessary, just do it at the end. Vorpal [[Image:Vorpal_Signature_1.jpg|19px]] 12:28, 18 October 2011 (UTC)
 * Oh and you get bonus interwebz for making a recursive version! =P Vorpal [[Image:Vorpal_Signature_1.jpg|19px]] 22:48, 18 October 2011 (UTC)
 * recursion is fun, might be a good exercise, although loops are generally faster for these types of problems. I don't see how you are getting run times of 15-30 seconds, that loop should be finished in a blink an eye. Printing every iteration is probably what is killing your speed here. The problem itself doesn't grow multiplicatively like you said, but you are probably multiplicatively creating more work for the output buffer.  Maybe try adding the string to a string, and print the result string once the loop finishes.  I came across the Collatz problem before, your best bet is to cache values in a look up table (array), if you think about it, once you hit a number which you have already calculated, you don't really need to do the work to calculate it again.Smitysmitehex.png Smity Smitington !!   on 00:49, May 3 2012 (UTC)
 * I think you missed the part where I said I have no clue what I'm doing. And yes, if you're not printing out every number it takes like 1 second, but then you get a hundred or a thousand ones. The rest mostly went over my head, though I might be able to add it to a string. Wouldn't you use append? -- Jai .  -  01:00, May 3 2012 (UTC)
 * Yep, append appears to be what you want. I only wrote one python program in my life, but from what I found, append seems pretty effecient. Smitysmitehex.png Smity Smitington !!   on 01:26, May 3 2012 (UTC)

i=10 nMinusOne = 0 nMinusTwo = 1 print "First " + str(i) + " numbers of the Fibonacci Sequence:" out = str(nMinusOne) + ", " + str(nMinusTwo) while i >= 1: n = nMinusOne + nMinusTwo out += ", " + str(n) nMinusTwo = nMinusOne nMinusOne = n   i = i - 1 print out fencepost it.-- Relyk 02:08, 3 May 2012 (UTC)
 * that's the idea. I think you might be better off using append from the sounds of it.  Every time you do a string concatenation like that, you are creating a new string object, so the previous string becomes an orphan, giving some work to the garbage collector.  append is still going to have to reallocate/grow the strings buffer, so the best solution would be to preallocate the buffer for as large as you need it, not sure how you would do that in python though. Smitysmitehex.png Smity Smitington !!   on 02:54, May 3 2012 (UTC)
 * the "+=" part might be fine, assuming the interpreter is smart enough to convert that to append, but the ", " + str(n) is what might create a new string. Smitysmitehex.png Smity Smitington !!   on 03:03, May 3 2012 (UTC)
 * Smity, you're making it more complicated than it should be-- Relyk 04:01, 3 May 2012 (UTC)
 * You'll thank me someday. Smitysmitehex.png Smity Smitington !!  on 04:35, May 3 2012 (UTC)
 * When I need to learn about implementing buffer variables in my code, I will personally come over to your house and give you a cookie as a thank you.-- Relyk 07:47, 3 May 2012 (UTC)
 * I'ma hold you to that, you know. -- Jai .  -  13:19, May 3 2012 (UTC)