School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,136 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,903 people online right now. Registration is fast and FREE... Join Now!




Perfect Squares

 

Perfect Squares, Sample Excercise

EthanR

10 Sep, 2009 - 02:08 PM
Post #1

New D.I.C Head
*

Joined: 19 Mar, 2009
Posts: 12

Assume there is a variable, h already associated with a positive integer value. Write the code necessary to compute the sum of the first h perfect squares, starting with 1 . (A perfect square is an integer like 9 , 16 , 25 , 36 that is equal to the square of another integer (in this case 3*3 , 4*4 , 5*5 , 6*6 respectively).) Associate the sum you compute with the variable q . For example, if h is 4 , you would assign 30 to q because the first 4 perfect squares (starting with 1 ) are: 1 , 4 , 9 , 16 and 30==1+4+9+16 .

For whatever reason I am having a difficult time solving this problem this is what I have but it is not correct.

q = sum(range(1,h+1,1)**2)



User is offlineProfile CardPM
+Quote Post


shadhin

RE: Perfect Squares

10 Sep, 2009 - 02:49 PM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Sep, 2008
Posts: 434



Thanked: 19 times
Dream Kudos: 3650
My Contributions
Try this-
CODE

q=0
for i in range(1,h+1):
    q +=i**2

User is offlineProfile CardPM
+Quote Post

reyno2ac

RE: Perfect Squares

10 Sep, 2009 - 04:59 PM
Post #3

New D.I.C Head
*

Joined: 10 Sep, 2009
Posts: 4


My Contributions
I just made a list of all the perfect squares and then set q = to the sum of that list up to h.

CODE
perfect_square = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225]

q = sum(perfect_square[:h+1])


This post has been edited by reyno2ac: 10 Sep, 2009 - 05:00 PM
User is offlineProfile CardPM
+Quote Post

Nallo

RE: Perfect Squares

11 Sep, 2009 - 09:51 AM
Post #4

New D.I.C Head
*

Joined: 19 Jul, 2009
Posts: 24



Thanked: 3 times
My Contributions
Hello Ethan,
QUOTE(EthanR @ 10 Sep, 2009 - 02:08 PM) *
q = sum(range(1,h+1,1)**2)


This doesnt work, as range is a list (in Python 2.x, in Python 3.x it is a range object) and Python doesnt know what list ** 2 shall mean.
What you want is to put the elements of the list to power 2, so to write it in one line using list comprehension:

CODE
q = sum([i ** 2 for i in range(1, h + 1)])


or more elegantly using generator expression (Python 2.4(? not sure if it was 2.4) or later:
CODE
#in Python 2.x
q = sum(i ** 2 for i in xrange(1, h+1))
#in Python 3.x
q = sum(i ** 2 for i in range(1, h+1))

This is better than the above version, as the list is not stored in memory (for long lists that matters) before sum is called. Its elements are generated lazily, meaning only once they are needed.

This post has been edited by Nallo: 11 Sep, 2009 - 09:55 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:01PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month