+ 2

how to fix loop for Square Numbers?

prints 2 ^ 2 = 9 i know sound's crazy but please check the code 👀 https://code.sololearn.com/c5CzsSp8enpf/?ref=app

21st Apr 2022, 1:30 PM
mohammed shibli
mohammed shibli - avatar
6 Antworten
+ 3
thank you John Wells i appreciate your assistance 🙏
21st Apr 2022, 2:13 PM
mohammed shibli
mohammed shibli - avatar
+ 2
n = 2 n3 = 3**2 = n**2 + (n + n + 1) = 2**2 + (2 + 2 +1) = 4 + 5 = 9
21st Apr 2022, 1:52 PM
John Wells
John Wells - avatar
+ 2
thanks john but the real problem at output it prints 3**2 ==16
21st Apr 2022, 1:57 PM
mohammed shibli
mohammed shibli - avatar
+ 2
Because you are using 3 to calculate 3 instead of 2 to calculate 3. Add the n+1 to the print because it belongs there
21st Apr 2022, 1:59 PM
John Wells
John Wells - avatar
+ 2
Everyone of these has n+1 = target square n = 2 n3 = 3**2 = n**2 + (n + n + 1) n = 3 n4 = 4**2 = n3 + (n + n + 1) n = 4 n5 = 5**2 = n4 + (n + n + 1)
21st Apr 2022, 2:06 PM
John Wells
John Wells - avatar
+ 1
n = *** *** *** = ++o **+ **+ = (n-1) ^2 ->* + 2(n-1) -> + +1 -> o But instead of n-1 u used n.
21st Apr 2022, 8:46 PM
Oma Falk
Oma Falk - avatar