Program to find Star number
Last Updated :
13 Sep, 2023
A number is termed as star number, if it is a centered figurate number that represents a centered hexagram (six-pointed star) similar to chinese checker game. The few star numbers are 1, 13, 37, 73, 121, 181, 253, 337, 433, ….
Examples:
Input : n = 2
Output : 13
Input : n = 4
Output : 73
Input : n = 6
Output : 181
If we take few examples, we can notice that the n-th star number is given by the formula:
n-th star number = 6n(n - 1) + 1
Below is the implementation of above formula.
C++
#include <bits/stdc++.h>
using namespace std;
int findStarNum( int n)
{
return (6 * n * (n - 1) + 1);
}
int main()
{
int n = 3;
cout << findStarNum(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findStarNum( int n)
{
return ( 6 * n * (n - 1 ) + 1 );
}
public static void main(String args[])
{
int n = 3 ;
System.out.println(findStarNum(n));
}
}
|
Python3
def findStarNum(n):
return ( 6 * n * (n - 1 ) + 1 )
n = 3
print (findStarNum(n))
|
C#
using System;
class GFG {
static int findStarNum( int n)
{
return (6 * n * (n - 1) + 1);
}
public static void Main()
{
int n = 3;
Console.Write(findStarNum(n));
}
}
|
PHP
<?php
function findStarNum( $n )
{
return (6 * $n * ( $n - 1) + 1);
}
$n = 3;
echo findStarNum( $n );
?>
|
Javascript
<script>
function findStarNum(n)
{
return (6 * n * (n - 1) + 1);
}
let n = 3;
document.write(findStarNum(n));
</script>
|
Output :
37
Time complexity: O(1) since performing constant operations
Space complexity: O(1) since using constant variables
Interesting Properties of Start Numbers:
- The digital root of a star number is always 1 or 4, and progresses in the sequence 1, 4, 1.
- The last two digits of a star number in base 10 are always 01, 13, 21, 33, 37, 41, 53, 61, 73, 81, or 93.
- The generating function for the star numbers is
x*(x^2 + 10*x + 1) / (1-x)^3 = x + 13*x^2 + 37*x^3 +73*x^4 .......
- The star numbers satisfy the linear recurrence equation
S(n) = S(n-1) + 12(n-1)
References :
http://mathworld.wolfram.com/StarNumber.html
https://en.wikipedia.org/wiki/Star_number
Share your thoughts in the comments
Please Login to comment...