https://www.codechef.com/CCSTART2/problems/ADDNATRL/

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()
:roll_eyes:

sry, i had that but wrong pic got uploaded, but even after type casting its not getting submitted, showing wrong answer


same again, but when i use long int in c language its getting accepted. but not in python why???

:man_shrugging:
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 :sweat_smile:

in gfg compiler same code gives the expected output for 746096194, but different output in codechef compiler :upside_down_face:

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)
3 Likes

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.

2 Likes

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

1 Like