Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // FasterViewController.m
- // Faster
- //
- // Created by Ani Vemprala on 12/24/10.
- // Copyright 2010 Ani Vemprala. All rights reserved.
- //
- #import "FasterViewController.h"
- @implementation FasterViewController
- @synthesize numbers;
- @synthesize textbox;
- #pragma mark -
- #pragma mark App logic
- - (void) backgroundCalculations {
- // Run in background:
- [self performSelectorInBackground:@selector(primeCalcs) withObject:nil];
- // Run in foreground UI thread:
- //[self performSelector:@selector(primeCalcs) withObject:nil];
- }
- - (void) primeCalcs {
- NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
- for (int i=1; i <= 100000; i++) {
- for (int j=1; j <= i; j++) {
- if (i % j == 0 && i != j && j != 1) {
- break;
- }
- else if (i % j == 0 && i == j) {
- [numbers addObject:[NSNumber numberWithInt:i]];
- NSLog(@"%d is prime - array is now %d big", i, [numbers count]);
- }
- }
- }
- NSUInteger count = [numbers count];
- [self.textbox setText:[NSString stringWithFormat:@"%d", count]];
- [pool release];
- }
- #pragma mark -
- #pragma mark View logic
- // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- - (void)viewDidLoad {
- [super viewDidLoad];
- UILabel* labelOne = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, 500.0, 20.0)];
- labelOne.text = @"Finding prime numbers btw 1 and 1MM";
- [self.view addSubview:labelOne];
- [labelOne release];
- UIButton* startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [startButton setFrame:CGRectMake(10.0, 50.0, 100.0, 20.0)];
- [startButton setTitle:@"Calculate" forState:UIControlStateNormal];
- [startButton addTarget:self action:@selector(backgroundCalculations) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:startButton];
- numbers = [[NSMutableArray alloc] init];
- }
- - (void)dealloc {
- [numbers release];
- [super dealloc];
- }
- @end
Add Comment
Please, Sign In to add comment