# Thread: problem in the half of odd integer

1. Member
Join Date
Jun 2010
Posts
8
Rep Power
0

## problem in the half of odd integer

Hi
this a an algorithm that I have found in the internet ,all the things are OK but just consider n=5 ,the last statement would be like this:

Java Code:
`return BinarySum(A,0,[5/2])+BinarySum(A,0+[5/2],[5/2])`
so the return statement will sum just 4 first numbers ! how about the index 5?
Java Code:
```Algorithm BinarySum(A,i,n)
Input:An array A and integers i and n
Output:The sum of integers in A starting  at index i
if n=1 then
return A[i]
return BinarySum(A,i,[n/2])+BinarySum(A,i+[n/2],[n/2])```

2. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
7
If you divide two integers, the operation will actually be integer division, ie. divide and forget the remainder:
a = m*b + r;
So, in the case of 5:
5 = 2*2 + 1;
So the return of the 5/2 operation is 2, the +1 remainder is discarded.

3. If you divide n by two the first 'half' contains n/2 (integer division) elements so the other 'half' (those quotes again) contains n-n/2 elements.

kind regards,

Jos

4. @OP Write a program to demonstrate the problem.

5. Member
Join Date
Jun 2010
Posts
8
Rep Power
0

## problem in the half of odd integer

thanks for your help but please consider n=5 , I want to sum all 5 integers together but here will sum just those 4 first integers.
A{1,4,6,8,9}

BinarySum(A,0,2)+BinarySum(A,2,2) ----> So this statement will return 19 not 28 !!!

6. Can you post the program with an explanation of why you think it doesn't work?
Also a console showing intput and output.