Addition of numbers from a list
For this question I'm trying to add together integers from a list.
For example, if the inputted numbers are [2,4,6], it should output 34 because (1+2)+(1+2+3+4)+(1+2+3+4+5+6) = 34.
If the input is [9], it will output 45; (1+2+3+4+5+6+7+8+9) = 45.
Here's my code:
def additionOfList(st):
n = len(st)
total = 0
for i in range(n):
for k in range(1, n+1):
total += k
return total
Any help would be greatly appreciated; can't seem to figure this out.
python
add a comment |
For this question I'm trying to add together integers from a list.
For example, if the inputted numbers are [2,4,6], it should output 34 because (1+2)+(1+2+3+4)+(1+2+3+4+5+6) = 34.
If the input is [9], it will output 45; (1+2+3+4+5+6+7+8+9) = 45.
Here's my code:
def additionOfList(st):
n = len(st)
total = 0
for i in range(n):
for k in range(1, n+1):
total += k
return total
Any help would be greatly appreciated; can't seem to figure this out.
python
so you are summing the triangle number values of each integer?
– Martijn Pieters♦
Nov 20 '18 at 18:47
You are very close. You just need to change what you're iterating over in the outer loop! (n
is the same each time, andi
is not used)
– wim
Nov 20 '18 at 18:51
@wim what do you mean about the outer loop?
– Josephi Dmitry
Nov 20 '18 at 18:54
1
The loop overfor i in range(n)
is the outer loop, and the loop overfor k in range(1, n+1)
is the inner loop.
– wim
Nov 20 '18 at 18:56
add a comment |
For this question I'm trying to add together integers from a list.
For example, if the inputted numbers are [2,4,6], it should output 34 because (1+2)+(1+2+3+4)+(1+2+3+4+5+6) = 34.
If the input is [9], it will output 45; (1+2+3+4+5+6+7+8+9) = 45.
Here's my code:
def additionOfList(st):
n = len(st)
total = 0
for i in range(n):
for k in range(1, n+1):
total += k
return total
Any help would be greatly appreciated; can't seem to figure this out.
python
For this question I'm trying to add together integers from a list.
For example, if the inputted numbers are [2,4,6], it should output 34 because (1+2)+(1+2+3+4)+(1+2+3+4+5+6) = 34.
If the input is [9], it will output 45; (1+2+3+4+5+6+7+8+9) = 45.
Here's my code:
def additionOfList(st):
n = len(st)
total = 0
for i in range(n):
for k in range(1, n+1):
total += k
return total
Any help would be greatly appreciated; can't seem to figure this out.
python
python
edited Nov 20 '18 at 19:05
Filip Młynarski
1,5881311
1,5881311
asked Nov 20 '18 at 18:46
Josephi DmitryJosephi Dmitry
316
316
so you are summing the triangle number values of each integer?
– Martijn Pieters♦
Nov 20 '18 at 18:47
You are very close. You just need to change what you're iterating over in the outer loop! (n
is the same each time, andi
is not used)
– wim
Nov 20 '18 at 18:51
@wim what do you mean about the outer loop?
– Josephi Dmitry
Nov 20 '18 at 18:54
1
The loop overfor i in range(n)
is the outer loop, and the loop overfor k in range(1, n+1)
is the inner loop.
– wim
Nov 20 '18 at 18:56
add a comment |
so you are summing the triangle number values of each integer?
– Martijn Pieters♦
Nov 20 '18 at 18:47
You are very close. You just need to change what you're iterating over in the outer loop! (n
is the same each time, andi
is not used)
– wim
Nov 20 '18 at 18:51
@wim what do you mean about the outer loop?
– Josephi Dmitry
Nov 20 '18 at 18:54
1
The loop overfor i in range(n)
is the outer loop, and the loop overfor k in range(1, n+1)
is the inner loop.
– wim
Nov 20 '18 at 18:56
so you are summing the triangle number values of each integer?
– Martijn Pieters♦
Nov 20 '18 at 18:47
so you are summing the triangle number values of each integer?
– Martijn Pieters♦
Nov 20 '18 at 18:47
You are very close. You just need to change what you're iterating over in the outer loop! (
n
is the same each time, and i
is not used)– wim
Nov 20 '18 at 18:51
You are very close. You just need to change what you're iterating over in the outer loop! (
n
is the same each time, and i
is not used)– wim
Nov 20 '18 at 18:51
@wim what do you mean about the outer loop?
– Josephi Dmitry
Nov 20 '18 at 18:54
@wim what do you mean about the outer loop?
– Josephi Dmitry
Nov 20 '18 at 18:54
1
1
The loop over
for i in range(n)
is the outer loop, and the loop over for k in range(1, n+1)
is the inner loop.– wim
Nov 20 '18 at 18:56
The loop over
for i in range(n)
is the outer loop, and the loop over for k in range(1, n+1)
is the inner loop.– wim
Nov 20 '18 at 18:56
add a comment |
3 Answers
3
active
oldest
votes
l = [2,4,6]
total = 0
for i in l:
summation = (i * (i + 1)) / 2
total = total + summation
print(total) # 34
or... for the one-liner folks
print(sum([(i*(i+1))/2 for i in [2,4,6]]))
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
1
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
add a comment |
You are summing triangle numbers; just define a function that calculates the triangle number for each entry, then use sum()
to sum each result.
Triangle numbers can be calculated trivially with the formula (N * (N + 1)) / 2:
def triangle_number(n):
return (n * (n + 1)) // 2
def triangle_sum(l):
return sum(map(triangle_number, l))
Your own error is to use the length of the input list as n
; you are not calculating the triangle number of the length of the list; you'd want to use each individual number in st
as n
here:
def additionOfList(st):
total = 0
for n in st:
# calculate triangle number for n
However, whatever you do, do not just loop from 1 to n! That won't scale to large numbers, and is not needed here at all.
If you want to understand why triangle numbers can so easily be calculated, just write out the numbers on a line. Lets use n = 5:
1 2 3 4 5
Now write the same numbers in reverse underneath, and add up the columns:
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6
The sum is always 6. That's no coincidence. When you increase N, the sum is always going to be N + 1. Now add up the numbers on the bottom column, that's just 5 times 6, right?
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6 = 5 x 6 = 30
So the sum of all numbers from 1 to 5 plus the sum of numbers of 5 to 1, is 5 times 5 + 1. That's double from what you needed for 1 to 5 alone. Since it is doubled only because you also added 5 through to 1, you can divide by 2 to get the sum:
1 2 3 4 5 = 5 x 6 / 2 = 15
5 4 3 2 1 = 5 x 6 / 2 = 15
----------
6 6 6 6 6 = 5 x 6 = 30
Generalising that to any N, that makes:
triangle_number(N) = N * (N + 1) / 2
Since we can calculate that number for any N, it'll always be faster than manually adding up 1 + 2 + 3 + 4 + ... + N. Computers like that kind of trick.
You can ask Python to sum all the numbers from 1 through to N with:
sum(range(1, n + 1))
but if you compare that with the above triangle_number()
function, you'll find that it can take a long, long time when your n
value becomes big:
>>> def triangle_number(n):
... return (n * (n + 1)) // 2
...
>>> def sum_range(n):
... return sum(range(1, n + 1))
...
>>> triangle_number(5)
15
>>> sum_range(5)
15
>>> from timeit import timeit
>>> timeit('c(1000)', 'from __main__ import sum_range as c') # how long does it take to do this 1 million times
17.520909604994813
>>> timeit('c(1000)', 'from __main__ import triangle_number as c')
0.1906668500159867
1 million calculations of the triangle number for N = 1000 is really fast, but summing 1 through to 1000 takes 17.5 seconds.
2
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
1
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
add a comment |
def additionOfList(st):
return int(sum([(i**2)/2 + i/2 for i in st]))
print(additionOfList([2,4,6])) # -> 34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399555%2faddition-of-numbers-from-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
l = [2,4,6]
total = 0
for i in l:
summation = (i * (i + 1)) / 2
total = total + summation
print(total) # 34
or... for the one-liner folks
print(sum([(i*(i+1))/2 for i in [2,4,6]]))
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
1
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
add a comment |
l = [2,4,6]
total = 0
for i in l:
summation = (i * (i + 1)) / 2
total = total + summation
print(total) # 34
or... for the one-liner folks
print(sum([(i*(i+1))/2 for i in [2,4,6]]))
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
1
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
add a comment |
l = [2,4,6]
total = 0
for i in l:
summation = (i * (i + 1)) / 2
total = total + summation
print(total) # 34
or... for the one-liner folks
print(sum([(i*(i+1))/2 for i in [2,4,6]]))
l = [2,4,6]
total = 0
for i in l:
summation = (i * (i + 1)) / 2
total = total + summation
print(total) # 34
or... for the one-liner folks
print(sum([(i*(i+1))/2 for i in [2,4,6]]))
answered Nov 20 '18 at 18:52
ConnerConner
23.2k84568
23.2k84568
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
1
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
add a comment |
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
1
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
Thanks, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:04
1
1
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
@JosephiDmitry If it helped you can use the check mark to designate an accepted answer
– Conner
Nov 20 '18 at 19:04
add a comment |
You are summing triangle numbers; just define a function that calculates the triangle number for each entry, then use sum()
to sum each result.
Triangle numbers can be calculated trivially with the formula (N * (N + 1)) / 2:
def triangle_number(n):
return (n * (n + 1)) // 2
def triangle_sum(l):
return sum(map(triangle_number, l))
Your own error is to use the length of the input list as n
; you are not calculating the triangle number of the length of the list; you'd want to use each individual number in st
as n
here:
def additionOfList(st):
total = 0
for n in st:
# calculate triangle number for n
However, whatever you do, do not just loop from 1 to n! That won't scale to large numbers, and is not needed here at all.
If you want to understand why triangle numbers can so easily be calculated, just write out the numbers on a line. Lets use n = 5:
1 2 3 4 5
Now write the same numbers in reverse underneath, and add up the columns:
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6
The sum is always 6. That's no coincidence. When you increase N, the sum is always going to be N + 1. Now add up the numbers on the bottom column, that's just 5 times 6, right?
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6 = 5 x 6 = 30
So the sum of all numbers from 1 to 5 plus the sum of numbers of 5 to 1, is 5 times 5 + 1. That's double from what you needed for 1 to 5 alone. Since it is doubled only because you also added 5 through to 1, you can divide by 2 to get the sum:
1 2 3 4 5 = 5 x 6 / 2 = 15
5 4 3 2 1 = 5 x 6 / 2 = 15
----------
6 6 6 6 6 = 5 x 6 = 30
Generalising that to any N, that makes:
triangle_number(N) = N * (N + 1) / 2
Since we can calculate that number for any N, it'll always be faster than manually adding up 1 + 2 + 3 + 4 + ... + N. Computers like that kind of trick.
You can ask Python to sum all the numbers from 1 through to N with:
sum(range(1, n + 1))
but if you compare that with the above triangle_number()
function, you'll find that it can take a long, long time when your n
value becomes big:
>>> def triangle_number(n):
... return (n * (n + 1)) // 2
...
>>> def sum_range(n):
... return sum(range(1, n + 1))
...
>>> triangle_number(5)
15
>>> sum_range(5)
15
>>> from timeit import timeit
>>> timeit('c(1000)', 'from __main__ import sum_range as c') # how long does it take to do this 1 million times
17.520909604994813
>>> timeit('c(1000)', 'from __main__ import triangle_number as c')
0.1906668500159867
1 million calculations of the triangle number for N = 1000 is really fast, but summing 1 through to 1000 takes 17.5 seconds.
2
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
1
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
add a comment |
You are summing triangle numbers; just define a function that calculates the triangle number for each entry, then use sum()
to sum each result.
Triangle numbers can be calculated trivially with the formula (N * (N + 1)) / 2:
def triangle_number(n):
return (n * (n + 1)) // 2
def triangle_sum(l):
return sum(map(triangle_number, l))
Your own error is to use the length of the input list as n
; you are not calculating the triangle number of the length of the list; you'd want to use each individual number in st
as n
here:
def additionOfList(st):
total = 0
for n in st:
# calculate triangle number for n
However, whatever you do, do not just loop from 1 to n! That won't scale to large numbers, and is not needed here at all.
If you want to understand why triangle numbers can so easily be calculated, just write out the numbers on a line. Lets use n = 5:
1 2 3 4 5
Now write the same numbers in reverse underneath, and add up the columns:
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6
The sum is always 6. That's no coincidence. When you increase N, the sum is always going to be N + 1. Now add up the numbers on the bottom column, that's just 5 times 6, right?
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6 = 5 x 6 = 30
So the sum of all numbers from 1 to 5 plus the sum of numbers of 5 to 1, is 5 times 5 + 1. That's double from what you needed for 1 to 5 alone. Since it is doubled only because you also added 5 through to 1, you can divide by 2 to get the sum:
1 2 3 4 5 = 5 x 6 / 2 = 15
5 4 3 2 1 = 5 x 6 / 2 = 15
----------
6 6 6 6 6 = 5 x 6 = 30
Generalising that to any N, that makes:
triangle_number(N) = N * (N + 1) / 2
Since we can calculate that number for any N, it'll always be faster than manually adding up 1 + 2 + 3 + 4 + ... + N. Computers like that kind of trick.
You can ask Python to sum all the numbers from 1 through to N with:
sum(range(1, n + 1))
but if you compare that with the above triangle_number()
function, you'll find that it can take a long, long time when your n
value becomes big:
>>> def triangle_number(n):
... return (n * (n + 1)) // 2
...
>>> def sum_range(n):
... return sum(range(1, n + 1))
...
>>> triangle_number(5)
15
>>> sum_range(5)
15
>>> from timeit import timeit
>>> timeit('c(1000)', 'from __main__ import sum_range as c') # how long does it take to do this 1 million times
17.520909604994813
>>> timeit('c(1000)', 'from __main__ import triangle_number as c')
0.1906668500159867
1 million calculations of the triangle number for N = 1000 is really fast, but summing 1 through to 1000 takes 17.5 seconds.
2
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
1
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
add a comment |
You are summing triangle numbers; just define a function that calculates the triangle number for each entry, then use sum()
to sum each result.
Triangle numbers can be calculated trivially with the formula (N * (N + 1)) / 2:
def triangle_number(n):
return (n * (n + 1)) // 2
def triangle_sum(l):
return sum(map(triangle_number, l))
Your own error is to use the length of the input list as n
; you are not calculating the triangle number of the length of the list; you'd want to use each individual number in st
as n
here:
def additionOfList(st):
total = 0
for n in st:
# calculate triangle number for n
However, whatever you do, do not just loop from 1 to n! That won't scale to large numbers, and is not needed here at all.
If you want to understand why triangle numbers can so easily be calculated, just write out the numbers on a line. Lets use n = 5:
1 2 3 4 5
Now write the same numbers in reverse underneath, and add up the columns:
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6
The sum is always 6. That's no coincidence. When you increase N, the sum is always going to be N + 1. Now add up the numbers on the bottom column, that's just 5 times 6, right?
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6 = 5 x 6 = 30
So the sum of all numbers from 1 to 5 plus the sum of numbers of 5 to 1, is 5 times 5 + 1. That's double from what you needed for 1 to 5 alone. Since it is doubled only because you also added 5 through to 1, you can divide by 2 to get the sum:
1 2 3 4 5 = 5 x 6 / 2 = 15
5 4 3 2 1 = 5 x 6 / 2 = 15
----------
6 6 6 6 6 = 5 x 6 = 30
Generalising that to any N, that makes:
triangle_number(N) = N * (N + 1) / 2
Since we can calculate that number for any N, it'll always be faster than manually adding up 1 + 2 + 3 + 4 + ... + N. Computers like that kind of trick.
You can ask Python to sum all the numbers from 1 through to N with:
sum(range(1, n + 1))
but if you compare that with the above triangle_number()
function, you'll find that it can take a long, long time when your n
value becomes big:
>>> def triangle_number(n):
... return (n * (n + 1)) // 2
...
>>> def sum_range(n):
... return sum(range(1, n + 1))
...
>>> triangle_number(5)
15
>>> sum_range(5)
15
>>> from timeit import timeit
>>> timeit('c(1000)', 'from __main__ import sum_range as c') # how long does it take to do this 1 million times
17.520909604994813
>>> timeit('c(1000)', 'from __main__ import triangle_number as c')
0.1906668500159867
1 million calculations of the triangle number for N = 1000 is really fast, but summing 1 through to 1000 takes 17.5 seconds.
You are summing triangle numbers; just define a function that calculates the triangle number for each entry, then use sum()
to sum each result.
Triangle numbers can be calculated trivially with the formula (N * (N + 1)) / 2:
def triangle_number(n):
return (n * (n + 1)) // 2
def triangle_sum(l):
return sum(map(triangle_number, l))
Your own error is to use the length of the input list as n
; you are not calculating the triangle number of the length of the list; you'd want to use each individual number in st
as n
here:
def additionOfList(st):
total = 0
for n in st:
# calculate triangle number for n
However, whatever you do, do not just loop from 1 to n! That won't scale to large numbers, and is not needed here at all.
If you want to understand why triangle numbers can so easily be calculated, just write out the numbers on a line. Lets use n = 5:
1 2 3 4 5
Now write the same numbers in reverse underneath, and add up the columns:
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6
The sum is always 6. That's no coincidence. When you increase N, the sum is always going to be N + 1. Now add up the numbers on the bottom column, that's just 5 times 6, right?
1 2 3 4 5
5 4 3 2 1 +
----------
6 6 6 6 6 = 5 x 6 = 30
So the sum of all numbers from 1 to 5 plus the sum of numbers of 5 to 1, is 5 times 5 + 1. That's double from what you needed for 1 to 5 alone. Since it is doubled only because you also added 5 through to 1, you can divide by 2 to get the sum:
1 2 3 4 5 = 5 x 6 / 2 = 15
5 4 3 2 1 = 5 x 6 / 2 = 15
----------
6 6 6 6 6 = 5 x 6 = 30
Generalising that to any N, that makes:
triangle_number(N) = N * (N + 1) / 2
Since we can calculate that number for any N, it'll always be faster than manually adding up 1 + 2 + 3 + 4 + ... + N. Computers like that kind of trick.
You can ask Python to sum all the numbers from 1 through to N with:
sum(range(1, n + 1))
but if you compare that with the above triangle_number()
function, you'll find that it can take a long, long time when your n
value becomes big:
>>> def triangle_number(n):
... return (n * (n + 1)) // 2
...
>>> def sum_range(n):
... return sum(range(1, n + 1))
...
>>> triangle_number(5)
15
>>> sum_range(5)
15
>>> from timeit import timeit
>>> timeit('c(1000)', 'from __main__ import sum_range as c') # how long does it take to do this 1 million times
17.520909604994813
>>> timeit('c(1000)', 'from __main__ import triangle_number as c')
0.1906668500159867
1 million calculations of the triangle number for N = 1000 is really fast, but summing 1 through to 1000 takes 17.5 seconds.
edited Nov 20 '18 at 21:45
answered Nov 20 '18 at 18:51
Martijn Pieters♦Martijn Pieters
703k13324422276
703k13324422276
2
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
1
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
add a comment |
2
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
1
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
2
2
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
I'm not sure providing a closed form solution which completely departs from the OP's original attempt is the best approach here.
– wim
Nov 20 '18 at 18:52
1
1
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Never knew it has its own name, great answer +1
– Filip Młynarski
Nov 20 '18 at 18:55
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
Thank you very much, this helped out!
– Josephi Dmitry
Nov 20 '18 at 19:03
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
@JosephiDmitry: Glad to have been of help! Feel free to accept one of the answers if you feel it was useful to you. :-) You can only pick one, the choice is yours. The idea is that you pick the one you feel helped you the most. There is no requirement that you select any, not accepting an answer is also allowed.
– Martijn Pieters♦
Nov 20 '18 at 19:06
add a comment |
def additionOfList(st):
return int(sum([(i**2)/2 + i/2 for i in st]))
print(additionOfList([2,4,6])) # -> 34
add a comment |
def additionOfList(st):
return int(sum([(i**2)/2 + i/2 for i in st]))
print(additionOfList([2,4,6])) # -> 34
add a comment |
def additionOfList(st):
return int(sum([(i**2)/2 + i/2 for i in st]))
print(additionOfList([2,4,6])) # -> 34
def additionOfList(st):
return int(sum([(i**2)/2 + i/2 for i in st]))
print(additionOfList([2,4,6])) # -> 34
answered Nov 20 '18 at 18:52
Filip MłynarskiFilip Młynarski
1,5881311
1,5881311
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399555%2faddition-of-numbers-from-a-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
so you are summing the triangle number values of each integer?
– Martijn Pieters♦
Nov 20 '18 at 18:47
You are very close. You just need to change what you're iterating over in the outer loop! (
n
is the same each time, andi
is not used)– wim
Nov 20 '18 at 18:51
@wim what do you mean about the outer loop?
– Josephi Dmitry
Nov 20 '18 at 18:54
1
The loop over
for i in range(n)
is the outer loop, and the loop overfor k in range(1, n+1)
is the inner loop.– wim
Nov 20 '18 at 18:56