得分:161558 227038 259038 260038 263068
我采用最快的整数平方根算法,可以找到并返回其余数是否为零。
# based on http://www.cc.utah.edu/~nahaj/factoring/isqrt.c.html
# converted to GOLF assembly for http://codegolf.stackexchange.com/questions/49356/testing-if-a-number-is-a-square
# unrolled for speed, original source commented out at bottom
start:
or u, t, 1 << 62
shr t, t, 1
gequ v, n, u
jz nope62, v
sub n, n, u
or t, t, 1 << 62
nope62:
or u, t, 1 << 60
shr t, t, 1
gequ v, n, u
jz nope60, v
sub n, n, u
or t, t, 1 << 60
nope60:
or u, t, 1 << 58
shr t, t, 1
gequ v, n, u
jz nope58, v
sub n, n, u
or t, t, 1 << 58
nope58:
or u, t, 1 << 56
shr t, t, 1
gequ v, n, u
jz nope56, v
sub n, n, u
or t, t, 1 << 56
nope56:
or u, t, 1 << 54
shr t, t, 1
gequ v, n, u
jz nope54, v
sub n, n, u
or t, t, 1 << 54
nope54:
or u, t, 1 << 52
shr t, t, 1
gequ v, n, u
jz nope52, v
sub n, n, u
or t, t, 1 << 52
nope52:
or u, t, 1 << 50
shr t, t, 1
gequ v, n, u
jz nope50, v
sub n, n, u
or t, t, 1 << 50
nope50:
or u, t, 1 << 48
shr t, t, 1
gequ v, n, u
jz nope48, v
sub n, n, u
or t, t, 1 << 48
nope48:
or u, t, 1 << 46
shr t, t, 1
gequ v, n, u
jz nope46, v
sub n, n, u
or t, t, 1 << 46
nope46:
or u, t, 1 << 44
shr t, t, 1
gequ v, n, u
jz nope44, v
sub n, n, u
or t, t, 1 << 44
nope44:
or u, t, 1 << 42
shr t, t, 1
gequ v, n, u
jz nope42, v
sub n, n, u
or t, t, 1 << 42
nope42:
or u, t, 1 << 40
shr t, t, 1
gequ v, n, u
jz nope40, v
sub n, n, u
or t, t, 1 << 40
nope40:
or u, t, 1 << 38
shr t, t, 1
gequ v, n, u
jz nope38, v
sub n, n, u
or t, t, 1 << 38
nope38:
or u, t, 1 << 36
shr t, t, 1
gequ v, n, u
jz nope36, v
sub n, n, u
or t, t, 1 << 36
nope36:
or u, t, 1 << 34
shr t, t, 1
gequ v, n, u
jz nope34, v
sub n, n, u
or t, t, 1 << 34
nope34:
or u, t, 1 << 32
shr t, t, 1
gequ v, n, u
jz nope32, v
sub n, n, u
or t, t, 1 << 32
nope32:
or u, t, 1 << 30
shr t, t, 1
gequ v, n, u
jz nope30, v
sub n, n, u
or t, t, 1 << 30
nope30:
or u, t, 1 << 28
shr t, t, 1
gequ v, n, u
jz nope28, v
sub n, n, u
or t, t, 1 << 28
nope28:
or u, t, 1 << 26
shr t, t, 1
gequ v, n, u
jz nope26, v
sub n, n, u
or t, t, 1 << 26
nope26:
or u, t, 1 << 24
shr t, t, 1
gequ v, n, u
jz nope24, v
sub n, n, u
or t, t, 1 << 24
nope24:
or u, t, 1 << 22
shr t, t, 1
gequ v, n, u
jz nope22, v
sub n, n, u
or t, t, 1 << 22
nope22:
or u, t, 1 << 20
shr t, t, 1
gequ v, n, u
jz nope20, v
sub n, n, u
or t, t, 1 << 20
nope20:
or u, t, 1 << 18
shr t, t, 1
gequ v, n, u
jz nope18, v
sub n, n, u
or t, t, 1 << 18
nope18:
or u, t, 1 << 16
shr t, t, 1
gequ v, n, u
jz nope16, v
sub n, n, u
or t, t, 1 << 16
nope16:
or u, t, 1 << 14
shr t, t, 1
gequ v, n, u
jz nope14, v
sub n, n, u
or t, t, 1 << 14
nope14:
or u, t, 1 << 12
shr t, t, 1
gequ v, n, u
jz nope12, v
sub n, n, u
or t, t, 1 << 12
nope12:
or u, t, 1 << 10
shr t, t, 1
gequ v, n, u
jz nope10, v
sub n, n, u
or t, t, 1 << 10
nope10:
or u, t, 1 << 8
shr t, t, 1
gequ v, n, u
jz nope8, v
sub n, n, u
or t, t, 1 << 8
nope8:
or u, t, 1 << 6
shr t, t, 1
gequ v, n, u
jz nope6, v
sub n, n, u
or t, t, 1 << 6
nope6:
or u, t, 1 << 4
shr t, t, 1
gequ v, n, u
jz nope4, v
sub n, n, u
or t, t, 1 << 4
nope4:
or u, t, 1 << 2
shr t, t, 1
gequ v, n, u
jz nope2, v
sub n, n, u
or t, t, 1 << 2
nope2:
or u, t, 1 << 0
shr t, t, 1
gequ v, n, u
jz nope0, v
sub n, n, u
nope0:
end:
not s, n # return !remainder
halt 0
# before unrolling...
#
# start:
# mov b, 1 << 62 # squaredbit = 01000000...
# loop: # do {
# or u, b, t # u = squaredbit | root
# shr t, t, 1 # root >>= 1
# gequ v, n, u # if remainder >= u:
# jz nope, v
# sub n, n, u # remainder = remainder - u
# or t, t, b # root = root | squaredbit
# nope:
# shr b, b, 2 # squaredbit >>= 2
# jnz loop, b # } while (squaredbit > 0)
# end:
# not s, n # return !remainder
# halt 0
编辑1:删除平方测试,直接返回!剩余,每个测试节省3个操作
编辑2:直接将n用作余数,每次测试可节省1 op
编辑3:简化循环条件,每次测试节省32个操作
编辑4:展开循环,每次测试节省约65个操作