I'm to trying to calculate the sum of cubes as part of a bigger problem, and i have got stuck at this point. In the below written code (two variations of same function), when I pass a large value such as 45001
, i get conflicting results in both the functions such as
import math
def cube_sum(n):
return int(math.pow((n*(n+1)/2),2));
def cube_sum1(n):
if n%2 != 0: return int(math.pow(n*((n+1)/2),2))
else: return int(math.pow((n/2)*(n+1),2));
def calc:
print('1:',cube_sum(45001));
print('2:',cube_sum1(45001));
print('3:',(int(45001*45002/2)**2)); # gives the correct answer
Even though all the print values should be same(apparently), i get the output as
1: 1025292944081384960
2: 1025292944081384960
3: 1025292944081385001
while trying to analyse, i stumbled upon the following
math.sqrt(1025292944081384960) == math.sqrt(1025292944081385001) # returns true
what's happening here?