Skip to content
Related Articles

Related Articles

Improve Article
Cake number
  • Last Updated : 05 May, 2021

Consider a 3D cube and n planes. We can make cuts in the cube using given planes. A Cake Number for a given n is the maximum number of regions that can be formed by n planes. 
Series after n plane cuts (0 ≤ n)
1, 2, 4, 8, 15, 26, 42, 64, 93, 130, 176, 232, 299, 378, 470, 576, 697…

Examples: 

Input : 1
Output : 2
Explanation : 
With 1 plane cut the cube is divided into 2 regions

Input : 2
Output : 4
Explanation: 
With 2 plane cuts, we can divide the cube into 4 regions

Input : 4
Output : 15

Input : 5
Output : 26

The formula of nth Term of Cake number: 
 

n-th Cake Number = nC3 + nC2 + nC1 + nC0 
= (n3 + 5*n + 6) / 6

Below is the implementation of above approach : 
 



C++




// CPP Program to find the
// nth Cake number
#include <iostream>
using namespace std;
 
// function for Cake number
int number_cake(int n)
{
    // formula for find  Cake number
    // nth term
    return (n * n * n + 5 * n + 6) / 6;
}
 
// Driver Code
int main()
{
    // For 2nd cake Number
    int n = 2;
    cout << number_cake(n) << endl;
 
    // For 8th cake Number
    n = 8;
    cout << number_cake(n) << endl;
    // For 25th cake Number
    n = 25;
    cout << number_cake(n) << endl;
 
    return 0;
}

Java




// Java Program to find the nth Cake number
import java.io.*;
 
class GFG {
     
    // function for Cake number
    static int number_cake(int n)
    {
         
        // formula for find Cake number
        // nth term
        return (n * n * n + 5 * n + 6) / 6;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        // For 2nd cake Number
        int n = 2;
        System.out.println( number_cake(n));
     
        // For 8th cake Number
        n = 8;
        System.out.println( number_cake(n));
         
        // For 25th cake Number
        n = 25;
        System.out.println( number_cake(n));
    }
}
 
// This code is contributed by anuj_67.

Python3




# Python program to find
# nth Cake number
 
# Function to calculate
# Cake number
def number_cake(n):
 
    # Formula to calculate nth
    # Cake number
     
    return (n * n * n + 5 * n + 6) // 6
 
# Driver Code
n = 2
print(number_cake(n))
 
n = 8
print(number_cake(n))
 
n = 25
print(number_cake(n))
                     
# This code is contributed by aj_36                

C#




// C# Program to find the nth Cake number
using System;
 
class GFG {
     
    // function for Cake number
    static int number_cake(int n)
    {
         
        // formula for find Cake number
        // nth term
        return (n * n * n + 5 * n + 6) / 6;
    }
     
    // Driver Code
    public static void Main ()
    {
        // For 2nd cake Number
        int n = 2;
        Console.WriteLine( number_cake(n));
     
        // For 8th cake Number
        n = 8;
        Console.WriteLine( number_cake(n));
         
        // For 25th cake Number
        n = 25;
        Console.WriteLine( number_cake(n));
    }
}
 
// This code is contributed by anuj_67.

PHP




<?php
// PHP Program to find the
// nth Cake number
 
 
// function for Cake number
function number_cake( $n)
{
    // formula for find Cake
    // number nth term
    return ($n * $n * $n +
             5 * $n + 6) / 6;
}
 
// Driver Code
 
// For 2nd cake Number
$n = 2;
echo number_cake($n) ,"\n";
 
// For 8th cake Number
$n = 8;
echo number_cake($n)," \n";
// For 25th cake Number
$n = 25;
echo number_cake($n);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
    // Javascript Program to find the nth Cake number
     
    // function for Cake number
    function number_cake(n)
    {
           
        // formula for find Cake number
        // nth term
        return parseInt((n * n * n + 5 * n + 6) / 6, 10);
    }
     
    // For 2nd cake Number
    let n = 2;
    document.write( number_cake(n) + "</br>");
 
    // For 8th cake Number
    n = 8;
    document.write( number_cake(n) + "</br>");
 
    // For 25th cake Number
    n = 25;
    document.write( number_cake(n));
 
</script>

Output : 
 

4
93
2626

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :