Results 1 to 6 of 6
 06022010, 05:57 PM #1Member
 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])
:mad:
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])
 06022010, 07:20 PM #2Senior 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.Ever seen a dog chase its tail? Now that's an infinite loop.
 06022010, 07:24 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,316
 Blog Entries
 7
 Rep Power
 25
If you divide n by two the first 'half' contains n/2 (integer division) elements so the other 'half' (those quotes again) contains nn/2 elements.
kind regards,
Jos
 06022010, 10:15 PM #4
@OP Write a program to demonstrate the problem.
 06032010, 02:56 AM #5Member
 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 !!!
 06032010, 03:03 AM #6
Similar Threads

convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05172010, 12:50 AM 
Hex to Integer Problem
By Tempspec in forum New To JavaReplies: 0Last Post: 04012010, 11:14 AM 
loop in a half
By Brain_Child in forum New To JavaReplies: 4Last Post: 12022009, 04:00 AM 
[SOLVED] Integer.parseInt() problem
By eggmanpete in forum New To JavaReplies: 18Last Post: 02152009, 05:25 PM 
Problem with Integer.parseInt()
By Hevonen in forum New To JavaReplies: 2Last Post: 12142008, 04:41 AM
Bookmarks