NSMutableArray *allNumbers = [
NSMutableArray array];
for (
int i = 2; i <= maxPrime; i++) {
[allNumbers addObject:[NSNumber numberWithInt:i]];
}
NSMutableArray *primes = [
NSMutableArray
arrayWithCapacity:maxPrime/2];
while (allNumbers.
count > 0) {
NSNumber *firstPrime = [allNumbers
objectAtIndex:0];
[primes
addObject:firstPrime];
NSMutableArray *nonPrimes = [
NSMutableArray array];
for (
int i = firstPrime.
intValue; i <= maxPrime; i += firstPrime.
intValue) {
[nonPrimes addObject:[NSNumber numberWithInt:i]];
}
}
[allNumbers
removeObjectsInArray:nonPrimes];
return [
NSArray arrayWithArray:primes];