目标c,反向Collat​​z,帮助我调试NSArrays

时间:2011-12-20 23:42:14

标签: objective-c nsmutablearray collatz nsmutableset

我正在使用Collat​​z猜想开展一个数学博览会项目,我想制作一个反向图,你可以找到here on wikipeadia,虽然我发现我已经在我的代码中弄乱了它并且它没有t显示所有反向值(例如,它将从数组中找到一个奇数值,然后找到相同的值* 2,但它不会超过它)。

    #import "numberCreator.h"

@implementation numberCreator
@synthesize levels, nextX, nextY, numbers;

- (id)init
{
    self = [super init];
    if (self) {
        numbers         = [[NSMutableArray alloc]initWithCapacity:1000];//gives the array an estamated cap.
        currentLevel    = [[NSMutableArray alloc]initWithCapacity:25];
        nextLevel       = [[NSMutableArray alloc]initWithCapacity:25];
        checkNumbers    = [[NSMutableSet alloc]init];
    }

    return self;
}

//Creates all of the needed numbers in the reverce Collatz Web
- (void) create{
    [currentLevel addObject:[NSNumber numberWithInt:1]];
    [numbers addObject:[NSNumber numberWithInt:1]];
    [numbers addObject:[NSNumber numberWithInt:0]];
    [numbers addObject:[NSNumber numberWithInt:0]];
    [checkNumbers addObject:[NSNumber numberWithInt:1]];

    for(int x; x < levels; x++){
        for(int y; y < [currentLevel count]; y++){
            if([checkNumbers containsObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]floatValue]*2]]){}
            else{
                [checkNumbers addObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]intValue]*2]];

                [numbers addObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]intValue]*2]];
                [numbers addObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]intValue]]];//The array is set like Number, Parent, level
                [numbers addObject:[NSNumber numberWithInt:x+1]];

                [nextLevel addObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]intValue]*2]];
                if((y-1)/3%2==1){
                    if([checkNumbers containsObject:[NSNumber numberWithInt:([[currentLevel objectAtIndex:y]floatValue]-1)/3]]){}
                    else{
                        [checkNumbers addObject:[NSNumber numberWithInt:([[currentLevel objectAtIndex:y]intValue]-1)/3]];


                        [numbers addObject:[NSNumber numberWithInt:([[currentLevel objectAtIndex:y]intValue]-1)/3]];
                        [numbers addObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]intValue]]];
                        [numbers addObject:[NSNumber numberWithInt:x+1]];

                        [nextLevel addObject:[NSNumber numberWithInt:[[currentLevel objectAtIndex:y]intValue]*2]];
                    }
                }else{}
            }

        }

        [currentLevel removeAllObjects];
        for(int y; y < [nextLevel count]; y++){
            [currentLevel addObject:[nextLevel objectAtIndex:y]];
        }
        [nextLevel removeAllObjects];
    }

    for(int x; x < [numbers count]; x++){
        NSLog(@"%i", [[numbers objectAtIndex:x]intValue]);
    }
}

@end

1 个答案:

答案 0 :(得分:1)

因为,你只使用整数,使用NSMutableIndexSet会更有效率。

#import "NumberCreator.h"

@implementation NumberCreator
@synthesize levels, numbers;

- (id)init
{
    if ( !(self = [super init] ) )
        return nil;

    numbers = [NSMutableIndexSet new];
    return self;
}

- (void)create
{
    [numbers removeAllIndexes];
    [numbers addIndex:1];

    NSMutableIndexSet *nextLevel = [NSMutableIndexSet indexSet];
    NSMutableIndexSet *currentLevel = [NSMutableIndexSet indexSetWithIndex:1];

    for (int i = 0; i < levels; i++ )
    {
        [currentLevel enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

            if ( ! [numbers containsIndex:idx * 2] )
                [nextLevel addIndex:idx * 2];

            if ( idx % 6 == 4 && ! [numbers containsIndex:(idx - 1) / 3] )
                [nextLevel addIndex:(idx - 1) / 3];
        }];

        [numbers addIndexes:nextLevel];
        [currentLevel removeAllIndexes];
        [currentLevel addIndexes:nextLevel];
        [nextLevel removeAllIndexes];
    }

    [numbers enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
        NSLog(@"%i", idx);
    }];
}

@end