Created
July 11, 2013 21:19
-
-
Save jparishy/5979349 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// project_euler_7 | |
// | |
// Created by Julius Parishy on 7/11/13. | |
// Copyright (c) 2013 Fitocracy. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
BOOL isPrime(NSInteger i, NSSet *otherPrimes) | |
{ | |
for(NSNumber *num in otherPrimes) | |
{ | |
if(i % num.integerValue == 0) | |
return NO; | |
} | |
return YES; | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSMutableSet *primes = [NSMutableSet setWithObject:@2]; | |
NSInteger currentInteger = 3; | |
while(primes.count < 10001) | |
{ | |
if(isPrime(currentInteger, primes)) | |
{ | |
[primes addObject:@(currentInteger)]; | |
} | |
++currentInteger; | |
} | |
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]; | |
NSArray *sortedPrimes = [primes sortedArrayUsingDescriptors:@[ sortDescriptor ]]; | |
NSLog(@"%@", sortedPrimes.lastObject); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment