-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
//算法入口
[self func2:9];
}
//计算阶乘 factor(m)= m!
-(int)factor:(int)m{
int factorNum=0;
if(m==0|m==1)
return 1;
else{
factorNum=m*[self factor:m-1];
NSLog(@"%d",factorNum);
return factorNum;
}
}
//计算 func1(m) = 1!+3!+5!+... +m!
-(int)func1:(int)m{
int sum=0;
for (int i=1; i<=m; i=i+2) {
sum +=[self factor:i];
}
NSLog(@"%d",sum);
return sum;
}
//计算最终和 func2(m)=1!+(1!+3!)+(1!+3!+5!) + ( 1! + 3! + 5! + 7! + 9!)+....+(1!+3!+5!+ ... + m!)
-(int)func2:(int)m{
int sum=0;
for (int i=1; i<=m; i=i+2) {
sum +=[self func1:i];
}
NSLog(@"最终结果为:%d",sum);
return sum;
}
时间: 2024-11-10 17:02:26