Code for addition
Marko Rauhamaa
marko at pacujo.net
Mon Aug 7 16:59:48 EDT 2017
Neil Cerutti <neilc at norwich.edu>:
> On 2017-08-05, Ode Idoko via Python-list <python-list at python.org>
> wrote:
>> Can anyone help with the python code that can add 101, 102,
>> 103...2033 please? As I said before, I'm new to python and need
>> assistance in this regard. Thanks for always assisting.
>
>>>> (2033 - 101 + 1) * (101 + 2033) / 2
>
> You could also calculate it with a combination of sum and range
> builtins, as others have hinted, and if it's homework that's
> probably a good idea.
During college, one project was to develop a factorial subprogram using
the assembly language of an imaginary, 18-bit CPU. I told the student
who got the task that the only sane implementation would be a lookup
table with a possible range check as in:
def factorial(n):
return [1, 1, 2, 6, 24, 120, 720, 5040, 40320][n]
I believe he chickened out, though, and implemented a loop with
multiplications.
Marko
More information about the Python-list
mailing list