Project2Fall2011 - PROJECT 2 INTRODUCTION In ...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: PROJECT 2 INTRODUCTION In this project, you will deal with the Lucas sequence. Just like the Fibonacci sequence, the Lucas numbers are obtained by adding the two previous in line. The first Lucas numbers are given below: 2, 1, 3, 4, 7, 11, 18, 29, 47 ….. In the code you will have to write the following 4 operations are required (they are also in increasing order of difficulty): 1. Detect if a given number is in the Lucas sequence or not. 2. Find the n- th in line Lucas number. 3. Compute the summation of all Lucas numbers up to a certain point. 4. Find out if a given two digit number is a Carol number or not. BASIC INFORMATION As you will notice, the program you are required to write looks more professional than the first one. What does that imply? First of all, the menu appears again and again until the user decides to quit pressing zero (0) in the main menu. Secondly, there are some precautions that have been taken to ensure that the program get executed correctly. For example, • in the main menu, the user might input a wrong choice and/or a character instead of a number. In both cases the message “Wrong choice. Please choose again.” • in all options, notice that the number inputted needs to be positive • in the fourth option, the number needs to be a two digit number. OPTION 1: Detecting if a number is in the Lucas sequence or not In the first option, the user is prompted to give a positive number and then your algorithm should output if the actual number belongs to the Lucas sequence or not. There are a few ways to do that, however I will provide a hint towards one of them. Suppose that you have two consecutive numbers in the Lucas sequence, let them be a and b. Then, the next Lucas number can be easily computed as c=a+b. Now let us consider the input of the user in comparison to the Lucas number c that we have just calculated. There are three ways that these two compare: 1. c and num are equal. If that is the case then, clearly, num is in the Lucas number. 2. c is bigger than num. In this case, num is not a Lucas number. The reason is that considering consecutive c’s, we have reached at the point where c has become larger than num and we have not yet come across num. 3. c is smaller than num. This is actually the most interesting case. Let us see a small example for these three cases and we will return to the third case hint. Consider that the number the user inputted (num) is equal to 16. And assume that you have already checked all Lucas numbers 2, 1, 3, 4, 7, 11. The next in line is 18. Comparing num(16) with the current c (18), we deduce that 16 is NOT a Lucas number. Now consider that the number the user inputted is equal to 7. Also assume that we have already seen the numbers 2, 1, 3, 4. The next in line (current c) is equal to num, hence we can say that 7 is a Lucas number. Last case, the third one. Assume that the user has inputted 1201231. Also assume that you have already checked 2, 1, 3, 4, 7, 11, 18. The next in line (current c) is 29. Clearly, 29 < 1201231. Can we deduce anything for the given number? The answer is NO. The algorithmic idea that we are discussing here actually says that if the number I am checking at the moment is smaller than the number that I am trying to detect, then I need to proceed on to the next number in line. By doing that, I am guaranteed to reach at some point one of the first two cases and hence decide if my number is a Lucas number or not. OPTION 2: Computing the n- th Lucas number This option is a little simpler, however it might require a little trial and error. The idea is that given the Lucas sequence: 2, 1, 3, 4, 7, 11, 18, 29, 47 ….. and given a rank (n), we can retrieve the n- th Lucas number. For example, n=2 - > 2- nd lucas number is 1. n=6 - > 6- th lucas number is 11. The idea is similar to before. However now we don’t stop increasing our current number c depending on a given number, but depending on the number of iterations that we are required to perform. OPTION 3: The summation of all Lucas numbers up to a bound Once more, you will be required to compute all Lucas numbers up to a certain number (similarly to the first option). The difference now is that you are required to keep track of a summation, more specifically, the summation of all Lucas numbers you have encountered up to that point. Let us see a small example: 1. The user inputs a bound of 23: Lucas numbers up to 23 are: 2, 1, 3, 4, 7, 11, 18. Hence the sum is 2+1+3+4+7+11+18=46. 2. The user inputs a bound of 18: Lucas numbers up to 18 are: 2, 1, 3, 4, 7, 11, 18. Hence the sum is 2+1+3+4+7+11+18=46. 3. The user inputs a bound of 17: Lucas numbers up to 17 are: 2, 1, 3, 4, 7, 11. Hence the sum is 2+1+3+4+7+11=28. OPTION 4: Detecting two- digit Carol numbers Given a sequence of numbers, a two- digit Carol number is the one that belongs to the sequence, and so do the two digits that make it up. For example, assume that we have the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144… (the Fibonacci sequence), then 13 would be a Carol number since 13, 1 and 3 all belong to the sequence. The same thing would apply to 21, since 21, 2 and 1 all belong to the Fibonacci numbers. Hence, in order to find a Carol number, you must first decompose it into the two digits that it consists of and, then, try to see if it meets the requirements to be a Carol number. SAMPLE RUN Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 9 Wrong choice. Please choose again. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 1 Please give the number you want to check: -1 Only positive numbers are accepted. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 1 Please give the number you want to check: 5 5 is NOT a Lucas number. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 1 Please give the number you want to check: 7 7 IS a Lucas number. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 2 Please give the rank of the number you are looking for: 5 7 is the 5-th Lucas number. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 2 Please give the rank of the number you are looking for: -2 Only positive numbers are accepted. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 2 Please give the rank of the number you are looking for: 3 3 is the 3-th Lucas number. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 3 Please enter the upper bound of the summation you are looking for: 21 46 is the summation of all Lucas numbers until 21. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 3 Please enter the upper bound of the summation you are looking for: 29 75 is the summation of all Lucas numbers until 29. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 4 Please give a two digit, positive number: 47 47 IS a Carol number. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 4 Please give a two digit, positive number: 76 76 is NOT a Carol number. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 4 Please give a two digit, positive number: 5 Only two digit positive numbers are accepted. Lucas Numbers Calculator Please enter one of the following choices. 1. To find if a number belongs to the series or not. 2. To find the n-th number in rank. 3. To compute the sum of all Lucas numbers up to a given bound 4. To check if a given two digit number is a Carol number. Please choose: 0 Now quitting.. ...
View Full Document

This note was uploaded on 09/18/2011 for the course CHM 3218 taught by Professor Stewart during the Spring '08 term at University of Florida.

Ask a homework question - tutors are online