(If you're not interested in computer science subtleties, please ignore this node - for normal programming tasks the O(1) estimate is good).
For big numbers the algorithm is slower than O(n), while the summing all the values is O(2^n).
In computer science, if you are talking about O(f(n)), n is the input length, not the number that the input presents.
So adding two numbers with length n is O(n), multiplying two numbers naively is O(n^2), there are a few more elaborate algorithms that do it a bit faster.
Summing all numbers from 1 to $number is O(2^n), because the length of $number is what we call n.
(This calculation kinda assumes use bigint;, because otherwise on the perl vm adding and multiplying is O(1) indeed)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.