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 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
随机推荐
- 高通msm8994性能及温度监测脚本
[plain] view plain copystartTime=$(date +%Y-%m-%d-%H-%M-%S) pathName="/data/cpu_logs" fi ...
- EBS财务模块表结构
gl_code_combinations:科目组合 字段名 含义 备注 code_combination_id 主键,科目编码ID,自动编号 segment1 分行代码 setgment2 是受 ...
- 1073. Scientific Notation (20)
题目如下: Scientific notation is the way that scientists easily handle very large numbers or very small ...
- UNIX网络编程——客户/服务器程序设计示范(八)
TCP预先创建线程服务器程序,主线程统一accept 最后一个使用线程的服务器程序设计示范是在程序启动阶段创建一个线程池之后只让主线程调用accept并把每个客户连接传递给池中某个可用线程. ...
- 5.创建表,使用alter进行表信息的增删改,Oracle回收站,集合运算
1 Oracle基于用户的管理方案 2 DDL语句可以管理数据库的对象有:视图 索引 序列 同义词 约束 3 创建一个表,有2个条件(1 有权限:2有表空间) Oracle给你提 ...
- LCD 常用的客观效果指标和测试方法
1.DPI--精密度: 评分标准 DPI 评分 DPI<200 50 200≤DPI<250 60 250≤DPI<300 70 300≤DPI<350 80 350≤DPI& ...
- awk:快速入门(简单实用19例+鸟哥书内容)
awk 用法:awk ' pattern {action} ' 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 ...
- 怎么在Eclipse中添加VI插件
下载地址 Vi插件下载位置 怎么安装? 将下载下来的zip文件进行解压,然后把对于的目录下的文件分别复制到eclipse目录下的plugins 和features目录下: 注册 在eclipse根目录 ...
- iOS中 快速正确的安装 CocoaPods
有问题或技术交流可以咨询!欢迎加入! 第一部分: CocoaPods 的安装 步骤1 - 安装 RVM RVM 是干什么的这里就不解释了,后面你将会慢慢搞明白. $ curl -L https://g ...
- 【一天一道LeetCode】#97. Interleaving String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given s ...