Open In App

Find the Nth Hogben Numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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).

hagoben numbers
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:
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 (N^2 - N + 1)      .

Below is the implementation of the above approach. 

C++

// C++ program to print
// N-th Hogben Number
 
#include <bits/stdc++.h>
using namespace std;
 
// Function returns N-th
// Hogben Number
int HogbenNumber(int a)
{
    int p = (pow(a, 2) - a + 1);
    return p;
}
 
// Driver code
int main()
{
    int N = 10;
 
    cout << HogbenNumber(N);
 
    return 0;
}

                    

Java

// Java program to print
// N-th Hogben Number
import java.util.*;
 
class GFG{
     
// Function returns N-th
// Hogben Number
public static int HogbenNumber(int a)
{
    int p = (int)(Math.pow(a, 2) - a + 1);
    return p;
}
 
// Driver code
public static void main(String args[])
{
    int N = 10;
 
    System.out.print(HogbenNumber(N));
}
}
 
// This code is contributed by Akanksha_Rai

                    

Python3

# Python3 program to print
# N-th Hogben Number
 
# Function returns N-th
# Hogben Number
def HogbenNumber(a):
     
    p = (pow(a, 2) - a + 1)
    return p
 
# Driver code
N = 10
 
print(HogbenNumber(N))
 
# This code is contributed by shubhamsingh10

                    

C#

// C# program to print
// N-th Hogben Number
using System;
class GFG{
     
// Function returns N-th
// Hogben Number
public static int HogbenNumber(int a)
{
    int p = (int)(Math.Pow(a, 2) - a + 1);
    return p;
}
 
// Driver code
public static void Main()
{
    int N = 10;
 
    Console.Write(HogbenNumber(N));
}
}
 
// This code is contributed by Code_Mech

                    

Javascript

<script>
 
// Javascript program to print
// N-th Hogben Number
 
// Function returns N-th
// Hogben Number
function HogbenNumber(a)
{
    let p = (Math.pow(a, 2) - a + 1);
    return p;
}
  
  // Driver Code
     
    let N = 10;
   
    document.write(HogbenNumber(N));
   
</script>

                    

Output: 
91

 

Time complexity: O(1)
Auxiliary Space: O(1)



Last Updated : 29 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads