Sha256: 8b8379e31a0852d10013a3aeb9b7146c507a9ca7032f46d14fb7685047e57cfa

Contents?: true

Size: 825 Bytes

Versions: 249

Compression:

Stored size: 825 Bytes

Contents

#import "SieveExample.h"

@implementation Sieve

+ (NSArray<NSNumber *> *)primesUpTo:(int)limit {
    if (limit < 2) {
        return @[];
    }
    
    NSMutableArray<NSNumber *> *numbers = [[NSMutableArray alloc] init];
    
    for (int i = 2; i <= limit; i++) {
        [numbers addObject:@(i)];
    }
    
    NSMutableArray<NSNumber *> *primes = [[NSMutableArray alloc] init];
    
    while (numbers.count > 0) {
        NSNumber *target = [numbers objectAtIndex:0];
        [numbers removeObjectAtIndex:0];
        [primes addObject:target];
        NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSNumber *number, NSDictionary *bindings) {
            return number.intValue % target.intValue != 0;
        }];
        [numbers filterUsingPredicate:predicate];
    }
    
    return primes;
}

@end

Version data entries

249 entries across 249 versions & 1 rubygems

Version Path
trackler-2.1.0.20 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.19 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.18 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.17 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.16 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.15 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.14 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.13 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.12 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.11 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.10 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.9 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.8 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.7 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.6 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.5 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.4 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.3 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.2 tracks/objective-c/exercises/sieve/SieveExample.m
trackler-2.1.0.1 tracks/objective-c/exercises/sieve/SieveExample.m