Se le da un número n, la tarea es encontrar el n número octogonal. Además, encuentre la serie Octogonal hasta el n.
Un número octogonal es el número de la figura que representa el octágono. Los números octagonales se pueden formar colocando números triangulares en los cuatro lados de un cuadrado. El número octogonal se calcula usando la fórmula (3n 2 – 2n) .
Ejemplos:
Input : 5 Output : 65 Input : 10 Output : 280 Input : 15 Output : 645
C++
// C++ program to find // nth octagonal number #include <bits/stdc++.h> using namespace std; // Function to calculate //octagonal number int octagonal(int n) { // Formula for finding // nth octagonal number return 3 * n * n - 2 * n; } // Driver function int main() { int n = 10; cout << n << "th octagonal number :" << octagonal(n); return 0; }
Java
// Java program to find // nth octagonal number import java.util.*; import java.lang.*; public class GfG { // Function to calculate //octagonal number public static int octagonal(int n) { // Formula for finding // nth octagonal number return 3 * n * n - 2 * n; } // Driver function public static void main(String argc[]) { int n = 10; System.out.println(n + "th octagonal" + " number :" + octagonal(n)); } } /* This code is contributed by Sagar Shukla */
Python
# Python program to find # nth octagonal number def octagonal(n): return 3 * n * n - 2 * n # Driver code n = 10 print(n, "th octagonal number :", octagonal(n))
C#
// C# program to find nth octagonal number using System; public class GfG { // Function to calculate //octagonal number public static int octagonal(int n) { // Formula for finding // nth octagonal number return 3 * n * n - 2 * n; } // Driver function public static void Main() { int n = 10; Console.WriteLine(n + "th octagonal" + " number :" + octagonal(n)); } } /* This code is contributed by Vt_m */
PHP
<?php // PHP program to find // nth octagonal number // Function to calculate //octagonal number function octagonal($n) { // Formula for finding // nth octagonal number return 3 * $n * $n - 2 * $n; } // Driver Code $n = 10; echo $n , "th octagonal number :" , octagonal($n); // This code is contributed by Vt_m . ?>
Javascript
<script> // JavaScript program to convert // Binary code to Gray code // Function to calculate //octagonal number function octagonal(n) { // Formula for finding // nth octagonal number return 3 * n * n - 2 * n; } // Driver code let n = 10; document.write(n + "th octagonal" + " number :" + octagonal(n)); // This code is contributed by code_hunt. </script>
Producción :
10th octagonal number : 280
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Dado el número n, encuentra la serie octogonal hasta n.
También podemos encontrar la serie octogonal. La serie octogonal contiene los puntos en el octágono.
Octagonal series 1, 8, 21, 40, 65, 96, 133, 176, 225, 280, . . .
C++
// C++ program to display the // octagonal series #include <bits/stdc++.h> using namespace std; // Function to display // octagonal series void octagonalSeries(int n) { // Formula for finding //nth octagonal number for (int i = 1; i <= n; i++) // Formula for computing // octagonal number cout << (3 * i * i - 2 * i); } // Driver function int main() { int n = 10; octagonalSeries(n); return 0; }
Java
// Java program to find // nth octagonal number import java.util.*; import java.lang.*; public class GfG { // Function to display octagonal series public static void octagonalSeries(int n) { // Formula for finding //nth octagonal number for (int i = 1; i <= n; i++) // Formula for computing // octagonal number System.out.print(3 * i * i - 2 * i); } // Driver function public static void main(String argc[]) { int n = 10; octagonalSeries(n); } /* This code is contributed by Sagar Shukla */ }
Python
# Python program to find # nth octagonal number def octagonalSeries(n): for i in range(1, n + 1): print(3 * i * i - 2 * i, end = ", ") # Driver code n = 10 octagonalSeries(n)
C#
// C# program to find // nth octagonal number using System; public class GfG { // Function to display octagonal series public static void octagonalSeries(int n) { // Formula for finding //nth octagonal number for (int i = 1; i <= n; i++) // Formula for computing // octagonal number Console.Write(3 * i * i - 2 * i + ", "); } // Driver function public static void Main() { int n = 10; octagonalSeries(n); } } /* This code is contributed by Vt_m */
PHP
<?php // PHP program to display the // octagonal series // Function to display // octagonal series function octagonalSeries($n) { // Formula for finding // nth octagonal number for ($i = 1; $i <= $n; $i++) // Formula for computing // octagonal number echo (3 * $i * $i - 2 * $i),","; } // Driver Code $n = 10; octagonalSeries($n); // This code is contributed by Vt_m . ?>
Javascript
<script> // Javascript program to display the // octagonal series // Function to display // octagonal series function octagonalSeries(n) { // Formula for finding // nth octagonal number for (let i = 1; i <= n; i++) // Formula for computing // octagonal number document.write(3 * i * i - 2 * i + ", "); } // Driver Code let n = 10; octagonalSeries(n); // This code is contributed by _saurabh_jaiswal </script>
Producción :
1, 8, 21, 40, 65, 96, 133, 176, 225, 280
Complejidad temporal: O(n)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Dharmendra_Kumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA