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 P_{j} 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 P_{j} 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: P_{j} = ^{0}C_{0} = 1, for j = 0 P_{j} = ^{1}C_{0} + ^{1}C_{1} = 1 + 1 = 2, for j = 1 For j >= 2, P_{j} = ^{j}C_{0} + ^{j}C_{1} + ^{j}C_{2} Thus, For j = 2, P_{2} = ^{2}C_{0} + ^{2}C_{1} + ^{2}C_{2} = 1 + 2 + 1 = 4 For j = 3, P_{3} = ^{3}C_{0} + ^{3}C_{1} + ^{3}C_{2} = 1 + 3 + 3 = 7 For j = 4, P_{4} = ^{4}C_{0} + ^{4}C_{1} + ^{4}C_{2} = 1 + 4 + 6 = 11 For j = 5, P_{5} = ^{5}C_{0} + ^{5}C_{1} + ^{5}C_{2} = 1 + 5 + 10 = 16 Combining the above formula for j = 0, 1, and >= 2, we get P_{j} = 1, for j = 0, and P_{j} = ^{j + 1}C_{2} + 1, for j >= 1 Thus, P_{0} = 1 P_{1} = ^{1 + 1}C_{2} + 1 = ^{2}C_{2} + 1 = 1 + 1 = 2 P_{2} = ^{2 + 1}C_{2} + 1 = ^{3}C_{2} + 1 = 3 + 1 = 4 P_{3} = ^{3 + 1}C_{2} + 1 = ^{4}C_{2} + 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
