小编Als*_*Als的帖子

如果两个算法都具有O(1)的性能,为什么一个算法比另一个算法更快

如果我有一个非常大的数字X, (978678567445456455878909099775523213255436445691200897623461676543789287 948754875435250392049320487584759329 454754875487589457244076477592458249)

我有两个算法可以计算输入数是否可被4整除(即n%4 == 0).由于模运算需要O(1),如果它们都是O(1),为什么一种算法会优于另一种算法.我怎么能证明只比较最后两位数(2)的那个实际上比另一个好?

算法1:

n:= Input 
if n divisible by 4, let output :=0 
else output :=1
Run Code Online (Sandbox Code Playgroud)

算法2:

m:=last two digits of input n
if m divisible by 4, let output:=0 
else output:=1
Run Code Online (Sandbox Code Playgroud)

algorithm complexity-theory big-o

1
推荐指数
1
解决办法
165
查看次数

标签 统计

algorithm ×1

big-o ×1

complexity-theory ×1