Program to find Cullen Number
A Cullen Number is a number of the form is 2n * n + 1 where n is an integer. The first few Cullen numbers are 1, 3, 9, 25, 65, 161, 385, 897, 2049, 4609 . . . . . .
Examples:
Input : n = 4
Output :65
Input : n = 0
Output : 1
Input : n = 6
Output : 161
Below is implementation of formula. We use bitwise left-shift operator to find 2n, then multiply the result with n and finally returns (1 << n)*n + 1.
C++
#include <bits/stdc++.h>
using namespace std;
unsigned findCullen(unsigned n)
{
return (1 << n) * n + 1;
}
int main()
{
int n = 2;
cout << findCullen(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findCullen( int n)
{
return ( 1 << n) * n + 1 ;
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(findCullen(n));
}
}
|
Python3
def findCullen(n):
return ( 1 << n) * n + 1
n = 2
print (findCullen(n))
|
C#
using System;
class GFG {
static int findCullen( int n)
{
return (1 << n) * n + 1;
}
public static void Main()
{
int n = 2;
Console.WriteLine(findCullen(n));
}
}
|
PHP
<?php
function findCullen( $n )
{
return (1 << $n ) * $n + 1;
}
$n = 2;
echo findCullen( $n );
?>
|
Javascript
<script>
function findCullen(n)
{
return (1 << n) * n + 1;
}
let n = 2;
document.write(findCullen(n));
</script>
|
Output:
9
Time complexity : O(1)
Auxiliary Space : O(1)
Properties of Cullen Numbers:
- Most of the Cullen Numbers are composite numbers.
- n’th Cullen number is divisible by p = 2n – 1 if p is a prime number of the form 8k – 3.
Reference: https://en.wikipedia.org/wiki/Cullen_number
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...