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.
2https://oeis.org/A001175 – Digital Trauma – 2017-02-21T20:27:36.377
3Limitation to 2^30 may ensure all intermediate values are less than 2^31 but it still doesn't guarantee that the Pisano Period will fit within a 32-bit signed integer. (I presume that's the reason for your limitation?) Pisano Periods can be significantly larger than their n. For example, the Pisano Period of 6 is 24. Powers of 10 above 100 come out 50 percent larger than n. – Iszi – 2013-12-06T05:34:09.707
3Pigeonhole principle says that
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
. – boothby – 2013-12-06T14:24:12.990@boothby I'm not quite sure I understand what you're saying, or if it's even properly addressing the same problem I am. Could you explain a bit further, perhaps with additional links? Feel free to pull me into chat if needed. – Iszi – 2013-12-06T17:39:40.867
2@Iszi Observe that
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. – boothby – 2013-12-06T19:35:19.283Wait, where does it say
6n
? I must have missed it. In any case, I'm pretty sure the math still says that6n
will blow out a signed 32-bit integer before we hit 2^30. – Iszi – 2013-12-06T19:45:23.247True, the Pisano period may be larger than 2^31. Would it be better to modify the question to be as I originally intended (to allow 32-bit integers) or to keep it as I originally asked meaning the Fibonnacci numbers can be stored in 32 bits but the period itself may be greater? – cardboard_box – 2013-12-07T16:15:58.333
"Eh, Paisano?" -Mario – Feathercrown – 2018-12-10T13:27:41.500