Centered Octahedral number

We are given a number n, we need to find n-th centered octahedral number.

Description: A centered octahedral number is a figurate number. It counts the number of points of a three-dimensional integer lattice that lie inside an octahedron centered at the origin. The same numbers are special cases of the Delannoy numbers, which count certain two-dimensional lattice paths.

The First Few Centered octahedral numbers (where n = 0, 1, 2, 3…….) are :
1, 7, 25, 63, 129, 231, 377, 575, 833, 1159………………………….

Mathematics formula for nth Centered octahedral number:

  \begin{math}Co_{n} = \frac{(2n+1)(2n^2+2n+3)}{3}  

Examples :

Input : n = 6
Output : 377

Input : n = 15
Output : 4991

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find nth
// Centered octahedral number
#include <bits/stdc++.h>
using namespace std;
  
// Function to find
// Centered octahedral number
  
int centeredOctahedral(int n)
{
    // Formula to calculate nth
    // Centered octahedral number
    // and return it into main function.
    return (2 * n + 1) * (2 * n * n + 2 * n + 3) / 3;
}
  
// Driver Code
int main()
{
    int n = 3;
    cout << centeredOctahedral(n) << endl;
  
    n = 9;
    cout << centeredOctahedral(n) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find nth
// Centered octahedral number
import java.io.*;
  
class GFG
{
    // Function to find
    // Centered octahedral number
    static int centeredOctahedral(int n)
    {
          
    // Formula to calculate nth
    // Centered octahedral number
    // and return it into main function.
      
    return (2 * n + 1) * 
           (2 * n * n + 2 * n + 3) / 3;
    }
  
    // Driver Code
    public static void main (String[] args) 
    {
    int n = 3;
    System.out.print( centeredOctahedral(n));
    System.out.println();
    n = 9;
    System.out.print(centeredOctahedral(n));
    }
}
  
// This code is contributed by aj_36

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 Program to find nth 
# Centered octahedral number
  
# Centered octahedral 
# number function
def centeredOctahedral(n) :
      
    # Formula to calculate nth
    # Centered octahedral number
    # return it into main function.
    return (2 * n + 1) * (
            2 * n * n + 
            2 * n + 3) // 3
  
# Driver Code
if __name__ == '__main__' :
          
    n = 3
    print(centeredOctahedral(n))
    n = 9
    print(centeredOctahedral(n))
  
# This code is contributed ajit

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find nth
// Centered octahedral number
using System;
  
public class GFG {
      
    // Function to find
    // Centered octahedral number
    static int centeredOctahedral(int n)
    {
          
        // Formula to calculate nth
        // Centered octahedral number
        // and return it into main function.
          
        return (2 * n + 1) * 
            (2 * n * n + 2 * n + 3) / 3;
    }
  
    // Driver Code
    static public void Main ()
    {
        int n = 3;
        Console.WriteLine( 
               centeredOctahedral(n));
  
        n = 9;
        Console.WriteLine(
              centeredOctahedral(n));
    }
}
  
// This code is contributed by m_kit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find nth
// Centered octahedral number
  
// Function to find
// Centered octahedral number
function centeredOctahedral($n)
{
    // Formula to calculate 
    // nth Centered octahedral 
    // number and return it 
    // into main function.
    return (2 * $n + 1) * 
           (2 * $n * $n
            2 * $n + 3) / 3;
}
  
// Driver Code
$n = 3;
echo centeredOctahedral($n), "\n";
  
$n = 9;
echo centeredOctahedral($n), "\n";
  
// This code is contributed ajit
?>

chevron_right


Output :

63
1159

Reference:
https://en.wikipedia.org/wiki/Centered_octahedral_number



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t