The Lazy Caterer's Problem - GeeksforGeeks

30 Mar, 2021
Given an integer n, denoting the number of cuts that can be made on a pancake, find the maximum number of pieces that can be formed by making n cuts. Examples :  
Input :  n = 1
Output : 2
With 1 cut we can divide the pancake in 2 pieces

Input :  2
Output : 4
With 2 cuts we can divide the pancake in 4 pieces

Input : 3
Output : 7
We can divide the pancake in 7 parts with 3 cuts

Input : 50
Output : 1276

 
 
Let f(n) denote the maximum number of pieces
that can be obtained by making n cuts.
Trivially,
f(0) = 1                                 
As there'd be only 1 piece without any cut.

Similarly,
f(1) = 2

Proceeding in similar fashion we can deduce 
the recursive nature of the function.
The function can be represented recursively as :
f(n) = n + f(n-1)

Hence a simple solution based on the above 
formula can run in O(n). 
We can optimize above formula.  
We now know ,
f(n) = n + f(n-1) 

Expanding f(n-1) and so on we have ,
f(n) = n + n-1 + n-2 + ...... + 1 + f(0)

which gives,
f(n) = (n*(n+1))/2 + 1
Hence with this optimization, we can answer all the queries in O(1).Below is the implementation of above idea : 
C++
Java
Python3
C#
PHP
Javascript
// A C++ program to find the solution to// The Lazy Caterer's Problem#include <iostream>using namespace std; // This function receives an integer n// and returns the maximum number of// pieces that can be made form pancake// using n cutsint findPieces(int n){    // Use the formula    return (n * ( n + 1)) / 2 + 1;} // Driver Codeint main(){    cout << findPieces(1) << endl;    cout << findPieces(2) << endl;    cout << findPieces(3) << endl;    cout << findPieces(50) << endl;    return 0;}
// Java program to find the solution to// The Lazy Caterer's Problemimport java.io.*; class GFG{    // This function returns the maximum    // number of pieces that can be made    //  form pancake using n cuts    static int findPieces(int n)    {        // Use the formula        return (n * (n + 1)) / 2 + 1;    }         // Driver program to test above function    public static void main (String[] args)    {        System.out.println(findPieces(1));        System.out.println(findPieces(2));        System.out.println(findPieces(3));        System.out.println(findPieces(50));    }} // This code is contributed by Pramod Kumar
# A Python 3 program to# find the solution to# The Lazy Caterer's Problem # This function receives an# integer n and returns the# maximum number of pieces# that can be made form# pancake using n cutsdef findPieces( n ):     # Use the formula    return (n * ( n + 1)) // 2 + 1 # Driver Codeprint(findPieces(1))print(findPieces(2))print(findPieces(3))print(findPieces(50)) # This code is contributed# by ihritik
// C# program to find the solution// to The Lazy Caterer's Problemusing System; class GFG{    // This function returns the maximum    // number of pieces that can be made    // form pancake using n cuts    static int findPieces(int n)    {        // Use the formula        return (n * (n + 1)) / 2 + 1;    }         // Driver code    public static void Main ()    {        Console.WriteLine(findPieces(1));        Console.WriteLine(findPieces(2));        Console.WriteLine(findPieces(3));        Console.Write(findPieces(50));    }} // This code is contributed by Nitin Mittal.
<?php// A php program to find// the solution to The// Lazy Caterer's Problem // This function receives// an integer n and returns// the maximum number of// pieces that can be made// form pancake using n cutsfunction findPieces($n){    // Use the formula    return ($n * ( $n + 1)) / 2 + 1;} // Driver Codeecho findPieces(1) , "\n" ;echo findPieces(2) , "\n" ;echo findPieces(3) , "\n" ;echo findPieces(50) ,"\n"; // This code is contributed// by nitin mittal.?>
<script> // Javascript program to find the solution to// The Lazy Caterer's Problem     // This function returns the maximum    // number of pieces that can be made    //  form pancake using n cuts    function findPieces(n)    {        // Use the formula        return (n * (n + 1)) / 2 + 1;    }    // Driver Code             document.write(findPieces(1) + "<br/>");        document.write(findPieces(2) + "<br/>");        document.write(findPieces(3) + "<br/>");        document.write(findPieces(50));         </script>
Output :  
2
4
7
1276
References : oeis.orgThis article is contributed by Ashutosh Kumar .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
ihritik
splevel62
Visa
Mathematical
Visa
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Modulo 10^9+7 (1000000007)
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Minimum number of jumps to reach end
Operators in C / C++
Efficient program to print all prime factors of a given number