Find the Pisano Period

21

1

The Fibonacci sequence is a well know sequence in which each entry is the sum of the previous two and the first two entries are 1. If we take the modulo of each term by a constant the sequence will become periodic. For example if we took decided to compute the sequence mod 7 we would get the following:

1 1 2 3 5 1 6 0 6 6 5 4 2 6 1 0 1 1 ...

This has a period of 16. A related sequence, called the Pisano sequence, is defined such that a(n) is the period of the fibonacci sequence when calculated modulo n.

Task

You will should write a program or function that when given n will compute and output the period of the Fibonacci sequence mod n. That is the nth term in the Pisano sequence.

You must only support integers on the range 0 < n < 2^30

This is a competition so you should aim to minimize the size of your source code as scored by bytes.

Test cases

1  -> 1
2  -> 3
3  -> 8
4  -> 6
5  -> 20
6  -> 24
7  -> 16
8  -> 12
9  -> 24
10 -> 60
11 -> 10
12 -> 24

cardboard_box

Posted 2012-10-21T12:35:25.833

Reputation: 5 150

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 most n^2 values mod n. Thus, n limited to 2^30 could wind up producing a period of up to 2^60. Restricting n <= 2^16 would give P(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 mod n. There are at most n^2 states, so the period is at most n^2. Oh! Wikipedia claims that the period is at most 6n. Nevermind my triviality. – boothby – 2013-12-06T19:35:19.283

Wait, where does it say 6n? I must have missed it. In any case, I'm pretty sure the math still says that 6n will blow out a signed 32-bit integer before we hit 2^30. – Iszi – 2013-12-06T19:45:23.247

True, 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

Answers

11

GolfScript (28 25 24 23 chars)

~1.{(2$+}{.@+2$%}/+\-,)

Takes input in stdin, leaves it on stdout (or the stack, if you want to further process it...)

This correctly handles the corner cases (Demo).

As a point of interest to GolfScript programmers, I think this is the first program I've written with an unfold which actually came out shorter than the other approaches I tried.

Peter Taylor

Posted 2012-10-21T12:35:25.833

Reputation: 41 901

7

GolfScript, 24 characters

~:&1.{.2$+&%.2$(|}do](-,

Next iteration of a GolfScript implementation. The second version now also handles 1 correctly. It became quite long but maybe someone can find a way to shorten this version. You can try above version online.

Howard

Posted 2012-10-21T12:35:25.833

Reputation: 23 109

Does this handle input 1 correctly? – Peter Taylor – 2012-10-22T12:20:18.127

@PeterTaylor Nope, didn't test that corner case. Back to the drawing board. – Howard – 2012-10-22T12:35:34.160

@PeterTaylor The new code also works for input 1 - and still only 24 chars. – Howard – 2012-10-23T05:27:10.297

4

Python, 188 132 101 95 87 characters

n=input()
s=[]
a=k=0
b=1
while s[:k]!=s[k:]or k<1:s+=[a%n];k=len(s)/2;a,b=b,a+b
print k

Usage

$ echo 10 | python pisano.py
60

For example:

$ for i in {1..50}; do; echo $i | python pisano.py; done
1
3
8
6
20
24
16
12
24
60
10
24
28
48
40
24
36
24
18
60
16
30
48
24
100
84
72
48
14
120
30
48
40
36
80
24
76
18
56
60
40
48
88
30
120
48
32
24
112
300

ESultanik

Posted 2012-10-21T12:35:25.833

Reputation: 1 078

Thanks, beary605, for the additional golfing!

– ESultanik – 2012-10-21T16:00:38.153

You may want to count your chars again. My count of your response is below your count of your response. – DavidC – 2012-10-21T19:01:31.373

@David: Are you counting whitespace? I just double-checked (by catting to wc -c and I get the same number. – ESultanik – 2012-10-21T19:28:19.650

I use a routine furnished by Wolfram Research. It counts necessary white space, I think. – DavidC – 2012-10-21T19:53:06.067

if k>0 and s[0:k]==s[k:]:break can be changed to if s and s[:k]==s[k:]:break. You can also cut down significantly by removing the iterator, changing the for loop to while 1:, and performing a,b=a,a+b at the end of the while loop. – Strigoides – 2012-10-21T23:24:13.053

@Strigoides: Thanks! I've updated the code. – ESultanik – 2012-10-21T23:33:26.660

I went ahead and made an edit that avoids having to check for an empty list, since it would have been difficult to explain in a comment – Strigoides – 2012-10-21T23:58:30.297

4

Python 90 85 96 94 90 82

n=input();c=[1,1];a=[]
while(c in a)<1%n:a+=[c];c=[c[1],sum(c)%n]
print len(a)or 1

Edit: Implemented suggestions by beary and primo

scleaver

Posted 2012-10-21T12:35:25.833

Reputation: 507

85: a.append(c) -> a+=[c], while loop can be put onto a single line, ((n>1)>>(c in a)) -> (n>1)>>(c in a) – beary605 – 2012-10-23T23:36:19.183

append actually has a different functionality than +=. Thanks for the tips though. – scleaver – 2012-10-24T18:35:30.697

I think it works the same way in this case. – beary605 – 2012-10-25T00:12:17.510

(n>1)>>(c in a) -> (c in a)<1%n for 3 bytes. And I agree with beary about the append. Whether you append a reference to c, or extend a by the value of c, it's exactly the same either way (as you immediately destroy your reference to c anyway). – primo – 2012-10-27T07:21:44.693

Ah ok, my mistake was that I was using a+=c instead of a+=[c] – scleaver – 2012-10-29T17:07:21.887

2

Mathematica 73

p = {1, 0}; j = 0; q = p;
While[j++; s = Mod[Plus @@ p, n]; p = RotateLeft@p; p[[2]] = s; p != q]; j

DavidC

Posted 2012-10-21T12:35:25.833

Reputation: 24 524

2

PHP - 61 57 bytes

<?for(;1<$a.$b=+$a+$a=!$i+++$b%$n+=fgets(STDIN););echo$i;

This script will erroneously report 2 for n=1, but all other values are correct.

Sample I/O, a left-truncable series where π(n) = 2n + 2 :

$ echo 3 | php pisano.php
8
$ echo 13 | php pisano.php
28
$ echo 313 | php pisano.php
628
$ echo 3313 | php pisano.php
6628
$ echo 43313 | php pisano.php
86628
$ echo 543313 | php pisano.php
1086628
$ echo 4543313 | php pisano.php
9086628
$ echo 24543313 | php pisano.php
49086628

primo

Posted 2012-10-21T12:35:25.833

Reputation: 30 891

11<$a.$b=+$a+$a=!$i+++$b%$n+=fgets(STDIN) Oh god, that's some order of operation exploitation right there. – Mr. Llama – 2012-10-25T17:11:51.223

1

Perl, 75, 61, 62 + 1 = 63

$k=1%$_;$a++,($m,$k)=($k,($m+$k)%$_)until$h{"$m,$k"}++;say$a-1

Usage

$ echo 8 | perl -n -M5.010 ./pisano.pl
12

Ungolfed

$k = 1 % $_;
$a++, ($m, $k) = ($k, ($m + $k) % $_) until $h{"$m,$k"}++;
say $a - 1

+1 byte for -n flag. Shaved off 13 bytes thanks to Gabriel Benamy.

Silvio Mayolo

Posted 2012-10-21T12:35:25.833

Reputation: 1 817

1You can get rid of $n=<>; (-6) and replace it with the -n flag (+1), then all instances of $n can be replaced with $_. You can use -M5.010 for free, which lets you use the say command instead of print (-2). Modifier while statements do not need parentheses around the condition (-2). Instead of @{[%h]}/2, you can have a counter $a++, before ($m,$k)= and then just have say$a-1 at the end (-2). Instead of "$m,$k" use $m.$k (-2). This should come out to $k=1%$_;$a++,($m,$k)=($k,($m+$k)%$_)while!$h{$m.$k}++;say$a-1 with the -n flag, for 61 + 1 = 62 bytes. – Gabriel Benamy – 2016-12-20T14:55:03.950

Clearly I'm not as clever with Perl as I thought I was. Thanks for the tips. – Silvio Mayolo – 2016-12-20T18:34:30.283

There are a lot of useful pointers in the Tips for golfing in Perl thread! Good luck! ^^

– Gabriel Benamy – 2016-12-20T18:37:06.603

Actually, I was wrong -- you need "$m,$k" instead of $m.$k, (+2), but you can save 1 byte by changing while!$h to until$h (-1). Sorry! – Gabriel Benamy – 2016-12-20T19:22:34.090

Hm? Under what input does $m.$k fail? It seemed to work on my end. – Silvio Mayolo – 2016-12-20T19:33:38.157

60 yields 77 but it should be 120. At least, it did for me. – Gabriel Benamy – 2016-12-20T19:34:05.917

1

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.

Iszi

Posted 2012-10-21T12:35:25.833

Reputation: 2 369

0

Clojure, 102 bytes

Not too exciting, iterates the formula until we reach back [1 1] (I hope this is always the case). Special handling of (f 1) as it converges to [0 0].

#(if(< % 2)1(+(count(take-while(fn[v](not=[1 1]v))(rest(iterate(fn[[a b]][b(mod(+ a b)%)])[1 1]))))1))

NikoNyrh

Posted 2012-10-21T12:35:25.833

Reputation: 2 361