寻找星号的程序

一个数字被称为星号,如果它是一个居中的数字,代表一个类似于中国棋盘游戏的居中六边形(六角星)。少数几个星号是1,13,37,73,121,181,253,337,433…。 例如:

null
Input : n = 2Output : 13Input : n = 4Output : 73Input : n = 6Output : 181

如果我们举几个例子,我们可以注意到第n个星号是由以下公式给出的:

n-th star number = 6n(n - 1) + 1 

下面是上述公式的实现。

C++

// C++ program to find star number
#include <bits/stdc++.h>
using namespace std;
// Returns n-th star number
int findStarNum( int n)
{
return (6 * n * (n - 1) + 1);
}
// Driver code
int main()
{
int n = 3;
cout << findStarNum(n);
return 0;
}


JAVA

// Java program to find star number
import java.io.*;
class GFG {
// Returns n-th star number
static int findStarNum( int n)
{
return ( 6 * n * (n - 1 ) + 1 );
}
// Driver code
public static void main(String args[])
{
int n = 3 ;
System.out.println(findStarNum(n));
}
}
// This code is contributed
// by Nikita Tiwari.


Python3

# Python3 program to
# find star number
# Returns n-th
# star number
def findStarNum(n):
return ( 6 * n * (n - 1 ) + 1 )
# Driver code
n = 3
print (findStarNum(n))
# This code is contributed by Smitha Dinesh Semwal


C#

// C# program to find star number
using System;
class GFG {
// Returns n-th star number
static int findStarNum( int n)
{
return (6 * n * (n - 1) + 1);
}
// Driver code
public static void Main()
{
int n = 3;
Console.Write(findStarNum(n));
}
}
// This code is contributed
// by vt_m.


PHP

<?php
//PHP program to find star number
// Returns n-th star number
function findStarNum( $n )
{
return (6 * $n * ( $n - 1) + 1);
}
// Driver code
$n = 3;
echo findStarNum( $n );
// This code is contributed by ajit
?>


Javascript

<script>
// Javascript program to find star number
// Returns n-th star number
function findStarNum(n)
{
return (6 * n * (n - 1) + 1);
}
// Driver code
let n = 3;
document.write(findStarNum(n));
// This code is contributed by rishavmahato348.
</script>


输出:

37

开始编号的有趣属性:

  1. 这个 数字根 一个星号的长度总是1或4,并按顺序1、4、1前进。
  2. 以10为基数的星号的最后两位数字总是01、13、21、33、37、41、53、61、73、81或93。
  3. 这个 生成函数 因为星号是
x*(x^2 + 10*x + 1) / (1-x)^3 = x + 13*x^2 + 37*x^3 +73*x^4 .......
  1. 星数满足线性递推方程
S(n) = S(n-1) + 12(n-1)

参考资料: http://mathworld.wolfram.com/StarNumber.html https://en.wikipedia.org/wiki/Star_number 本文由 丹麦拉扎 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享