的PowerShell:98
高尔夫代码:
for($a,$b=0,(1%($n=read-host))){$x++;if($a+$b-eq0-or("$a$b"-eq10)){$x;break}$a,$b=$b,(($a+$b)%$n)}
取消评论,并附有评论:
for
(
# Start with $a as zero, and $b as 1%$n.
# Setting $b like this at the start helps catch the exceptional case where $n=1.
$a,$b=0,(1%
(
# Grab user input for n.
$n=read-host
))
)
{
# Increasing the counter ($x) and testing for the end of the period at the start ensures proper output for $n=1.
$x++;
# Test to see if we've found the end of the Pisano Period.
if
(
# The first part catches $n=1, since $a and $b will both be zero at this point.
$a+$b-eq0-or
(
# A shorter way of testing $a-eq1-and$b-eq0, which is the end of a "normal" Pisano Period.
"$a$b"-eq10
)
)
{
# Pisano Period has reached its end. Output $x and get out of the loop.
$x;break
}
# Pisano Period still continues, perform operation to calculate next number.
# Works pretty much like a Fibonacci sequence, but uses ($a+$b)%$n for the new $b instead.
# This takes advantage of the fact we don't really need to track the actual Fibonacci numbers, just the Fibonacci pattern of %$n.
$a,$b=$b,(($a+$b)%$n)
}
# Variable cleanup - not included in golfed code.
rv n,a,b,x
笔记:
我不确定使用此脚本对$ n的最大可靠限制是多少。它可能小于2 ^ 30,因为$ x可能在$ n到达那里之前溢出int32。除此之外,我自己还没有测试上限,因为脚本的运行时间在我的系统上已经达到30秒,价格为$ n = 1e7(仅比2 ^ 23多一点)。出于相同的原因,我不会很快倾向于对可能需要的其他语法进行测试和故障排除,以将变量升级到uint32,int64或uint64,以便扩展此脚本的范围。
样本输出:
我将其包装在另一个for循环中:
for($i=1;;$i++)
然后设置$n=$i
代替=read-host
,然后更改输出以"$i | $x"
了解脚本的一般可靠性。这是一些输出:
1 | 1
2 | 3
3 | 8
4 | 6
5 | 20
6 | 24
7 | 16
8 | 12
9 | 24
10 | 60
11 | 10
12 | 24
13 | 28
14 | 48
15 | 40
16 | 24
17 | 36
18 | 24
19 | 18
20 | 60
...
9990 | 6840
9991 | 10192
9992 | 624
9993 | 4440
9994 | 1584
9995 | 6660
9996 | 1008
9997 | 1344
9998 | 4998
9999 | 600
10000 | 15000
10001 | 10212
10002 | 3336
10003 | 5712
10004 | 120
10005 | 1680
10006 | 10008
10007 | 20016
10008 | 552
10009 | 3336
10010 | 1680
边注: 我不太确定Pisano周期比$ n短得多。这是正常现象,还是我的脚本有问题?没关系-我只是记得,5后,斐波那契数迅速成为很多比他们的顺序发生大。因此,这现在很有意义。