Re: Fibonacci program



spartacus wrote:
hey

i have to program a cobol program where the prompt lists the fibonacci
number sequence with a maximum of 9 digits. i'm supposed to use COMPUTE
and ON SIZE ERROR.

can anyone please help?

thanks a lot in advance
 spartacus

Jeepers ! The replies you received, (except for Robert's). I'll let you into a secret. Judson is a self-confessed math-geek - believes if you aren't into math you should be given a pink slip and shouldn't be programming :-)

Mike's solution - your prof/teacher wouldn't even give you and "E" for Effort ! And Mike, how come you managed to bugger up the layout of your source ?

So you are a newbie at school and your prof/teacher throws a zinger at you and you really don't know which way is up - 'Fibonacci', 'Compute', 'On Size Error' .....

Let's break it down. FIBONACCI - have you got that squirreled away ? Try the following from Dan Brown's "da Vinci Code" :-

"Chapter 8 : Langdon couldn't tear his eyes from the glowing purple text scrawled across the parquet floor. Jacques Sauniere's final communication seemed as unlikely a departing message as any Langdon could imagine. The message read:

	13 - 3 - 2 - 21 - 1 - 1 - 8 - 5
	O, Draconian devil !
	Oh, lame saint !

Although Langdon hadn't the slightest idea what it meant, he did understand Fache's instinct that the pentacle had something to do with devil worship. ............"

"Chapter 11 : "Une plaisanterie numerique ?", Bezu Fache was livid, glaring at Sophie Neveu in disbelief. .......
"This code", Sophie explained in rapid French, "is simplistic to the point of absurdity. Jacques Sauniere must have known we would see through it immediately". She pulled a scrap of paper from, her sweater pocket and handed it to Fache. "Here is the decryption".


Fache looked at the card.

	1 - 1 - 2 - 3 - 5 - 8 - 13 - 21

"This is it ?" he snapped. "All you did was put the numbers in increasing order !"............ "Captain", Sophie said, her tone dangerously defiant, "the sequence of numbers you have in your hand happens to be one of the most famous mathematical progressions in history". .......... "This is the Fibonacci sequence", she declared, nodding toward the piece of paper in Fache's hand. "A progression in which each term is equal to the sum of the two preceding terms"....
"Mathematician Leonardo Fibonacci created this succession of numbers in the thirteenth century"................"


Not a bad explanation for *us* non-rocket scientists. Hint, hint, Sophie's phrase "A PROGRESSION IN WHICH EACH TERM IS EQUAL TO THE SUM OF THE TWO PRECEDING TERMS".

COMPUTE :

OK, you can express a problem as several lines of code which will work.
Up here in the Great White North we have GST (Goods and Services Tax), just like the EU's VAT.


You want to sell something for $100 but there's GST of 7% - therefore final price $107.

How much GST in $107 - EASY - $7. But how much GST in $253.77 ?
Pseudo code :

a)GSTNumber = 7/107
b)GSTAmount = SalePrice - (GSTNumber)
c)TicketPrice = SalePrice - GSTAmount

Above code does the trick, but prof/teacher wants you to come up with something more elegant, like :-

Compute TicketPrice Rounded = SalePrice - (SalePrice * 0.0654)

ON SIZE ERROR

Why is he asking you for this ? He's told you to allow for pic 9(09), "nine-slots" for a number. So if you and I add 1 to 999,999,999 we know the answer is 1,000,000,000. But what happens if you add 1 to 999,999,999 contained in a pic 9(09) ? Hence the ON SIZE ERROR. (Not too many people use ON SIZE ERROR by the way, ensuring their fields are big enough to take the largest result - but your prof/teacher is quizzing you).

SOLUTION :

Believe me I'm no rocket scientist and couldn't have explained Fibonacci without first having come across it in the 'da Vinci Code'.
So I gave it a shot; to nudge you along :-


- I used three variables (fields)
- I used a PERFORM/END PERFORM varying n from 1 by 1 until n > 10
- One line of COMPUTE
- then I MOVED values from RIGHT TO LEFT
- I didn't bother with ON SIZE ERROR, just confirming my number increments were correct.


In your case, presumably, you enter a start number which is reasonably close to 999,999,999, so that you don't have too many iterations of the PERFORM. So something like :-

COMPUTE this.............
ON SIZE ERROR do this
NOT ON SIZE ERROR
display result, (OR write result to a file record) END-COMPUTE



After the words PROCEDURE DIVISION - I have a total of 9 lines of code.
Perhaps not 'clever' but it does it and is clear to read. I wonder what Judson's solution would look like, (some while after you have submitted your test paper) ?


Hope above helps - give it a shot !

Jimmy, Calgary AB
	
.



Relevant Pages

  • Re: c language
    ... this sequence was described by the Indian mathematicians Gopala and Hemachandra in 1150, who were investigating the possible ways of exactly bin packing items of length 1 and 2. ... In the West, it was first studied by Leonardo of Pisa, who was also known as Fibonacci, to describe the growth of an idealised rabbit population. ... Every positive integer can be written in a unique way as the sum of one or more distinct Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers. ... For example, the solutions to reaction-diffusion differential equations can show such a patterning; in biology, genes often express themselves through gene regulatory networks, that is, in terms of several enzymes controlling a reaction, which can be modelled with reaction-diffusion equations. ...
    (comp.programming)
  • Re: Whats with this sequence?
    ... number and the corresponding Fibonacci number. ... Starting with the first triangle number and the first ... Gives this resulting sequence -- ... difference table is all zeroes from its 3rd row onward. ...
    (sci.math)
  • ++var VS var++
    ... that ++var is faster than var++ as it just adds one and returns the value, ... The above code generates 17 seconds if the compiler is using optimising and ... As I previously said, I was experimenting with the fibonacci sequence, so I ...
    (alt.comp.lang.learn.c-cpp)
  • Re: 1/89 and the Fibonacci sequence-
    ... You will see the Fibonacci numbers being generated when you develop the power series, and you will see them to fall into place when you multiply out the identity ... All power series developments of reciprocals of polynomials yield recurrent relations for the coefficients. ... >>Then adding the Fibonacci sequence in this manner -- ... >>Will this continue repeating the period of 1/89>no matter how many fibonacci numbers are added>in this manner? ...
    (sci.math)
  • Re: Whats with this sequence?
    ... number and the corresponding Fibonacci number. ... Gives this resulting sequence -- ... The 3rd deltas are the ... are the (deltas of triangles) minus. ...
    (sci.math)