why this code gives wrong answersubmitted? but its working for custom inputs.
did i missed anything, need help with this.
n = int(input())
instead of n = input()
sry, i had that but wrong pic got uploaded, but even after type casting its not getting submitted, showing wrong answer
Hmm, Something always goes wrong
Try this test case:
746096194
Your Output:
278329765723690912
Expected Output
278329765723690915
where to see expected output?? and where am i going wrong
in gfg compiler same code gives the expected output for 746096194, but different output in codechef compiler
No, even GFG compiler gives the same output. (For 746096194
, I get 278329765723690912
).
I think the reason is loss of precision when using /
operator. The following code gives AC.
n = int(input())
print((n * (n + 1)) // 2)
Thanks for help
@suman_18733097 , I think ‘/’ operator does not lead loss of precision. It returns a float object. On the other hand, ‘//’ operator stand for floor division and returns int. Also, in @sohail7 's screenshot, (n+1)/2 will be evaluated first followed by multiplication due to higher precedence of ‘/’ operator over ‘*’ operator.
Everything’s fine, but it doesn’t state the reason for WA.
An entire document written on this: 15. Floating Point Arithmetic: Issues and Limitations — Python 3.11.3 documentation