Vous êtes sur la page 1sur 2

CS14 Fall 2015 Lab 1

The Fibonacci Series Modulo M


September 29, 2015
DUE: Submit by iLearn by 11:59pm Tuesday, October 6, 2015

Introduction

Consider a modification to the well-known Fibonacci series in which each element in the series is replaced by
its remainder modulo M , where M 2 is some positive integer. In this case, the definition of the modified
Fibonacci series becomes:
F [k + 1] = (F [k 1] + F [k]) mod M
(1)
This modification has a dramatic effect on the resulting series, which is now both (i) finite (i.e., bounded
between 0 and M 1, rather than growing aritrarily large) and (ii) periodic (repeating).
For example, if we choose M = 4 then the series becomes:
F [0]

= 0

F [1]

= 1

F [2]
F [3]

= (0 + 1) mod 4 = 1
= (1 + 1) mod 4 = 2

F [4]
F [5]

= (1 + 2) mod 4 = 3
= (2 + 3) mod 4 = 1

F [6]

= (3 + 1) mod 4 = 0

F [7]
F [8]

= (1 + 0) mod 4 = 1
= (0 + 1) mod 4 = 1

Notice that the values of both F [k 1] and F [k] are identical for k = 7 as they were for k = 1. Therefore,
the next value F [k + 1] must be identical for both k = 7 and k = 1 because of Eq.(1), and thereafter every
subsequent value must also be identical, starting from k = 7 versus starting from k = 1. In other words, the
infinite Fibonacci series is now just one basic vector (0, 1, 1, 2, 3, 1) repeating over and over.

1.1

Problem Generalization

In addition to the basic defining described above, we could also allow the user to select a different pair of
starting values for the sequence, instead of simply using the first two elements from the standard Fibonacci
series. If the new pair of starting values does not appear anywhere in the basic vector described above, then
it must be part of a different repeating cycle.
For example, since the values (3, 2) never appear in that order in the previous example, the Fibonacci-like
sequence they define
F [0]

= 3

F [1]
F [2]

= 2
= (3 + 2) mod 4 = 1
1

F [3]
F [4]

= (2 + 1) mod 4 = 3
= (1 + 3) mod 4 = 0

F [5]
F [6]

= (3 + 0) mod 4 = 3
= (0 + 3) mod 4 = 3

F [7]

= (3 + 3) mod 4 = 2

F [8]

= (3 + 2) mod 4 = 1

corresponds to a different basic vector (3, 2, 1, 3, 0, 3) repeating over and over.

1.2

Observations

Using the two examples above, it should be clear that the length of the basic vector representing the repeating
pattern may be considerably larger than M because the Fibonacci formula depends of the most-recently seen
pair of values (rather than single value). Furthermore, individual values may appear multiple times in the
basic vector, and not necessarily the same number of times for each value (i.e., 1 appears three times as
often as any of the other values in the first example).
Since the elements 0, 1, . . . , M 1 can form exactly M 2 distinct ordered pairs, the length of the repeating
cycle in the generalized Fibonacci sequence cannot be greater than M 2 . Therefore, if you cant find the basic
vector by the time you have reached the M 2 + 1st element of the sequence, your program is broken.
Notice that in both examples shown above, the basic vector includes F [0] and F [1]. However, this
observation does not prove the general case, so it is conceivable that for some other pair of starting values
some elements from the beginning of the sequence are not included in the repeating pattern, similar to this:
a, b, c, d, c, d, c, d, . . . You should design your algorithm to handle this possibility correctly.

Specifications for the fibmod function

Write a function, fibmod, that returns a vector class (NOT a C-style array) containing exactly one copy
of the basic vector of repeating elements in the generalized Fibonnaci series modulo M . Fibmod can be
called with either one or three parameters. In the single-parameter form, it requires a modulus, M . In the
three-parameter form, it requires the first two values for the sequence, F [0] and F [1], followed by a modulus
M.
Write a driver that calls your fibmod function with inputs obtained interactively from the keyboard, and
for each test case outputs the repeating vector and the frequencies of each value between 0 and M 1 in
that vector.
Add the option to your driver to use repeated calls to fibmod with the same modulus M and different
initial sequence values to find the set of all distinct basic vectors for the given modulus M . For example, if
M = 4 then the two example sequences given above are not sufficient because they only cover 12 out of the
16 distinct pairs. Clearly (0, 0) is always in the set for every M . In this case, (0, 2, 2) covers the remaining
pairs.
Your program must display both the basic vector and individual value frequencies in a compact tabular
format.
Test your function on a variety of values of M including large ones. How large can M be before your
method fails, or becomes impractically slow?

Vous aimerez peut-être aussi