HELP!

I'm a computer science student currently taking Data Structures and Algorithms II and my professor is making the class write a series of programs he found on the internet which were used in programming contests. I'm totally stoked on where to start with this one problem. Actually I'm totally lost as to what to do and what type of data structure I should be using. Any help would be greatly appreciated. The progam is the following:

CCSNE-2002: Second Annual College Programming Contest

Problem 4: A Mod Fibonacci

Many computer scientists are familiar with the Fibonacci sequence of numbers. The first value in this sequence is 1. The second value is also 1. The third value in the sequence is the sum of the first two values which is 2. The fourth value is the sum of the second and the third values. In general, every value after the first two is the sum of the two values before it.

Now consider a mod Fibonacci sequence. Choose an integer greater than or equal to 2. The first two values of the mod n sequence are still 1 and 1 but every value after that is the sum of the previous two values mod n. For example, if n is 5, the first few values of the sequence are 1, 1, 2, 3, 0, 3, 3, 1, 4, 0, 4, 4, 3, 2,....As another example, if n is 3, the first few values are 1, 1, 2, 0, 2, 2, 1, 0, 1, 1, 2,....Call the sequence for a given n, the mod-n Fibonacci sequence.

Since the number of possible values in a mod-n Fibonacci sequence is finite, the sequence must repeat with some period. For example, the mod-3 sequence has 8 values (1,1,2,0,2,2,1,0) that repeat continuously. We will call the period of the mod-3 Fibonacci sequence 8 since the length of the repeating sequence is 8.

Write a program which will read an interger n between 2 and 5000 and determine the period for the mod-n sequence.

ok thats the whole problem. Thanks again people

[1899 byte] By [HDesai1181a] at [2007-10-1 8:12:34]
# 1

So, the problem is quite easy to understand. First you should write your solution in pseudo code. After that, you can think of how you would implement it.

To get an idea, here's a start:

- read integer mod

- check if mod is between 2 and 5000

- use e.g. a ArrayList to let your fibonacci stream grow

- initialize the ArrayList with the first 2 elements (1 and 1)

- write a loop that only breaks if your repeat check returns true

- in your loop, compute the sum of the last 2 integers of the ArrayList

- divide the sum via your input mod

- add the result to your ArrayLIst

- the end of your loop code is the repeat check: for each sequence of numbers beginning from the first element in your ArrayList to at least the 3rd element, check if the same sequence exist after you considered sequence. If the result is true, break the loop and print out the number of your sequence elements.

I guess, it's about 30 minutes of coding.

MartinHilperta at 2007-7-9 21:15:10 > top of Java-index,Developer Tools,Java Compiler...