给定数字n,找到第n个中心十进制数。
居中十进制数是居中的图形编号,它表示一个十进制,其点位于中心,而所有其他点以连续的十进制形式围绕它。来源[Wiki]。
前几个居中十进制数为:
1,11,31,61,101,151,211,281,361,451,551,661…………
例子 :
Input : 3
Output : 31
Input : 6
Output : 151
在数学中,第n个项的中心十进制数由下式给出:
以下是上述想法的基本实现。
C++
// Program to find nth
// centered decagonal
// number
#include
using namespace std;
// Centered decagonal
// number function
int centereddecagonalnum(int n)
{
// Formula to calculate nth
// centered decagonal number &
// return it into main function.
return (5 * n * n + 5 * n + 1);
}
// Driver Code
int main()
{
int n = 5;
cout << n << "th centered decagonal"
<< "number: ";
cout << centereddecagonalnum(n);
cout << endl;
n = 9;
cout << n << "th centered decagonal"
<< "number: ";
cout << centereddecagonalnum(n);
return 0;
}
Java
// Java Program to find nth
// centered decagonal number
import java.io.*;
class GFG
{
// Centered decagonal
// number function
static int centereddecagonalnum(int n)
{
// Formula to calculate nth
// centered decagonal number &
// return it into main function.
return (5 * n * n + 5 * n + 1);
}
// Driver Code
public static void main (String[] args)
{
int n = 5;
System.out.print(n + "th centered " +
"decagonal number: ");
System.out.println(centereddecagonalnum(n));
n = 9;
System.out.print(n + "th centered " +
"decagonal number: ");
System.out.println(centereddecagonalnum(n));
}
}
// This code is contributed by m_kit
Python3
# Program to find nth
# centered decagonal number
# Centered decagonal
# number function
def centereddecagonalnum(n) :
# Formula to calculate
# nth centered decagonal
# number & return it
# into main function.
return (5 * n * n +
5 * n + 1)
# Driver Code
if __name__ == '__main__' :
n = 5
print(n,"th centered decagonal " +
"number : ",
centereddecagonalnum(n))
n = 9
print(n,"th centered decagonal " +
"number : ",
centereddecagonalnum(n))
# This code is contributed by m_kit
C#
// Program to find nth
// centered decagonal
// number
using System;
class GFG
{
// Centered decagonal
// number function
static int centereddecagonalnum(int n)
{
// Formula to calculate nth
// centered decagonal number &
// return it into main function.
return (5 * n * n + 5 * n + 1);
}
// Driver Code
static public void Main ()
{
int n = 5;
Console.Write(n + "th centered decagonal"+
"number: ");
Console.WriteLine(centereddecagonalnum(n));
n = 9;
Console.Write(n + "th centered decagonal"+
"number: ");
Console.WriteLine(centereddecagonalnum(n));
}
}
// This code is contributed by aj_36
PHP
Javascript
输出
5th centered decagonalnumber: 151
9th centered decagonalnumber: 451