Skip to content

Instantly share code, notes, and snippets.

@jparishy
Created July 11, 2013 21:19
Show Gist options
  • Save jparishy/5979349 to your computer and use it in GitHub Desktop.
Save jparishy/5979349 to your computer and use it in GitHub Desktop.
//
// 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