Guest User

Untitled

a guest
Jul 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //
  2. // FasterViewController.m
  3. // Faster
  4. //
  5. // Created by Ani Vemprala on 12/24/10.
  6. // Copyright 2010 Ani Vemprala. All rights reserved.
  7. //
  8.  
  9. #import "FasterViewController.h"
  10.  
  11. @implementation FasterViewController
  12. @synthesize numbers;
  13. @synthesize textbox;
  14.  
  15. #pragma mark -
  16. #pragma mark App logic
  17.  
  18. - (void) backgroundCalculations {
  19. // Run in background:
  20. [self performSelectorInBackground:@selector(primeCalcs) withObject:nil];
  21. // Run in foreground UI thread:
  22. //[self performSelector:@selector(primeCalcs) withObject:nil];
  23. }
  24.  
  25. - (void) primeCalcs {
  26. NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  27. for (int i=1; i <= 100000; i++) {
  28. for (int j=1; j <= i; j++) {
  29. if (i % j == 0 && i != j && j != 1) {
  30. break;
  31. }
  32. else if (i % j == 0 && i == j) {
  33. [numbers addObject:[NSNumber numberWithInt:i]];
  34. NSLog(@"%d is prime - array is now %d big", i, [numbers count]);
  35. }
  36. }
  37. }
  38. NSUInteger count = [numbers count];
  39. [self.textbox setText:[NSString stringWithFormat:@"%d", count]];
  40. [pool release];
  41. }
  42.  
  43. #pragma mark -
  44. #pragma mark View logic
  45.  
  46.  
  47. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. UILabel* labelOne = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 500.0, 20.0)];
  51. labelOne.text = @"Finding prime numbers btw 1 and 1MM";
  52. [self.view addSubview:labelOne];
  53. [labelOne release];
  54.  
  55. UIButton* startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  56. [startButton setFrame:CGRectMake(10.0, 50.0, 100.0, 20.0)];
  57. [startButton setTitle:@"Calculate" forState:UIControlStateNormal];
  58. [startButton addTarget:self action:@selector(backgroundCalculations) forControlEvents:UIControlEventTouchUpInside];
  59. [self.view addSubview:startButton];
  60.  
  61. numbers = [[NSMutableArray alloc] init];
  62. }
  63.  
  64. - (void)dealloc {
  65. [numbers release];
  66. [super dealloc];
  67. }
  68.  
  69. @end
Add Comment
Please, Sign In to add comment