Free Essay

# Recursive

In:

Submitted By KibblesNBits
Words 676
Pages 3
/*
* The application should be able to calculate factorials, bunny ears, nth number of fibonacci sequence,
* total number of blocks in a triangle, sum of digits (positive number), and occurence of specific
* number (7) in a positive number. I will make a menu to make a selection, and use recursion vs loops.
*/

import java.util.*; import java.util.InputMismatchException;

public class FunWithNumbersTestDrive
{
public static void main(String[] args) { Scanner input = new Scanner(System.in); // Scanner object for user input int choice, userNumber; // Integer for menu selection and another for integer to pass to methods long sevensAbound; // chose a larger primitive type for the howManySevens method to allow for more counting FunWithNumbers fun = new FunWithNumbers(); boolean done = false; try { do { fun.displayMenu(); choice = input.nextInt(); switch(choice) { case 1: System.out.print("Please enter the number that you would like to perform the factorial on: "); userNumber = input.nextInt(); System.out.println("The factoral of " + userNumber + " yields the value: " + fun.findFactorial(userNumber)); break; case 2: System.out.print("Please enter the number of fluffy bunnies whose ears you would like to count: "); userNumber = input.nextInt(); System.out.println(userNumber + " bunnies would get you " + fun.bunnyCounter(userNumber) + " ears."); break; case 3: System.out.print("Please enter the number representing the position in the Fibonacci Sequence " + "that you would like to see the value of: "); userNumber = input.nextInt(); System.out.println("The " + userNumber + " position in the Fibonacci Sequence is the value " + fun.fibonacciSequence(userNumber) + "."); break; case 4: System.out.print("Please enter the number of tiers in the triangle you would like to make: "); userNumber = input.nextInt(); System.out.println("Your triangle of " + userNumber + " height, would have " + fun.makeTriangle(userNumber) + " blocks in it."); break; case 5: System.out.print("Please enter the number whose individual digits you would like to sum: "); userNumber = input.nextInt(); System.out.println("The sum of the individual digits of the number " + userNumber + " is " + fun.addDigits(userNumber)); break; case 6: System.out.print("Plase enter the number that you would like to inspect for occurrences of 7: "); sevensAbound = input.nextLong(); System.out.println("In the number " + sevensAbound + ", there are " + fun.howManySevens(sevensAbound) + " occurrences of the number 7."); break; case 7: done = true; break; default: System.out.println("You have entered an incorrect selection, please try again! (1-7)"); } }while(!done); } catch(InputMismatchException e) { System.out.println("The input given was not numerical, aborting program"); } }
}

class FunWithNumbers
{
public static void displayMenu() { System.out.println("\nPlease make a selection from the following Menu:"); System.out.println("1. Find the factorial of a number."); System.out.println("2. Count the bunny ears of a given number of bunnies."); System.out.println("3. Determine the nth occurrence in the Fibonacci Sequence."); System.out.println("4. Figure the number of blocks to build a triangle of given number height."); System.out.println("5. Find the summation of the individual numbers that comprise a given number."); System.out.println("6. Count the occurrence of the number 7 in a given number."); System.out.println("7. Exit the Program."); } public static int findFactorial(int counter) // Takes in the number to be taken as factorial { if(counter == 1) { return 1; } else { return(counter * (findFactorial(counter - 1))); } } public static int bunnyCounter(int counter)// Takes in the number of bunnies, counts ears { if(counter == 0) { return 0; } else { return (2 + bunnyCounter(counter - 1)); } } public static int fibonacciSequence(int counter) // Delivers nth term of Fibonacci Sequence { if(counter == 0) { return 0; } else if(counter == 1) { return 1; } else { return(fibonacciSequence(counter - 2) + fibonacciSequence(counter - 1)); } } public static int makeTriangle(int counter) // Builds a triangle with base "counter", one less block per line { if(counter == 0) { return 0; } else { return(counter + makeTriangle(counter -1)); } } public static int addDigits(int number) // Sums the individual digits of the number passed { if(number == 0) { return 0; } else { return(number % 10 + addDigits(number / 10)); } } public static long howManySevens(long number) // Counts the occurrences of the number 7 in the given number { if(number == 0) { return 0; } else { long rightMostDigit = (number % 10); if(rightMostDigit == 7) { return(howManySevens(number / 10) + 1); } else { return(howManySevens(number / 10)); } } }
}

### Similar Documents

...In CRP Program •  Sign-up as a Preferred Customer •  Get a new service line OR •  Switch from your existing operator •  Refer 4 Preferred Customers or more That’s It. Done! No extra expenses, free of contract Life with free mobile ! Preferred Customer Benefits •  Referral Reward - RM120 per customer referred (one-time cash reward) •  Recursive Reward - 10% of monthly commitment from total preferred customers (up to RM10/customer) •  Appreciation Reward Life with free mobile ! How CRP Works Referrer You Life with free mobile ! Referral Reward Referral Reward You •  Direct referring – RM120/customer •  Earning more by referring more customers •  Expenses reduced become entirely FREE mobile services Life with free mobile ! Recursive Reward You After referring 4 customers ... 2 of your preferred customers will be randomly picked by the system to be moved up to your referrer. Both of them then become your Angel Customers. Life with free mobile ! Recursive Reward Referrer Your Angel Customers You “system move up” Life with free mobile ! Recursive Reward 1st Week You...

Words: 622 - Pages: 3

Free Essay

#### Computer Theory

...L2 ii) if w L2, then so is wxw L2 Find all strings in L2 with length less than 7 characters (note: ‘w’ is a meta symbol). 3. (7 pts.) Consider the recursively defined language, L3: i) ac L3 and gbb L3 ii) if w L3, then so is gwa L3 Find all strings in L3 with length less than 9 characters (note: ‘w’ is a meta symbol). 4. (7 pts.) Given the alphabet {xyz abc}, give a recursive definition for the language whose words do not contain the string xyzxyz. Notes: a. Treat ‘xyz’ and ‘abc’ as single letters (i.e. atomic tokens that cannot be decomposed). b. This must be a constructive definition (i.e. in the definition, you cannot say what is not in the language. That is, the definition cannot use constructs like: ‘not,’, ≠, +. Also you cannot use exponents in the meta variable: w+,w2. You can use repeated variables, ww or multiple variables w, x. These same constraints apply to all of the remaining recursive questions. Basically, they should look similar to Questions 2 and 3 above. 5. (7 pts.) Given the alphabet {xyz abc}, give a recursive definition for the language whose words contain the string xyzxyz (note: there are an infinite number of words in this language, same constraints as in Question 4. 6. (10 pts.) Let L = {x yy xxy} Which of the following strings are in L*, which are not? Show why you answered yes or no. a. yyxxxy, b. xyyyxxyy, c. xyyxyyxxxyy, d. yyxyyxxyxy, e. yyxyyxxyyy 7. (5 pts.)...

Words: 672 - Pages: 3

Free Essay

#### Solenet

...and Computers . . 1.2 Measuring Computing Power . . . . . . . 1.2.1 Information . . . . . . . . . . . . . 1.2.2 Representing Data . . . . . . . . . 1.2.3 Growth of Computing Power . . . 1.3 Science, Engineering, and the Liberal Arts 1.4 Summary and Roadmap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 2 3 3 8 12 13 16 Part I: Deﬁning Procedures 2 Language 2.1 Surface Forms and Meanings 2.2 Language Construction . . . . 2.3 Recursive Transition Networks 2.4 Replacement Grammars . . . 2.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 19 20 22 26 32 3 Programming 3.1 Problems with Natural Languages . . . . 3.2 Programming Languages . . . . . . . . . 3.3 Scheme . . . . . . . . . . . . . . . . . . . 3.4 Expressions . . . . . . . . . . . . . . . . . 3.4.1 Primitives . . . . . . . . . . . . . 3.4.2 Application Expressions . . . . . 3.5 Deﬁnitions . . . . . . . . . . . . . . . . . 3.6 Procedures...

Words: 58807 - Pages: 236

#### Dns Terminology

...INTERNET PERFORMANCE. DELIVERED. EBOOK The Master List Of DNS Terminology published: 01/14/15 DNS: What It Is & Why It’s Important The Domain Name System or DNS is a distributed Internet database that maps human-readable names to IP addresses, allowing people to reach the correct website when entering a URL. For example, the domain name dyn.com translates to the IP address of 216.146.45.245. DNS speed and reliability are central to the performance and availability of your website and the success of your online business. Every visitor’s first interaction with your website begins with a series of DNS queries. Poor DNS performance can lead to subpar customer experiences and lost business. Some companies rely on free or low-cost DNS services provided by ISPs, hosting providers, or domain name registrars. However, many organizations turn to a company like Dyn for Managed DNS for several reasons, including: V Expertise & Support V Global Reach V Continuous Availability V High Scalability V Security V Resiliency Who We Are Dyn is a cloud-based Internet Performance company. Dyn helps companies monitor, control, and optimize online infrastructure for an exceptional end-user experience. Through a world-class network and unrivaled, objective intelligence into Internet conditions, Dyn ensures traffic gets delivered faster, safer, and more reliably than ever. Dyn is the leading Internet Performance provider to the most visited web properties...

Words: 2131 - Pages: 9

#### Eza Ma Bt3Rf Shi La Tkon 3la Ka2Mt Al2Sdk2 ..

...3.1 Discuss the role of a high-level data model in the database design process? High-level data models assist in conceptual design and helps express data requirements of the users and includes detailed descriptions of the entity types, relationships, and constraints. The high-level data model is also used as a reference to ensure that all users’ data requirements are met and that the requirements do not include conflicts. 3.2 List the various cases where use of a null value would be appropriate? Null value would be appropriate when a particular entity does not have an applicable value for an attribute. There are two cases for such situations. The first case arises when it is known that the attribute value exists but is missing. An example for cases like such would be if the Height attribute of a person is listed as null. The second case arises when it is not known whether the attribute value exists. An example would be is the HomePhone attribute of a person is null. 3.3 Define the following terms: entity, attribute, attribute value, relationship instance, composite attribute, multivalued attribute, derived attribute, complex attribute, key attribute, value set (domain)? Entity Entity is a “thing” in the real world with an independent existence. It can also be an object with physical existence (i.e. person, car) or conceptual existence (i.e. company, job). Attribute An attribute is a particular property that describes entity (i.e. person name company...

Words: 1644 - Pages: 7

Free Essay

#### Hostel

...cout<<"------------------------------------------------------"<<endl; count = count *10; } system ("pause"); return 0; } 2. Analysis: n=3count1=count-3+1 T(n) =count-2 =O(count) =O(n) 3. Table n-th Fibonacci no. | Time 1(sec) | Time 2(sec) | Time 3(sec) | Average(sec) | 10 | 0.001 | 0.001 | 0.001 | 0.001 | 100 | 0.001 | 0.001 | 0.001 | 0.001 | 1’000 | 0.002 | 0.001 | 0.001 | 0.0013 | 10’000 | 0.003 | 0.002 | 0.001 | 0.0020 | 100’000 | 0.003 | 0.001 | 0.002 | 0.0020 | 1’000’000 | 0.007 | 0.006 | 0.007 | 0.0067 | 10’000’000 | 0.053 | 0.048 | 0.047 | 0.0493 | 100’000’000 | 0.483 | 0.454 | 0.475 | 0.4706 | 1’000’000’000 | 4.495 | 4.612 | 4.503 | 4.5367 | 10’000’000’000 | 44.401 | 45.114 | 44.984 | 44.833 | 4. Graph b. Recursive Fibonacci 1. Code: #include <iostream> #include <time.h> using...

Words: 4123 - Pages: 17

#### Examples Of Humanism In Dante's Inferno

...After Virgil and Dante’s establish their relationship, Virgil leads Dante the Pilgrim on his journey through Hell. Not only does he lead Dante on a physical journey, he also teaches Dante lesson about morality. He teaches Dante that if he pities the sinners, it would lead to a recursive process in Dante’s spiritual development. While Dante and Virgil come across Bolgia four in the eighth circle, where the fortune tellers dwell, Dante felt pity for the sinners until Virgil says, “… There is no place/ for pity here. Who is more arrogant/within his soul, who is more impious/than one who dares to sorrow at God’s judgment?” (XX: 27-30) Virgil warns Dante the pilgrim about the dangers of pitying those who defied God. His usefulness deprives from his logical understanding of divine punishment, a characteristic that Dante the Pilgrim learns throughout his spiritual journey. As Dante’s Hell depicts punishment for those who defy God’s will. The sinners in Hell deserved the punishment that he or she receives. Virgil, as Human Reasoning...

Words: 1364 - Pages: 6

#### International Monetary Fund

...o r ( s ) a n d d o n o t necessarily represent those of the Fund. WP/98/68 INTERNATIONAL MONETARY FUND Policy Development and Review Department Inflation, Disinflation, and Growth Prepared by Atish Ghosh and Steven Phillips1 Authorized for distribution by Timothy Lane May 1998 Abstract Although few would doubt that very high inflation is bad for growth, there is much less agreement about moderate inflation's effects. Using panel regressions and a nonlinear specification, this paper finds a statistically and economically significant negative relationship between inflation and growth. This relationship holds at all but the lowest inflation rates and is robust across various samples and specifications. The method of binary recursive trees identifies inflation as one the most important statistical determinants of growth. Finally, while there are short-run growth costs of disinflation, these are only relevant for the most severe disinflations, or when the initial inflation rate is well within the single-digit range. JEL Classification Numbers: E31, 040. Keywords: inflation, growth determinants, growth regressions, robustness. Authors' E-Mail Addresses: Aghosh@imf.org; Sphillips@imf.org 1 We would like to thank Kadima Kalonji for research assistance, and Alan Taylor and Maurice Obstfeld for making available their computer programs. Hugh Bredenkamp, Sharmini Coorey, Peter Doyle, Stanley Fischer, Manuel Guitian, Javier Hamann, Timothy Lane, Michael Sarel, Susan Schadler...

Words: 2757 - Pages: 12

#### Discrete Maths

...Each layer has two more logs than the layer directly above it. If the top layer has six logs, how many layers are there? Work: The 1st layer (the top one) has 6 logs The 2nd layer has 6+1(2) logs The 3rd layer has 6+2(2) The 4th layer has 6+3(2) ……………. The nth layer 6 +(n-1)(2) Total number of layers is: 6n +2(1+2+3+….+n-1) = 6n + 2(n-1)n/2 = 6n+n(n-1) = (n+5)n Then: n(n+5)=4n+110 n2+5n = 4n +110 n2 + n-110 =0 n = [-1441]/2 = [-121]/2 n = (-1+21)/2 = 10 (Taking the positive solution) Answer: 10 layers Exercise 4.2: 16. Give a recursive definition for the set of all a) positive even integers b) nonnegative even integers Answers: a) Let S the set of positive even integers Recursive definition of S: i) 2 S ii) If x S x+2 S b)...

Words: 545 - Pages: 3

#### Monetary Policy

...both recursive and structural specifications, this study analyses the effects of interest rate, money growth and the movements in nominal exchange rate on real GDP growth and inflation in Sri Lanka for the period from 1978 to 2005. The results of the recursive VARs are broadly in line with the established empirical findings, especially when the interest rate is considered the monetary policy variable. Following a positive innovation in interest rate, GDP growth and inflation decrease while the exchange rate appreciates. When money growth and exchange rate are used as policy indicators, the impact on GDP growth contrasts with established findings. However, as expected, an exchange rate appreciation has an immediate impact on the reduction of inflation. Interest rate innovations are persistent, supporting the view that the monetary authority adjusts interest rates gradually, while innovations in money growth and exchange rate appreciation are not persistent. Several puzzling results emerge from the study: for most sub-samples, inflation does not decline following a contractionary policy shock; innovations to money growth raises the interest rate; when inflation does respond, it reacts to monetary innovations faster than GDP growth does; and exchange rate appreciations almost always lead to an increase in GDP growth. The results from the semi-structural VARs, which impose identification restrictions only on the policy block, are not different from those obtained from recursive VARs...

Words: 18533 - Pages: 75

#### Operating System Course Analysis

...assignments dependent on running a VirtualBox in Oracle. One of the most difficult assignments involved doing benchmark testing of whether recursive or iterative methods ran faster. This assignment was more complex than standard assignments from previous courses because it entailed more research outside of the regular program testing. It was imperative to use step wise development in order to complete the assignment properly, because it allowed our team to break down the whole assignment into more manageable tasks on a schedule. Of course, the first thing I had decided to do was to make sure all the tasks were split up evenly between team members. We ended up with a parent process and two children. The parent would spawn the children after receiving an integer from the user by forking. The parent also set up a message queue and would wait while the child processes were running. The first child would calculate the factorial number given by the user recursively, outputting the time taken to the screen. The second child would use that same number but calculate using iteration....

Words: 469 - Pages: 2

#### Path Analysis

...การวิเคราะห์เส้นทาง (Path Analysis) ความหมายการวิเคราะห์เส้นทาง การวิเคราะห์เส้นทาง เป็นการวิเคราะห์สาเหตุ โดยนักวิจัยต้องการศึกษาสาเหตุ ของปรากฏการณ์ต่าง ๆว่ามาจากอิทธิพลของสิ่งใด กล่าวอีกนัยหนึ่งก็คือ นักวิจัยต้องการ ค้นหาว่าตัวแปรตามที่กำลังศึกษานั้น เกิดจากอิทธิพลของตัวแปรอิสระอะไรบ้าง และตัว แปรอิสระนั้นมีอิทธิพลต่อตัวแปรตามมากน้อยเพียงใด (สำเริง บุญเรืองรัตน์, หน้า 91) การอธิบายสาเหตุของปรากฏการณ์ นักวิจัยต้องอาศัยความรู้และทฤษฏีต่าง ๆ มา วิเคราะห์เพื่อตั้งสมมติฐานด้วยการสร้างแผนภาพ (Diagram) แสดงเหตุต่าง ๆ ที่มีอิทธิพล ต่อสิ่งที่ตนเองกำลังศึกษาอยู่ เมื่อนักวิจัยสร้างรูปแบบแผนภาพแสดงสาเหตุดังกล่าวแล้ว ก็หาวิธีทดสอบว่า แผนภาพดังกล่าวเป็นไปตามสมมติฐานที่ตั้งไว้หรือไม่ โดยการวิเคราะห์เส้นทาง (Path Analysis) เทคนิคนี้ ไรท์ (Wright,1934) เป็นผู้คิดค้นขึ้นมา การทดสอบสมมติฐานอาจใช้ วิธีการคำนวณโดยใช้โปรแกรมลิสเรล (LISREL) (การเขียนแผนภาพเพื่อนำไปเขียนคำสั่ง วิเคราะห์ในโปรแกรม LISREL จากตัวแปรอิสระไปยังตัวแปรตามใช้สัญลักษณ์เป็น GA และจากตัวแปรตามหนึ่งไปยังอีกตัวหนึ่งใช้สัญลักษณ์เป็น BE และเขียนตัวเลขกำกับ เส้นทางเรียงจากปลายลูกศรไปต้นลูกศร) ซึ่งโปรแกรม LISREL จะคำนวณค่าสัมประสิทธิ์ สหสัมพันธ์และระดับนัยสำคัญของแต่ละเส้นทาง พร้อมแนะนำเส้นทางที่เหมาะสมให้ด้วย ดังตัวอย่างที่จะกล่าวต่อไปในตอนสุดท้าย ความรู้เบื้องต้นเกี่ยวกับการวิเคราะห์เส้นทาง การวิเคราะห์เส้นทาง (Path analysis) เป็นการศึกษาความสัมพันธ์ของตัวแปรในเชิงเหตุและผล เป็นวิธีที่มีพื้นฐานทางสถิติมาจากการวิเคราะห์การถดถอย (Regression analysis) โดยอาศัยแผนภาพและสมการโครงสร้างของแผนภาพเป็นหลักในการนำม...

Words: 921 - Pages: 4

Free Essay

#### Hello World

Words: 268 - Pages: 2

Free Essay