Pancake Number in JavaIn this section, we will discuss what is pancake number and also create Java programs with different approaches to find the pancake number. The pancake number program frequently asked in Java coding interviews and academics. Pancake NumberA pancake number Pj represents the maximum number of pieces of a circle or pancake that can be divided using j number of cuts. Pancake numbers are also known as lazy caterer numbers, more formally it is known as central polygonal numbers. Mathematically, the pancake number Pj is represented as: The following diagram shows the pancake numbers for the values j = 0 to j = 5. Pancake Number Java ProgramThe following code shows the implementation of the pancake numbers using the mathematical formula defined above. FileName: PancakeNumberExample.java Output: For j = 0, the pancake number is: 1 For j = 1, the pancake number is: 2 For j = 2, the pancake number is: 4 For j = 3, the pancake number is: 7 For j = 4, the pancake number is: 11 For j = 5, the pancake number is: 16 For j = 6, the pancake number is: 22 For j = 7, the pancake number is: 29 For j = 8, the pancake number is: 37 For j = 9, the pancake number is: 46 Pancake Number Using CombinationThe pancake numbers can also be obtained using combinations. Using combinations, the pancake numbers are defined as: Pj = 0C0 = 1, for j = 0 Pj = 1C0 + 1C1 = 1 + 1 = 2, for j = 1 For j >= 2, Pj = jC0 + jC1 + jC2 Thus, For j = 2, P2 = 2C0 + 2C1 + 2C2 = 1 + 2 + 1 = 4 For j = 3, P3 = 3C0 + 3C1 + 3C2 = 1 + 3 + 3 = 7 For j = 4, P4 = 4C0 + 4C1 + 4C2 = 1 + 4 + 6 = 11 For j = 5, P5 = 5C0 + 5C1 + 5C2 = 1 + 5 + 10 = 16 Combining the above formula for j = 0, 1, and >= 2, we get Pj = 1, for j = 0, and Pj = j + 1C2 + 1, for j >= 1 Thus, P0 = 1 P1 = 1 + 1C2 + 1 = 2C2 + 1 = 1 + 1 = 2 P2 = 2 + 1C2 + 1 = 3C2 + 1 = 3 + 1 = 4 P3 = 3 + 1C2 + 1 = 4C2 + 1 = 6 + 1 = 7 Pancake Number: Iterative ApproachLet's implement the above combination formula to compute the pancake numbers. FileName: PancakeNumberExample1.java Output: For j = 0, the pancake number is: 1 For j = 1, the pancake number is: 2 For j = 2, the pancake number is: 4 For j = 3, the pancake number is: 7 For j = 4, the pancake number is: 11 For j = 5, the pancake number is: 16 For j = 6, the pancake number is: 22 For j = 7, the pancake number is: 29 For j = 8, the pancake number is: 37 For j = 9, the pancake number is: 46 Pancake Number: Recursive ApproachThe combination formula can also be implemented using recursion. The following program shows the same. FileName: PancakeNumberExample2.java Output: For j = 0, the pancake number is: 1 For j = 1, the pancake number is: 2 For j = 2, the pancake number is: 4 For j = 3, the pancake number is: 7 For j = 4, the pancake number is: 11 For j = 5, the pancake number is: 16 For j = 6, the pancake number is: 22 For j = 7, the pancake number is: 29 For j = 8, the pancake number is: 37 For j = 9, the pancake number is: 46 Next TopicPancake Sorting in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India