Python Forum
List of square roots python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List of square roots python
#1
So the question is:

I have to do a little program where the function receives a argument x, who is an integer and returns a list of the sum of the first n square roots.

Like [sqrt(1), sqrt(1) + sqrt(2), sqrt(1) + sqrt(2) + sqrt(3), ....]

I've made this line of code but it is just returning the sum
 return sum([x**0.5 for x in range(n+1)])
Reply
#2
To get the cumulative sums of a list of length n:
[sum(the_list[:index]) for index in range(1, n+1)]
One problem is that your list is n+1 long. That's because you're including the square root of zero.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Apr-06-2017, 01:39 AM)ichabod801 Wrote: To get the cumulative sums of a list of length n:
[sum(the_list[:index]) for index in range(1, n+1)]
One problem is that your list is n+1 long. That's because you're including the square root of zero.

And what is 'the_list' ?
The only thing that the function receives is an integer number (n).
Reply
#4
It returns what you want.

Here is the sum of the list without any additional math:

>>> sum([x for x in range(11)])
55
Here is the sum of list's elements with x**0.5 applied on each of them
>>> sum([x**0.5 for x in range(11)])
22.4682781862041
Notice the result?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-08-2017, 08:33 PM)py7 Wrote: And what is 'the_list'

The list of square roots that you've already generated.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Apr-08-2017, 09:39 PM)ichabod801 Wrote: 1
[sum(the_list[:index]) for index in range(1, n+1)]

it only returns a list --> [0,0,0]
Reply
#7
Output:
>>> the_list = [1, 2, 3] >>> n = 3 >>> [sum(the_list[:index]) for index in range(1, n+1)] [1, 3, 6]
The cumulative sums of the items in the list. That's what you're homework is asking for, just with square roots, not integers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding square roots using long division. jahuja73 10 5,438 Feb-24-2021, 01:25 PM
Last Post: jahuja73
  Magic square! frequency 1 2,551 Dec-17-2018, 06:35 PM
Last Post: micseydel
  Square reverse sum(overloaded) shihomiyano 6 4,094 Aug-18-2018, 06:27 AM
Last Post: micseydel
  Perfect Square program forumer444 4 8,939 Sep-01-2017, 09:32 PM
Last Post: forumer444
  Magic Square Puzzle Harnick 1 4,883 Aug-09-2017, 04:51 PM
Last Post: nilamo
  Square Root on calculator MP1234593 1 7,952 Jun-06-2017, 06:58 PM
Last Post: nilamo
  Square root of a number mbestivert 1 4,059 Nov-24-2016, 04:35 PM
Last Post: casevh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020