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 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
随机推荐
- JBOSS EAP 6 系列三 Oracle、Mysql数据源的配置(驱动)—认识模块的使用
本文介绍JBOSS EAP 6.2中Oracle数据源的配置方式.结合之前JBOSS EAP 6.2新功能,本文初识JBOSS模块申明式容器这一特性. 模块申明式容器:JBOSS EAP不再有lib的 ...
- K均值聚类的失效性分析
K均值聚类是一种应用广泛的聚类技术,特别是它不依赖于任何对数据所做的假设,比如说,给定一个数据集合及对应的类数目,就可以运用K均值方法,通过最小化均方误差,来进行聚类分析. 因此,K均值实际上是一个最 ...
- Dynamics CRM2015 Update1 新功能之表单增强功能
CRM2015 Update 1发布后,系统的界面的变化很大,仔细观察后会发现表单窗体也有些不同了,在CRM2015 Update1的官方介绍中对此变化的解释是起用了新的窗体呈现引擎,让界面更好看加载 ...
- 百度地图开发之POI数据检索
前面学习百度地图的一些基本的用法,这次我们一起来看一看百度地图的检索功能吧 poi检索api的基本用法 百度地图的POI类中共有如下几个方法 PoiBoundSearchOption POI范围内检索 ...
- 【Android应用开发】Android Studio 错误集锦 -- 将所有的 AS 错误集合到本文
. 一. 编译错误 1. "AndroidManifest.xml file not found" 错误 (1) 报错信息 报错信息 : -- Message Make : Inf ...
- android 填满手机磁盘空间方法
http://blog.csdn.net/fulinwsuafcie/article/details/9700619 很多时候我们需要进行临界测试. 譬如当手机盘空间存满的条件下应用会有何表现等. 之 ...
- ajax post请求request.getParameter("")取值为null
今天在写提交一个json数据到后台,然后后台返回一个json数据类型.但是发现后台通过request.getParamter("")取到的值为null. 于是写一个简单的ajax ...
- 利用openssl管理证书及SSL编程第2部分:在Windows上编译 openssl
利用openssl管理证书及SSL编程第2部分:在Windows上编译 openssl 首先mingw的环境搭建,务必遵循下文: http://blog.csdn.net/ubuntu64fan/ar ...
- 1034. Head of a Gang (30) -string离散化 -map应用 -并查集
题目如下: One way that the police finds the head of a gang is to check people's phone calls. If there is ...
- UNIX环境高级编程——线程
线程包含了表示进程内执行环境必需的信息,其中包括进程中标示线程的线程ID.一组寄存器值.栈.调度优先级和策略.信号屏蔽字.errno变量以及线程私有数据. 进程的所有信息对该进程的所有线程都是共享的, ...