📜  八角形数

📅  最后修改于: 2021-05-04 18:03:13             🧑  作者: Mango

给您一个数字n,任务是找到n八边形数字。另外,找到直到n的八角形数列。
八角形数字是代表八角形的图形数字。可以通过在正方形的四个边上放置三角形来形成八角形。 ( – 2N 3N 2)八角数量由使用下式计算。
例子 :

Input : 5
Output : 65

Input : 10
Output : 280

Input : 15
Output : 645
C++
// C++ program to find
// nth octagonal number
#include 
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


Javascript


C++
// C++ program to display the
// octagonal series
#include 
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


Javascript


输出 :

10th octagonal number : 280

给定数字n,找到直到n的八边形级数。

我们还可以找到八角形系列。八角形级数包含八角形上的点。

Octagonal series 1, 8, 21, 40, 65, 96, 133, 176, 225, 280, . . .

C++

// C++ program to display the
// octagonal series
#include 
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


Java脚本


输出 :

1, 8, 21, 40, 65, 96, 133, 176, 225, 280