PowerShell: 98
Golfed code:
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)}
Ungolfed, with comments:
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
Notes:
I'm not sure exactly what the maximum reliable limit is for $n with this script. It's quite possibly less than 2^30, as $x could possibly overflow an int32 before $n gets there. Besides that, I haven't tested the upper limit myself because run times for the script already hit around 30 seconds on my system for $n=1e7 (which is only a bit over 2^23). For the same reason, I'm not quickly inclined to test and troubleshoot whatever additional syntax may be needed to upgrade the variables to uint32, int64, or uint64 where needed in order to expand this script's range.
Sample output:
I wrapped this in another for loop:
for($i=1;;$i++)
Then set $n=$i
instead of =read-host
, and changed the output to "$i | $x"
to get an idea of the script's general reliability. Here's some of the output:
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
Sidenote: I'm not really sure how some Pisano Periods are significantly shorter than $n. Is this normal, or is something wrong with my script? Nevermind - I just remembered that, after 5, Fibonacci numbers quickly become much larger than their place in the sequence. So, this makes total sense now.
f(i),f(i+1)
can take at mostn^2
values modn
. Thus,n
limited to2^30
could wind up producing a period of up to2^60
. Restrictingn <= 2^16
would giveP(n) <= 2^32
. \$\endgroup\$f(i+2) = f(i+1)+f(i)
, so the 'state' of a machine looping over the period can be described with a pair of integers modn
. There are at mostn^2
states, so the period is at mostn^2
. Oh! Wikipedia claims that the period is at most6n
. Nevermind my triviality. \$\endgroup\$