sieve的objective-c实现
用obj-cl来实现前面的sieve代码貌似“丑”了不少,应该有更好的方式:比如不用Foundation或不用NSArray类,而改用其它更“底层”的类。
先把代码贴出来:
//
// main.m
// sieve
//
// Created by kinds on 15/5/2.
// Copyright (c) 2015年 hopy. All rights reserved.
//
#import <Foundation/Foundation.h>
//#include <time.h>
//#include <unistd.h>
typedef unsigned long long ULL;
void zero_array(ULL count,NSMutableArray *ary){
for (ULL i = 0; i < count; i++) {
[ary addObject:[NSNumber numberWithInt:0]];
}
}
ULL sieve_objc(ULL n){
NSMutableArray *ary = [NSMutableArray arrayWithCapacity:n+1];
zero_array(n+1, ary);
//NSLog(@"ary is %lu",[ary count]);
ULL max = sqrtl(n);
ULL p = 2;
NSNumber *x = nil;
while (p<=max) {
for(ULL i=2*p;i<=n;i+=p)
//[ary replaceObjectAtIndex:i withObject:[NSNumber numberWithInt:1]];
[ary setObject:[NSNumber numberWithInt:1] atIndexedSubscript:i];
x = [ary objectAtIndex:++p];
while ([x intValue]) {
x = [ary objectAtIndex:++p];
}
}
x = [ary objectAtIndex:n];
while ([x intValue]) {
n--;
x = [ary objectAtIndex:n];
}
return n;
}
ULL sieve(ULL n){
char *ary = malloc(n+1);
if(!ary) return 0;
memset(ary,0,n+1);
ULL max = sqrtl(n);
ULL p = 2;
while (p <= max) {
for(ULL i = 2*p;i<=n;i+=p)
ary[i] = 1;
while (ary[++p]); //empty
}
while(ary[n]) n--;
return n;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSProcessInfo *psi = [NSProcessInfo processInfo];
NSArray *args = [psi arguments];
if([args count] != 2){
printf("usage : %s n\n",[[[args objectAtIndex:0] lastPathComponent] UTF8String]);
return 1;
}
long long n = [[args objectAtIndex:1] integerValue];
if (!n) {
puts("you must input a number");
return 2;
}
if(n<0){
puts("you must input a +number");
return 3;
}
clock_t start = clock();
//ULL result = sieve((ULL)n);
ULL result = sieve_objc((ULL)n);
if(!result){
puts("sieve calc failed!");
return 4;
}
double end = ((1.0 * (clock() - start)) / CLOCKS_PER_SEC) * 1000.0;
printf("max p is %llu (take %f ms)\n",result,end);
}
return 0;
}
没找到NSArray中的类或实例方法有可以完成如下简单的数组功能:
//定义10000个指定对象的数组
typedef struct _st_foo{
char name[256];
char note[1024];
int age;
unsigned long long id;
}st_foo,*pst_foo;
st_foo ary[1024]; //NSArray can't do this
所以写了一个zero_array函数来完成该功能,该函数超慢的。
所以可想而知这个obj-c版的效率能有多差,等有机会再优化一下吧。
sieve的objective-c实现的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- 埃拉托色尼筛法(Sieve of Eratosthenes)求素数。
埃拉托色尼筛法(Sieve of Eratosthenes)是一种用来求所有小于N的素数的方法.从建立一个整数2~N的表着手,寻找i? 的整数,编程实现此算法,并讨论运算时间. 由于是通过删除来实现, ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- Objective C ARC 使用及原理
手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...
- Objective -C学习笔记之字典
//字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
随机推荐
- 指令汇B新闻客户端开发(一) 新手引导页开发
首先做开发的时候应该有一个闪屏页面和新手引导页, 我相信闪屏页面大家应该都会了,那么先看到新手引导页了. 我们可以看到这其实是一个ViewPager,我们也可以看到这是3个引导页,那么首先来看一下布局 ...
- 2.关于QT中数据库操作,简单数据库连接操作,数据库的增删改查,QSqlTableModel和QTableView,事务操作,关于QItemDelegate 代理
Linux下的qt安装,命令时:sudoapt-get install qt-sdk 安装mysql数据库,安装方法参考博客:http://blog.csdn.net/tototuzuoquan ...
- PullToRefreshScrollView 嵌套RecyclerView实现特卖列表倒计时抢购
不久之前,我们谈到了通过Handler与timer及TimerTask结合实现倒计时抢购列表,那个是PullToRefreshListView实现的,今天要讲的是PullToRefreshScroll ...
- Android初级教程:使用xml序列器
之前备份短信的时候生成xml都是手动拼写的,有一个问题:当短信里面存在</body>这样的标签的时候,最后结果就不是完整的xml文件,显然出错.但是,今天使用序列化器的方式,就能有效的解决 ...
- Error处理:Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack tra
[2014-04-20 20:59:23 - MyDetectActivity] Dx trouble writing output: already prepared [2014-04-20 20 ...
- Cocos2D iOS之旅:如何写一个敲地鼠游戏(二):Cocos2D中的高清支持
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 1045. Favorite Color Stripe (30) -LCS允许元素重复
题目如下: Eva is trying to make her own color stripe out of a given one. She would like to keep only her ...
- android值得珍藏的6个开源框架技术
1.volley 项目地址 https://github.com/smanikandan14/Volley-demo JSON,图像等的异步下载: 网络请求的排序(scheduling) 网络请求的 ...
- qualcomm memory dump 抓取方法
Memory dump是系统出现crash时常用的分析故障原因的方法,qualcomm 各子系统运行时,为方便debug,都会开辟ram log和debug variable用于保存各系统运行信息及健 ...
- AngularJS进阶(三十八)上拉加载问题解决方法
AngularJS上拉加载问题解决方法 项目中始终存在一个问题:当在搜索栏输入关键词后(见图1),按照既定的业务逻辑应该是服务端接收到请求后,首先返回查询的前7条数据,待客户端出现上拉加载时,继续查找 ...