Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  main.m
  3. //  ApptopiaTest
  4. //
  5. //  Created by Nikita Pankiv on 2/19/17.
  6. //  Copyright © 2017 Neutral. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. int main(int argc, const char * argv[]) {
  12.     @autoreleasepool {
  13.        
  14.         NSString *location = @"/Users/Nikita/Desktop/Test/";// Place your directory here
  15.        
  16.         NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:location];
  17.         NSString *fileName = nil;
  18.        
  19.         NSMutableArray *files = [NSMutableArray new];
  20.         while (fileName = [enumerator nextObject]) {
  21.             if ([fileName hasSuffix:@".txt"]) { // Only txt's
  22.                 [files addObject:[location stringByAppendingString:fileName]];
  23.             }
  24.         }
  25.        
  26.         // Find the largest number in all files to generate the Eratosthenes only once
  27.         // Not the most benefit way, but I decided to do so
  28.         unsigned long size = 0;
  29.         for (NSString *fileName in files) {
  30.             NSArray *numbers = [[[NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:@","] sortedArrayUsingComparator:^NSComparisonResult(NSString*  _Nonnull obj1, NSString*  _Nonnull obj2) {
  31.                 return [obj1 integerValue] > [obj2 integerValue];
  32.             }];
  33.             size = MAX([[numbers lastObject] integerValue], size);
  34.         }
  35.        
  36.        
  37.         // Calculate Sieve Array for prime numbers
  38.         NSMutableArray *checkNumbers = [NSMutableArray new];
  39.    
  40.         for (int i = 0; i < size + 1; i++) {
  41.             [checkNumbers insertObject:@(YES) atIndex:i];
  42.         }
  43.        
  44.         for (int i = 2; i * i <= size; ++i) {
  45.             if ([checkNumbers[i] boolValue] && i % 2 != 0) {
  46.                 for (int j = 2; j <= size / i; j++) {
  47.                     if ([checkNumbers[i * j] boolValue]) {
  48.                         checkNumbers[i * j] = @(NO);
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.        
  54.        
  55.         // Find prime numbers
  56.         for (NSString *fileName in files) {
  57.             NSArray *numbers = [[NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil] componentsSeparatedByString:@","];
  58.             NSMutableArray *resultedNumbers = [NSMutableArray new];
  59.             for (NSString *num in numbers) {
  60.                 if ([checkNumbers[[num integerValue]] boolValue]) {
  61.                     [resultedNumbers addObject:num];
  62.                 }
  63.             }
  64.            
  65.             // Write to same directory
  66.             NSData *resultedData = [[resultedNumbers componentsJoinedByString:@","] dataUsingEncoding:NSUTF8StringEncoding];
  67.             [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@modified_%@", location, [fileName lastPathComponent]] contents:resultedData attributes:nil];
  68.         }
  69.        
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement