Given a positive integer n, the task is to print nth Hilbert Number.
Hilbert Number: In mathematics, A Hilbert Number is a positive integer of the form 4*n + 1 , Where n is a non-negative integer.
The first few Hilbert numbers are –
1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97
Examples :
Input: 5
Output: 21 ( i.e 4*5 + 1 )
Input: 9
Output: 37 (i.e 4*9 + 1 )
Approach:
- The n-th Hilbert Number of the sequence can be obtained by putting the value of n in the formula 4*n + 1.
Below is the implementation of the above idea:
CPP
#include <bits/stdc++.h>
using namespace std;
long nthHilbertNumber( int n)
{
return 4 * (n - 1) + 1;
}
int main()
{
int n = 5;
cout << nthHilbertNumber(n);
return 0;
}
|
JAVA
class GFG {
static long nthHilbertNumber( int n)
{
return 4 * (n - 1 ) + 1 ;
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(nthHilbertNumber(n));
}
}
|
Python
def nthHilbertNumber( n):
return 4 * (n - 1 ) + 1
n = 5
print (nthHilbertNumber(n))
|
C#
using System;
class GFG {
static long nthHilbertNumber( int n)
{
return 4 * (n - 1) + 1;
}
public static void Main()
{
int n = 5;
Console.WriteLine(nthHilbertNumber(n));
}
}
|
PHP
<?php
function nthHilbertNumber( $n )
{
return 4*( $n -1) + 1;
}
$n =5;
echo nthHilbertNumber( $n );
?>
|
Javascript
<script>
function nthHilbertNumber(n)
{
return 4 * (n - 1) + 1;
}
var n = 5;
document.write( nthHilbertNumber(n));
</script>
|
Time Complexity: O(1), performing constant multiplication and addition operations.
Auxiliary Space: O(1) because using constant space.
Last Updated :
26 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...