Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 28, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/e1045859cbd49d587028 to your computer and use it in GitHub Desktop.
Save mzsima/e1045859cbd49d587028 to your computer and use it in GitHub Desktop.
sieve of eratosthenes
//
// ViewController.mm
// SieveOfEratosthenes
//
// Created by MizushimaYusuke on 8/28/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
#include <vector>
#include <memory>
using namespace std;
class EratosthenesSolver {
public:
bool isPrime[1000];
int sieve(int n) {
fill(isPrime + 2, isPrime + n, true);
for (int i=2; i<n; i++) {
if (isPrime[i] == true) {
for (int j = pow(i, 2); j < n; j += i) isPrime[j] = false;
notify(n);
}
}
return n;
}
private:
void notify(int n) {
NSMutableArray *primeArray = [NSMutableArray array];
for (int i=0; i<=n; i++) [primeArray addObject:isPrime[i] ? @(YES) : @(NO)];
[[NSNotificationCenter defaultCenter] postNotificationName:@"my message" object:primeArray];
}
};
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *numbers;
@property (nonatomic, weak) UICollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.numbers = [NSMutableArray array];
for (int i; i<=500; i++) {
[self.numbers addObject:@(i)];
}
[self setupCollection];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(200, 600, 150, 50);
btn.tintColor = [UIColor whiteColor];
[btn setTitle:@"START" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor colorWithHue:0 saturation:1 brightness:0.3 alpha:0.9];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(startPrime) forControlEvents:UIControlEventTouchUpInside];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMessage:) name:@"my message" object:nil];
}
- (void)setupCollection {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 2.0;
layout.minimumInteritemSpacing = 2.0;
[layout setSectionInset:UIEdgeInsetsMake(30, 10, 10, 10)];
UICollectionView *cv = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
cv.delegate = self;
cv.dataSource = self;
cv.backgroundColor = [UIColor lightGrayColor];
[cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];
[self.view addSubview:cv];
self.collectionView = cv;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 500; // no change
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
cell.backgroundColor=[UIColor darkGrayColor];
[cell.subviews enumerateObjectsUsingBlock:^(UIView *v, NSUInteger idx, BOOL *stop) {
[v removeFromSuperview];
}];
NSUInteger number = indexPath.row + 1;
UILabel *num = [[UILabel alloc] init];
num.font = [UIFont systemFontOfSize:14];
num.tag = 1;
num.text = [NSString stringWithFormat:@"%ld", number];
num.textColor = [UIColor whiteColor];
[num sizeToFit];
num.center = CGPointMake(15, 15);
[cell addSubview:num];
if (![self.numbers containsObject:@(number)] || number == 1) {
cell.backgroundColor = [UIColor colorWithHue:0.1 saturation:1 brightness:0.25 alpha:1];
num.textColor = [UIColor colorWithHue:0.1 saturation:1 brightness:0.5 alpha:1];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(30, 30);
}
- (void)startPrime {
shared_ptr<EratosthenesSolver> cppClass(new EratosthenesSolver());
cppClass->sieve((int)self.numbers.count);
}
- (void)myMessage:(NSNotification *)note {
static int count = 0;
count++;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * count * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
for (int i= (int)self.numbers.count - 1; i>0; i--) {
int idx = [self.numbers[i] intValue];
if (![note.object[idx] boolValue]) {
[self.numbers removeObject:self.numbers[i]];
}
}
[self.collectionView reloadData];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment