+ 2

In This code with the largest number show some error please help me to solve this

https://code.sololearn.com/c0Y2Ih26bIhp/?ref=app

2nd Dec 2022, 4:33 AM
Shailesh Kewat
Shailesh Kewat - avatar
1 Resposta
+ 3
The last loop takes much time so it execution time out. Instead of these line or inplace of loop , use formula : n * (n+1) /2 some of n concecutive numbers So instead of nums = (range(1,y+1)) sum_nums = 0 for num in nums: sum_nums = sum_nums + num print(f'Sum = {sum_nums}',"\n") Replace sum_nums = y*(y+1)//2 print(f'Sum = {sum_nums}') Next why you need to print n numbers? It's again using loop so time out happens for big numbers.. Instead try to print in a list if you want. So replace this : for x in range(1,y+1): print("Count :-", x) With print( *range(1, y+1) )
2nd Dec 2022, 10:11 AM
Jayakrishna 🇮🇳