Find the Nth Hogben Numbers
Last Updated :
29 Dec, 2022
Given a number N, the task is to print the Nth Hogben number.
Hogben Number: In a spiral arrangement of the integers, Hogben Numbers appear on the main diagonal (see the picture below).
The first few Hogben numbers are 1, 3, 7, 13, 21, 31, 43, 57, 73, 91, 111, 133, 157, 183, 211, 241, 273……. and many more.
Example:
Input: N = 4
Output: 3
Explanation:
The 4th Hogben number that lies on the diagonal of the spiral pattern is 13.
Input: N = 7
Output: 43
Explanation:
The 7th Hogben number that lies on the diagonal of the spiral pattern is 43.
Approach:
We can observe from the sequence of Hogben numbers, that the Nth Hogben number HN is equal to .
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int HogbenNumber( int a)
{
int p = ( pow (a, 2) - a + 1);
return p;
}
int main()
{
int N = 10;
cout << HogbenNumber(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int HogbenNumber( int a)
{
int p = ( int )(Math.pow(a, 2 ) - a + 1 );
return p;
}
public static void main(String args[])
{
int N = 10 ;
System.out.print(HogbenNumber(N));
}
}
|
Python3
def HogbenNumber(a):
p = ( pow (a, 2 ) - a + 1 )
return p
N = 10
print (HogbenNumber(N))
|
C#
using System;
class GFG{
public static int HogbenNumber( int a)
{
int p = ( int )(Math.Pow(a, 2) - a + 1);
return p;
}
public static void Main()
{
int N = 10;
Console.Write(HogbenNumber(N));
}
}
|
Javascript
<script>
function HogbenNumber(a)
{
let p = (Math.pow(a, 2) - a + 1);
return p;
}
let N = 10;
document.write(HogbenNumber(N));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...