Nonagonal number
Last Updated :
07 Oct, 2021
A nonagonal number is a figurate number that extends the concept of triangular and square numbers to the nonagon. Specifically, the nth nonagonal numbers count the number of dots in a pattern of n nested nonagons (9 sided polygons), all sharing a common corner, where the ith nonagon in the pattern has sides made of i dots spaced one unit apart from each other.
Examples :
Input : n = 10
n (7n - 5) / 2
10 * (7 * 10 - 5) / 2
10 * 65 / 2 = 325
Output :325
Input : 15
Output :750
The nth nonagonal number is given by the formula: n (7n – 5) / 2.
C++
#include <bits/stdc++.h>
using namespace std;
int Nonagonal( int n)
{
return n * (7 * n - 5) / 2;
}
int main()
{
int n = 10;
cout << Nonagonal(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int Nonagonal( int n)
{
return n * ( 7 * n - 5 ) / 2 ;
}
public static void main(String args[])
{
int n = 10 ;
System.out.println(Nonagonal(n));
}
}
|
Python3
def Nonagonal(n):
return int (n * ( 7 * n - 5 ) / 2 )
n = 10
print (Nonagonal(n))
|
C#
using System;
class GFG {
static int Nonagonal( int n)
{
return n * (7 * n - 5) / 2;
}
public static void Main()
{
int n = 10;
Console.Write(Nonagonal(n));
}
}
|
PHP
<?php
function Nonagonal( $n )
{
return $n * (7 * $n - 5) / 2;
}
$n = 10;
echo Nonagonal( $n );
?>
|
Javascript
<script>
function Nonagonal(n)
{
return parseInt(n * (7 * n - 5) / 2);
}
let n = 10;
document.write(Nonagonal(n));
</script>
|
Output :
325
Time Complexity: O(1)
Auxiliary Space: O(1)
Given a number n, find nonagonal number series till n terms.
C++
#include <bits/stdc++.h>
using namespace std;
int Nonagonal( int n)
{
for ( int i = 1; i <= n; i++)
{
cout << i * (7 * i - 5) / 2;
cout << " " ;
}
}
int main()
{
int n = 10;
Nonagonal(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void Nonagonal( int n)
{
for ( int i = 1 ; i <= n; i++)
{
System.out.print(i * ( 7 *
i - 5 ) / 2 );
System.out.print( " " );
}
}
public static void main(String args[])
{
int n = 10 ;
Nonagonal(n);
}
}
|
Python3
def Nonagonal(n):
for i in range ( 1 , n + 1 ):
print ( int (i * ( 7 * i - 5 ) / 2 ),end = " " )
n = 10
Nonagonal(n)
|
C#
using System;
class GFG
{
static void Nonagonal( int n)
{
for ( int i = 1; i <= n; i++)
{
Console.Write(i * (7 *
i - 5) / 2);
Console.Write( " " );
}
}
public static void Main()
{
int n = 10;
Nonagonal(n);
}
}
|
PHP
<?php
function Nonagonal( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
{
echo $i * (7 * $i - 5) / 2;
echo " " ;
}
}
$n = 10;
Nonagonal( $n );
?>
|
Javascript
<script>
function Nonagonal(n)
{
for (let i = 1; i <= n; i++)
{
document.write(parseInt(i * (7 * i - 5) / 2) + " " );
}
}
let n = 10;
Nonagonal(n);
</script>
|
Output :
1 9 24 46 75 111 154 204 261 325
Time Complexity: O(n)
Auxiliary Space: O(1)
Reference:https://en.wikipedia.org/wiki/Nonagonal_number
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...