Help me to code it | SoloLearn: Learn to code for FREE!

+5

Help me to code it

x = (r+7)**2 r is in range (1,18) Find the sum of all the values of x. For e.g. if r is 1, x=64 If r is 2, x = 81 So I have to find 64+81+...

7/7/2020 6:19:01 AM

Khushan Chauhan

23 Answers

New Answer

+15

print(sum([(x+7)**2 for x in range(1,18)])) You can write like this in only one line code

+5

So basically, you want to just put this in code playground and run it?

+3

Khushan x=0 for r in range (1,18): r = (r+7)**2 x+=r print(x) Tomiwa Joseph don't suggest to use a Function name as identifier Ex. sum

+2

Yeah 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 my bad

+1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I wanted him to figure that out all by himself 😃😃😃

+1

Here is a function that will calculate the question. I have included all arguments so you can use it flexibly. def square_sum(range_start, range_end, exponential_power, add_num): x = 0 for r in range(range_start, range_end): x = x + ((r + add_num)**2) print(x) square_sum(1, 8, 2, 7) >>> 875 #for range (1, 2, 3, 4, 5, 6, 7), power raise to 2, the number added to r, 7 in your case square_sum(1, 18, 2, 7) >>> 4760

0

Answer Find all the answer in range of 1 to 18 and then add all the required number So the code is X= (1+7)**2 = 64 ..... .... .... .... ... ... .... .... .. ... ... ... ... .... ... .... X=(18+7)**2 = 15625 Print X+X+....+....+......+....+....+.....+...+.....+....+...+X

0

Harshit Singh I appreciate your efforts bt I think you should learn mathematics too with programming..

0

Sum=0 >>> for r in range(1,18,1): Sum=Sum+((r+7)**2) >>> print(Sum)

-1

-1

Khushan it looks like assignment , show your attempt for further help

-1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Kiibo Ghayal it's not an assignment I was doing mathematics and this was a calculation so I thought let's use my Python knowledge to get an answer instead of that calci. I was able to get the square bt can't add them up.

-1

Khushan I want to see how you have used your Python knowledge.So share your attempt here

-1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Oh.. Pls don't laugh I don't have any computer background and everything I have learned is using SoloLearn. Here's the code I did. for r in range in (1,18): r = (r+7)**2 x = r+r print(x) It gives me square and don't add them.

-1

Khushan here is a hint: addition = 0 for q in range(1,18): x = (q+7)**2 .....find a way to add x to addition😃 Hope this helps.

-1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I also try this it won't work for me.

-1

Khushan Save this code in code playground and click on run button If it won't work, share its link here

-1

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I got my error. TY

-1

Khushan check this list out: print([(x+7)**2 for x in range(1,18)]) If this are the numbers you want to add, add them. 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I normally wouldn't write for loops like that, that's why I made that simple mistake. Comprehensions made me lazy😃😅

-1

Tomiwa Joseph can u read mind? I was just trying to convert it in list so I could use sum function and u did it already. TY