I want to develop in Objective-C all possible sums to reach a result, like in this another thread, but in Objective-C
The code in JAVA is:
public void sum_up_recursive(ArrayList<Integer> numbers, int target, ArrayList<Integer> partial) {
int s = 0;
for (int x: partial) s += x;
if (s == target){
System.out.println("sum("+Arrays.toString(partial.toArray())+")="+target);
}
if (s >= target)
return;
for(int i=0;i<numbers.size();i++) {
ArrayList<Integer> remaining = new ArrayList<Integer>();
int n = numbers.get(i);
for (int j=i+1; j<numbers.size();j++) remaining.add(numbers.get(j));
ArrayList<Integer> partial_rec = new ArrayList<Integer>(partial);
partial_rec.add(n);
sum_up_recursive(remaining,target,partial_rec);
}
}
My intent in Objective-C:
Main Class:
NSMutableArray *numbers = [[NSMutableArray alloc] init];
[numbers addObject:@2];
[numbers addObject:@4];
[numbers addObject:@8];
[numbers addObject:@16];
[numbers addObject:@32];
NSMutableArray *partial = [[NSMutableArray alloc] init];
[testclass sum_up_recursive:numbers : 12 :partial];
Method:
- (void) sum_up_recursive: (NSMutableArray*) numbers: (int) target:(NSMutableArray*) partial{
NSInteger s = 0;
for (NSNumber *x in partial){
s = s + [x integerValue];
}
if (s == target){
NSLog(@"RESULT: %@", partial);
}
if (s >= target){
return;
}
for (NSInteger i = 0 ; i < [numbers count]; i++) {
NSMutableArray* remaining;
NSInteger n = [numbers objectAtIndex:i];
for (NSInteger j=i+1 ; j < [numbers count]; j++) {
[remaining addObject:[numbers objectAtIndex:j]];
}
NSMutableArray *partial_rec = partial;
[partial_rec addObject: [NSNumber numberWithInteger:n]];
[self sum_up_recursive:remaining:target:partial_rec];
}
}
I have not errors, but I have not result, I can't reach the target value, I'm a Android developer probably anybody with more experience can see what is the error in my code or give another opinion.
I fixed the errors, this method works perfectly:
- (void) sumUpRecursive:(NSMutableArray *)numbers withTarget:(NSInteger)target withPartial:(NSMutableArray *)partial {
NSInteger s = 0;
for (NSNumber *x in partial){
s = s + [x integerValue];
}
if (s == target){
NSLog(@"RESULT: %@", partial);
}
if (s >= target){
return;
}
for (NSInteger i = 0; i < [numbers count]; i++) {
NSMutableArray *remaining = [[NSMutableArray alloc] init];
NSInteger n = [[numbers objectAtIndex:i] integerValue];
for (NSInteger j = i + 1; j < [numbers count]; j++) {
[remaining addObject:[numbers objectAtIndex:j]];
}
NSMutableArray *partial_rec = [[NSMutableArray alloc] init];
[partial_rec addObjectsFromArray:partial];
[partial_rec addObject: [NSNumber numberWithInteger:n]];
[self sumUpRecursive:remaining withTarget:target withPartial:partial_rec];
}
}
The output of this method is:
numbers(2, 4, 8, 16, 32)
1) Example, sum target: 12, Output: 4, 8 means.. 4+8.
2) Example, sum target: 14, Output: 2, 4, 8.
3) Example, sum target: 44, Ouput: 4, 8, 32
This question is not a duplicate, because in the another question the idea is search all combinations to sum, but the problem is only works with 2 numbers, in my case I need to try with N+ numbers.