Considere un cubo 3D y n planos. Podemos hacer cortes en el cubo usando planos dados. Un Número de Torta para un n dado es el número máximo de regiones que pueden estar formadas por n planos.
Serie después de n cortes de plano (0 ≤ n) :
1, 2, 4, 8, 15, 26, 42, 64, 93, 130, 176, 232, 299, 378, 470, 576, 697…
Ejemplos:
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
La fórmula del n-ésimo término del número de pastel:
Número de pastel n = n C 3 + n C 2 + n C 1 + n C 0
= (n 3 + 5*n + 6) / 6
A continuación se muestra la implementación del enfoque anterior:
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; }
C
// C Program to find the // nth Cake number #include <stdio.h> // 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; printf("%d\n",number_cake(n)); // For 8th cake Number n = 8; printf("%d\n",number_cake(n)); // For 25th cake Number n = 25; printf("%d\n",number_cake(n)); return 0; } // This code is contributed by kothavvsaakash
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>
Producción :
4 93 2626
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)