+ 2
In This code with the largest number show some error please help me to solve this
1 Answer
+ 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) )