[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Newfag here First time doing this Fibonacci thing, written in
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 18
Thread images: 2
File: Fibonacci.png (5 KB, 294x191) Image search: [Google]
Fibonacci.png
5 KB, 294x191
Newfag here

First time doing this Fibonacci thing, written in Python
Is there any hope for me? what should i improve? any advice (either tips, links, book names...)?
>>
>>51876298
>Python
>Is there any hope for me?

nope
>>
python is great, but '__init__' made me look towards rust and go
>>
Why would you write an iterative solution for fibonacci numbers just use the explicit formula
>>
>>51876370
there's nothing wrong that
it makes a nice clearly visible difference to methods that have special syntactic sugar like __init__, __dell__, __call__, __add__, ...
>>
>>51876298
>all that code
>actually setting a variable called "n"
>no recursion in fibonacci sequence
What are you doing?
Programming is something that involves logic, but I fail to see your logic in that code.
Explain your code to us, maybe you'll know what is wrong then.
>>
>>51876399
It's overly verbose. Why four underscores rather than one? Why not just reserve the words and let a syntax highlighting theme show them a different color?
>>
>>51876421
>no recursion in fibonacci sequence
you do realize that a iterative solution is more efficient, do you?

>inb4 tail recursion, that's basically just a more awkward iterative version
>>
>>51876298
How the fuck can people fail Fibonacci? It's Programming 101 shit
>>
>>51876447
>Why four underscores rather than one?
Used for something else already
https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles

>Why not just reserve the words and let a syntax highlighting theme show them a different color?
I'd say it's less universally clear. For example might show up red in one editor, and green in another.

When Python was invented, syntax highlighting was also not as commonplace.
>>
Instead of printing out each fib-value up to the Nth one, let the function return the Nth fib-value. This will make the fib-function more clean. If you wish to get all intermediate fib-values, then you could, to name a few examples, add an extra default parameter that checks if the function should return the Nth fib-value or some container of all fib-values up to N. You could also change the default parameter to represent some mutable container that if it is given it will after the function call contain all intermediate values
>>
matrix squaring is best

direct method is boring
>>
>>51876519
Now this is helpful, thanks.
>>
>>51876298
>>51876559
>not using the closed-form solution
You can derive it using linear algebra or using generating functions
>>
>>51876636
as I said it's boring
>>
>>51876464
Well yes, but seeing as OP is a beginner I would assume he should first write code that is a 1:1 implementation of the fibonacci sequence. If he knows wtf he's doing, he can start optimizing.
>>
>>51876697
In Python?
No he shouldn't.
Python's recursion depth is very limited.
>>
>>51876298
first time too
def fib (n):
if (n < 2): return n
if (n in ns): return ns[n]
ns[n]=(fib(n-1)+fib(n-2))
return ns[n]

ns = {}
fib(300)
print ns[300]
Thread replies: 18
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.