Re: Fibonacci program
- From: "James J. Gavan" <jgavandeletethis@xxxxxxx>
- Date: Sun, 23 Oct 2005 22:47:42 GMT
spartacus wrote:
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 :-)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
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 .
- Follow-Ups:
- Re: Fibonacci program
- From: Mike B
- Re: Fibonacci program
- References:
- Fibonacci program
- From: spartacus
- Fibonacci program
- Prev by Date: Re: Fibonacci program
- Next by Date: Re: Fibonacci program
- Previous by thread: Re: Fibonacci program
- Next by thread: Re: Fibonacci program
- Index(es):
Relevant Pages
|
|